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, uploadVideoFile } from "../../../api/files.api"; const MAX_THUMB_BYTES = 5 * 1024 * 1024; const MAX_VIDEO_BYTES = 2 * 1024 * 1024 * 1024; const THUMB_TYPES = new Set(["image/jpeg", "image/png"]); const VIDEO_TYPES_PREFIX = "video/"; function isAllowedThumb(file: File): boolean { if (THUMB_TYPES.has(file.type)) return true; const n = file.name.toLowerCase(); return /\.(jpe?g|png)$/.test(n); } function isAllowedVideoFile(file: File): boolean { if (file.type.startsWith(VIDEO_TYPES_PREFIX)) return true; const n = file.name.toLowerCase(); return /\.(mp4|webm|mov|m4v|mkv)$/.test(n); } export type LessonMediaUploadKind = "thumbnail" | "video"; export interface LessonMediaUploadFieldProps { kind: LessonMediaUploadKind; value: string; onChange: (url: string) => void; disabled?: boolean; onUploadBusyChange?: (busy: boolean) => void; className?: string; } export function LessonMediaUploadField({ kind, value, onChange, disabled = false, onUploadBusyChange, className, }: LessonMediaUploadFieldProps) { 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 (kind === "thumbnail") { if (!isAllowedThumb(file)) { toast.error("Please use a JPG or PNG image."); return; } if (file.size > MAX_THUMB_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("Thumbnail uploaded"); } catch (e: unknown) { console.error(e); const msg = (e as { response?: { data?: { message?: string } } })?.response?.data ?.message ?? "Failed to upload thumbnail"; toast.error(msg); } finally { setBusy(false); } return; } if (!isAllowedVideoFile(file)) { toast.error("Please use a video file (e.g. MP4, WebM, MOV)."); return; } if (file.size > MAX_VIDEO_BYTES) { toast.error("Video is too large", { description: "Maximum size is 2 GB.", }); return; } setBusy(true); try { const res = await uploadVideoFile(file); const url = res.data?.data?.url?.trim(); if (!url) throw new Error("Upload did not return a file URL"); onChange(url); toast.success("Video uploaded"); } catch (e: unknown) { console.error(e); const msg = (e as { response?: { data?: { message?: string } } })?.response?.data ?.message ?? "Failed to upload video"; toast.error(msg); } finally { setBusy(false); } }, [disabled, uploading, kind, 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 isThumb = kind === "thumbnail"; const label = isThumb ? "Thumbnail" : "Video"; const hint = isThumb ? "JPG, PNG (MAX 5 MB)" : "MP4, MOV, WebM (MAX 2 GB)"; return (
onChange(e.target.value)} placeholder="https://…" className="h-12 rounded-xl" disabled={disabled || uploading} autoComplete="off" />
); }