import { useCallback, useRef, useState, type ChangeEvent, type DragEvent } from "react"; import { CloudUpload } from "lucide-react"; import { toast } from "sonner"; import { Input } from "../../../components/ui/input"; import { cn } from "../../../lib/utils"; import { uploadImageFile } from "../../../api/files.api"; const MAX_ICON_BYTES = 5 * 1024 * 1024; const ALLOWED_IMAGE_TYPES = new Set(["image/jpeg", "image/png"]); function isAllowedImageFile(file: File): boolean { if (ALLOWED_IMAGE_TYPES.has(file.type)) return true; const name = file.name.toLowerCase(); return /\.(jpe?g|png)$/.test(name); } export interface ModuleIconUploadFieldProps { value: string; onChange: (url: string) => void; disabled?: boolean; /** Notifies parent so dialogs can block closing while an upload is in flight. */ onUploadBusyChange?: (busy: boolean) => void; className?: string; } export function ModuleIconUploadField({ value, onChange, disabled = false, onUploadBusyChange, className, }: ModuleIconUploadFieldProps) { const fileInputRef = useRef(null); const [uploading, setUploading] = useState(false); const [dragActive, setDragActive] = useState(false); const setBusy = useCallback( (next: boolean) => { setUploading(next); onUploadBusyChange?.(next); }, [onUploadBusyChange], ); const processFile = useCallback( async (file: File) => { if (disabled || uploading) return; if (!isAllowedImageFile(file)) { toast.error("Please use a JPG or PNG image."); return; } if (file.size > MAX_ICON_BYTES) { toast.error("Image is too large", { description: "Maximum size is 5 MB.", }); return; } setBusy(true); try { const res = await uploadImageFile(file); const url = res.data?.data?.url?.trim(); if (!url) { throw new Error("Upload did not return a file URL"); } onChange(url); toast.success("Icon uploaded"); } catch (e: unknown) { console.error(e); const msg = (e as { response?: { data?: { message?: string } } })?.response?.data ?.message ?? "Failed to upload icon"; toast.error(msg); } finally { setBusy(false); } }, [disabled, uploading, onChange, setBusy], ); const handleFileInputChange = (e: ChangeEvent) => { const file = e.target.files?.[0]; e.target.value = ""; if (file) void processFile(file); }; const handleDragOver = (e: DragEvent) => { e.preventDefault(); e.stopPropagation(); if (!disabled && !uploading) setDragActive(true); }; const handleDragLeave = (e: DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragActive(false); }; const handleDrop = (e: DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragActive(false); if (disabled || uploading) return; const file = e.dataTransfer.files?.[0]; if (file) void processFile(file); }; const zoneDisabled = disabled || uploading; const showSpinner = uploading; return (
onChange(e.target.value)} placeholder="https://…" className="h-12 rounded-xl" disabled={disabled || uploading} autoComplete="off" />
); }