"use client"; import { useEffect, useState, useRef } from "react"; import { api } from "@/lib/api"; import { cn } from "@/lib/utils"; import { X, Upload, Film, Check } from "lucide-react"; interface MediaItem { id: string; slug: string; type: "image" | "video"; mimeType: string; size: number; originalFilename: string; url: string; } interface MediaPickerModalProps { onSelect: (mediaId: string) => void; onClose: () => void; selectedId?: string | null; } export function MediaPickerModal({ onSelect, onClose, selectedId }: MediaPickerModalProps) { const [media, setMedia] = useState([]); const [loading, setLoading] = useState(true); const [uploading, setUploading] = useState(false); const [error, setError] = useState(""); const fileInputRef = useRef(null); const loadMedia = async () => { try { const data = await api.getMediaList(); setMedia(data.filter((m: MediaItem) => m.type === "image")); } catch (err: any) { setError(err.message); } finally { setLoading(false); } }; useEffect(() => { loadMedia(); }, []); useEffect(() => { const handleKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); }; window.addEventListener("keydown", handleKey); return () => window.removeEventListener("keydown", handleKey); }, [onClose]); const handleUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; setUploading(true); setError(""); try { const result = await api.uploadMedia(file); await loadMedia(); onSelect(result.id); } catch (err: any) { setError(err.message); } finally { setUploading(false); if (fileInputRef.current) fileInputRef.current.value = ""; } }; return (

Select Image

{error &&

{error}

}
{loading ? (

Loading media...

) : media.length === 0 ? (

No images available.

Upload an image to get started.

) : (
{media.map((item) => ( ))}
)}
); }