From c2b70c13e988fba76a7d9bc6ddebeeab82eb5ed9 Mon Sep 17 00:00:00 2001 From: Hyteq Date: Mon, 23 Jun 2025 09:02:32 +0300 Subject: [PATCH] chore: cleaned up logic, comments, logic and overall structure of media panel to be more robust --- .../web/src/components/editor/media-panel.tsx | 291 ++++++++---------- 1 file changed, 120 insertions(+), 171 deletions(-) diff --git a/apps/web/src/components/editor/media-panel.tsx b/apps/web/src/components/editor/media-panel.tsx index 17fe041..f38e0f4 100644 --- a/apps/web/src/components/editor/media-panel.tsx +++ b/apps/web/src/components/editor/media-panel.tsx @@ -16,17 +16,17 @@ export function MediaPanel() { const [isProcessing, setIsProcessing] = useState(false); const processFiles = async (files: FileList | File[]) => { + if (!files?.length) return; + setIsProcessing(true); - try { - const processedItems = await processMediaFiles(files); - - for (const processedItem of processedItems) { - addMediaItem(processedItem); - toast.success(`Added ${processedItem.name} to project`); - } + const items = await processMediaFiles(files); + items.forEach(item => { + addMediaItem(item); + toast.success(`Added ${item.name}`); + }); } catch (error) { - console.error("Error processing files:", error); + console.error("File processing failed:", error); toast.error("Failed to process files"); } finally { setIsProcessing(false); @@ -34,82 +34,64 @@ export function MediaPanel() { }; const { isDragOver, dragProps } = useDragDrop({ - onDrop: (files) => { - processFiles(files); - }, + onDrop: processFiles, }); - const handleFileSelect = () => { - fileInputRef.current?.click(); + const handleFileSelect = () => fileInputRef.current?.click(); + + const handleFileChange = (e: React.ChangeEvent) => { + if (e.target.files) processFiles(e.target.files); + e.target.value = ""; }; - const handleFileInputChange = (e: React.ChangeEvent) => { - if (e.target.files && e.target.files.length > 0) { - processFiles(e.target.files); - // Reset the input so the same file can be selected again - e.target.value = ""; - } - }; - - const handleRemoveItem = (e: React.MouseEvent, itemId: string) => { + const handleRemove = (e: React.MouseEvent, id: string) => { e.stopPropagation(); - removeMediaItem(itemId); - toast.success("Media removed from project"); - }; - - const handleDragStart = (e: React.DragEvent, item: any) => { - // Mark this as an internal app drag - e.dataTransfer.setData( - "application/x-media-item", - JSON.stringify({ - id: item.id, - type: item.type, - name: item.name, - }) - ); - e.dataTransfer.effectAllowed = "copy"; + removeMediaItem(id); + toast.success("Media removed"); }; const formatDuration = (duration: number) => { - const minutes = Math.floor(duration / 60); - const seconds = Math.floor(duration % 60); - return `${minutes}:${seconds.toString().padStart(2, "0")}`; + const min = Math.floor(duration / 60); + const sec = Math.floor(duration % 60); + return `${min}:${sec.toString().padStart(2, "0")}`; }; - const getMediaIcon = (type: string) => { - switch (type) { - case "video": - return