"use client"; import { useState, useEffect, useCallback, useRef } from "react"; import { useDropzone } from "react-dropzone"; import { Howl } from "howler"; // ─── Types ────────────────────────────────────────────────────────── type LibrarySound = { id: string; originalName: string; url: string; duration: number; }; type LibraryImage = { id: string; originalName: string; url: string; mimeType: string; fileSize: number; }; type FreesoundResult = { id: number; name: string; duration: number; previewUrl: string | null; }; export type DragData = | { type: "sound"; url: string; originalName: string } | { type: "image"; url: string; originalName: string } | { type: "emoji"; emoji: string }; // ─── Howl helper ──────────────────────────────────────────────────── const howlCache = new Map(); function playPreview(url: string) { let h = howlCache.get(url); if (!h) { h = new Howl({ src: [url], format: ["mp3", "ogg"], volume: 0.4, preload: true }); howlCache.set(url, h); } h.play(); } // ─── Emoji Picker (inline, searchable) ────────────────────────────── const EMOJI_LIST = [ "😀", "😂", "🤣", "😍", "🥰", "😘", "😊", "😎", "🤩", "🥳", "🤯", "😱", "😈", "🤡", "💀", "👻", "👽", "🤖", "🎃", "😺", "🙈", "🙉", "🙊", "💩", "🔥", "🌊", "💥", "💫", "⭐", "🌟", "✨", "⚡", "☄️", "💧", "🧊", "🌋", "🎉", "🎊", "🎈", "🎁", "🏆", "🥇", "💎", "🔮", "🪄", "🧨", "🔫", "⚔️", "🛡️", "🚀", "🛸", "🚁", "⚓", "🌀", "🌈", "🌪️", "☢️", "☣️", "⚕️", "🧬", "💉", "💊", "🩸", "🧪", "🔬", "🔭", "📡", "🎯", "🎲", "♟️", "🧩", "🎭", "🎵", "🎶", "🎺", "📯", "🔔", "🎤", "🎧", "📢", "📣", "🔊", "🔇", "💤", "💣", "🔪", "🪦", "⚰️", "🪤", "🧲", "🗝️", "🔑", "🔓", "🔒", "🛠️", "⛓️", "🧹", "🪠", "🔧", "⚙️", "📦", "📀", "💿", "📹", "📸", "📷", "🖼️", "🎨", "🍺", "🍻", "🍷", "🥃", "🍸", "🍹", "🧉", "🍕", "🍔", "🌭", "🍿", "🧀", "🥜", "🌶️", "🍄", "🥚", "🧅", "🥕", "🥦", "🫁", "🧠", "👀", "👁️", "🫀", "💋", "🫂", "🤝", "👍", "👎", "👊", "✊", "🤛", "🤜", "👆", "👇", "🖕", "💪", "🦵", "🦶", "👂", "👃", "🧑‍🚀", "🧑‍🔧", "🧑‍⚕️", "🧑‍✈️", "🧑‍🏫", "🧑‍🎤", "🧑‍🍳", "🐶", "🐱", "🐭", "🐹", "🐰", "🦊", "🐻", "🐼", "🐨", "🐯", "🦁", "🐮", "🦈", "🐙", "🦑", "🐋", "🐬", "🦭", "🐊", "🦎", "🐍", "🐢", "🦖", "🦕", "🦟", "🦗", "🐛", "🪱", "🦋", "🐌", "🐞", "🐜", "🦂", "🕷️", "🦀", "🪸", ]; function EmojiPickerGrid({ onSelect }: { onSelect: (emoji: string) => void }) { const [search, setSearch] = useState(""); const filtered = search ? EMOJI_LIST.filter(e => e.includes(search)) : EMOJI_LIST; return (
setSearch(e.target.value)} className="w-full bg-slate-800/60 border border-slate-700/30 rounded px-2 py-1.5 text-xs font-mono text-slate-200 placeholder:text-slate-600 outline-none focus:border-cyan-500/50" />
{filtered.map(e => ( ))}
); } // ─── Sounds Tab ───────────────────────────────────────────────────── function SoundsTab() { const [sounds, setSounds] = useState([]); const [uploading, setUploading] = useState(false); const [progress, setProgress] = useState(0); const [searchQuery, setSearchQuery] = useState(""); const [searchResults, setSearchResults] = useState([]); const [searching, setSearching] = useState(false); const [showSearch, setShowSearch] = useState(false); const searchTimer = useRef>(undefined); const fetchSounds = useCallback(async () => { try { const res = await fetch("/api/sounds"); const data = await res.json(); setSounds(data.sounds || []); } catch {} }, []); useEffect(() => { fetchSounds(); }, [fetchSounds]); const { getRootProps, getInputProps } = useDropzone({ onDrop: async (files) => { if (!files.length) return; setUploading(true); setProgress(0); const formData = new FormData(); files.forEach(f => formData.append("files", f)); try { const xhr = new XMLHttpRequest(); xhr.upload.onprogress = e => { if (e.lengthComputable) setProgress(Math.round((e.loaded / e.total) * 100)); }; await new Promise((resolve, reject) => { xhr.onload = () => { if (xhr.status === 200) resolve(); else reject(); }; xhr.onerror = () => reject(); xhr.open("POST", "/api/sounds"); xhr.send(formData); }); await fetchSounds(); } catch {} setUploading(false); }, accept: { "audio/ogg": [".ogg"] }, multiple: true, }); const deleteSound = async (filename: string) => { await fetch(`/api/sounds?filename=${encodeURIComponent(filename)}`, { method: "DELETE" }); await fetchSounds(); }; // Freesound search with debounce const doSearch = useCallback(async (q: string) => { if (!q.trim()) { setSearchResults([]); return; } setSearching(true); try { const res = await fetch(`/api/sounds/search?q=${encodeURIComponent(q)}&page=1`); const data = await res.json(); setSearchResults(data.results || []); } catch {} setSearching(false); }, []); useEffect(() => { clearTimeout(searchTimer.current); if (!searchQuery.trim()) { setSearchResults([]); return; } searchTimer.current = setTimeout(() => doSearch(searchQuery), 300); return () => clearTimeout(searchTimer.current); }, [searchQuery, doSearch]); const importSound = async (result: FreesoundResult) => { if (!result.previewUrl) return; await fetch("/api/sounds/import", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: result.name, duration: result.duration, previewUrl: result.previewUrl }), }); await fetchSounds(); }; return (
{uploading && (
Uploading... {progress}%
)}

Drop OGG files here or click to upload

{/* Library */} {sounds.length > 0 && (
{sounds.map(s => { const filename = s.url.split("/").pop() || ""; return ( playPreview(s.url)} onDelete={() => deleteSound(filename)} /> ); })}
)} {/* Freesound toggle */} {showSearch && (
setSearchQuery(e.target.value)} className="w-full bg-slate-800/60 border border-slate-700/30 rounded px-2 py-1.5 text-xs font-mono text-slate-200 placeholder:text-slate-600 outline-none focus:border-cyan-500/50" /> {searching &&

Searching...

}
{searchResults.map(r => ( r.previewUrl && playPreview(r.previewUrl)} onImport={() => importSound(r)} /> ))}
)}
); } // ─── Images Tab ───────────────────────────────────────────────────── function ImagesTab() { const [images, setImages] = useState([]); const [uploading, setUploading] = useState(false); const [progress, setProgress] = useState(0); const fetchImages = useCallback(async () => { try { const res = await fetch("/api/images"); const data = await res.json(); setImages(data.images || []); } catch {} }, []); useEffect(() => { fetchImages(); }, [fetchImages]); const { getRootProps, getInputProps } = useDropzone({ onDrop: async (files) => { if (!files.length) return; setUploading(true); setProgress(0); const formData = new FormData(); files.forEach(f => formData.append("files", f)); try { const xhr = new XMLHttpRequest(); xhr.upload.onprogress = e => { if (e.lengthComputable) setProgress(Math.round((e.loaded / e.total) * 100)); }; await new Promise((resolve, reject) => { xhr.onload = () => { if (xhr.status === 200) resolve(); else reject(); }; xhr.onerror = () => reject(); xhr.open("POST", "/api/images"); xhr.send(formData); }); await fetchImages(); } catch {} setUploading(false); }, accept: { "image/*": [".png", ".jpg", ".jpeg", ".gif", ".webp"] }, multiple: true, }); const deleteImage = async (filename: string) => { await fetch(`/api/images?filename=${encodeURIComponent(filename)}`, { method: "DELETE" }); await fetchImages(); }; return (
{uploading && (
Uploading... {progress}%
)}

Drop images here or click to upload

{images.map(img => { const filename = img.url.split("/").pop() || ""; return (
{ ev.dataTransfer.setData("application/json", JSON.stringify({ type: "image", url: img.url, originalName: img.originalName } satisfies DragData)); }} className="relative group aspect-square rounded border border-slate-700/30 bg-slate-800/40 overflow-hidden cursor-grab active:cursor-grabbing" > {/* eslint-disable-next-line @next/next/no-img-element */} {img.originalName}
); })}
); } // ─── Shared Row Components ────────────────────────────────────────── function SoundRow({ label, duration, url, onPlay, onDelete }: { label: string; duration: number; url: string; onPlay: () => void; onDelete: () => void; }) { return (
{ ev.dataTransfer.setData("application/json", JSON.stringify({ type: "sound", url, originalName: label } satisfies DragData)); }} className="flex items-center gap-2 px-2 py-1.5 rounded bg-slate-800/30 border border-slate-700/20 text-[10px] font-mono cursor-grab active:cursor-grabbing hover:bg-slate-700/30 transition-colors group" > 🔊 {label} {duration.toFixed(1)}s
); } function FreesoundRow({ name, duration, onPreview, onImport }: { name: string; duration: number; onPreview: () => void; onImport: () => void; }) { return (
🌐 {name} {duration.toFixed(1)}s
); } // ─── Main ResourceBrowser ─────────────────────────────────────────── type Tab = "sounds" | "images" | "emojis"; export function ResourceBrowser({ visible, onClose }: { visible: boolean; onClose: () => void }) { const [tab, setTab] = useState("sounds"); if (!visible) return null; return (
{/* Tab bar */}
{([ { key: "sounds" as const, label: "🔊 Sounds" }, { key: "images" as const, label: "🖼️ Images" }, { key: "emojis" as const, label: "😀 Emojis" }, ]).map(t => ( ))}
{/* Content */}
{tab === "sounds" && } {tab === "images" && } {tab === "emojis" && }
); } function EmojisTab() { const handleSelect = (emoji: string) => { // Dispatch a custom event so ItemEditor can pick it up window.dispatchEvent(new CustomEvent("emoji-select", { detail: { emoji } })); }; return
; }