From 9d63e62bc0ff2296d4068d3bd14cf8a76eb44fb1 Mon Sep 17 00:00:00 2001 From: satyamtiwari1499 Date: Fri, 22 May 2026 19:45:51 +0530 Subject: [PATCH] fix: use a single hidden file input --- src/components/FileUpload.tsx | 186 ++++++++++++++-------------------- 1 file changed, 77 insertions(+), 109 deletions(-) diff --git a/src/components/FileUpload.tsx b/src/components/FileUpload.tsx index 6e10fd77..58aa7ad4 100644 --- a/src/components/FileUpload.tsx +++ b/src/components/FileUpload.tsx @@ -1,7 +1,7 @@ "use client"; -import { useRef, useState, useEffect} from "react"; -import { Film, FolderOpen } from "lucide-react"; +import { useEffect, useRef, useState } from "react"; +import { FolderOpen, Film } from "lucide-react"; import LottiePlayer from "./LottiePlayer"; import uploadAnim from "@/lib/lottie/upload.json"; import { cn, formatBytes, formatDuration } from "@/lib/utils"; @@ -21,11 +21,10 @@ export default function FileUpload({ duration, }: Props) { const inputRef = useRef(null); - const [dragging, setDragging] = useState(false); const [error, setError] = useState(""); const [warning, setWarning] = useState(""); - + useEffect(() => { const handler = (e: KeyboardEvent) => { if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "o") { @@ -33,6 +32,7 @@ export default function FileUpload({ inputRef.current?.click(); } }; + document.addEventListener("keydown", handler); return () => document.removeEventListener("keydown", handler); }, []); @@ -41,7 +41,6 @@ export default function FileUpload({ setError(""); setWarning(""); - // Validate type if (!file.type.startsWith("video/")) { setError("Only video files are allowed."); return; @@ -52,19 +51,18 @@ export default function FileUpload({ return; } - // Hard limit if (file.size > MAX_FILE_SIZE) { setError( - `File too large (${formatBytes( - file.size - )}). Maximum allowed size is 2GB.` + `File too large (${formatBytes(file.size)}). Maximum allowed size is 2GB.` ); return; } - // Soft warning if (file.size > WARNING_FILE_SIZE) { - const estimatedMinutes = Math.max(1, Math.round(file.size / (100 * 1024 * 1024))); + const estimatedMinutes = Math.max( + 1, + Math.round(file.size / (100 * 1024 * 1024)) + ); setWarning( `Large file detected (${formatBytes( file.size @@ -75,6 +73,13 @@ export default function FileUpload({ onFileSelect(file); }; + const handleInputChange = (e: React.ChangeEvent) => { + const file = e.target.files?.[0]; + + if (file) handleFile(file); + e.target.value = ""; + }; + const handleDrop = (e: React.DragEvent) => { e.preventDefault(); setDragging(false); @@ -83,90 +88,64 @@ export default function FileUpload({ if (file) handleFile(file); }; - const FileInfo = () => ( -
-
- -
- -
- -
+ const openFilePicker = () => { + inputRef.current?.click(); + }; - - -
-
-

- {currentFile?.name} -

- {currentFile && ( - - {currentFile.name.includes(".") - ? currentFile.name.split(".").pop() - : "VIDEO"} - - )} + const FileInfo = () => ( +
+
+
+
+
-
-

{formatBytes(currentFile?.size ?? 0)}

- -

- {duration > 0 - ? `Duration: ${formatDuration(duration)}` - : "Loading duration..."} -

+ + + +
+
+

+ {currentFile?.name} +

+ {currentFile && ( + + {currentFile.name.includes(".") + ? currentFile.name.split(".").pop() + : "VIDEO"} + + )} +
+
+

{formatBytes(currentFile?.size ?? 0)}

+

+ {duration > 0 + ? `Duration: ${formatDuration(duration)}` + : "Loading duration..."} +

+
-
- + +
+

+ Supports: MP4, MOV, AVI, MKV, WebM, and most video formats +

{fileError && ( -

- {fileError} -

+

{fileError}

)} - { - const f = e.target.files?.[0]; - if (f) handleFile(f); - }} - />
+ ); -

- Supports: MP4, MOV, AVI, MKV, WebM, and most video formats -

- - {fileError && ( -

{fileError}

- )} - - { - const f = e.target.files?.[0]; - if (f) handleFile(f); - }} - /> -
-); const DropZone = () => (
setDragging(false)} onDrop={handleDrop} - onClick={() => inputRef.current?.click()} + onClick={openFilePicker} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") { - inputRef.current?.click(); + openFilePicker(); } }} className={cn( @@ -192,7 +171,6 @@ export default function FileUpload({ : "border-[var(--border)] bg-[var(--bg)] hover:border-film-400 hover:bg-film-50/40" )} > - {/* Premium Light Beam Shimmer Effect */} {dragging && (
)} @@ -202,14 +180,10 @@ export default function FileUpload({

- {dragging - ? "Release to upload" - : "Drag & Drop your video in here"} + {dragging ? "Release to upload" : "Drag & Drop your video in here"}

-

- or click to browse -

+

or click to browse

Ctrl+O / Cmd+O @@ -225,28 +199,14 @@ export default function FileUpload({ Supports: MP4, MOV, AVI, MKV, WebM, and most video formats up to 2GB

- {fileError && ( -

{fileError}

- )} + {fileError &&

{fileError}

} {currentFile && (

Selected: {formatBytes(currentFile.size)}

)} - - { - const f = e.target.files?.[0]; - - if (f) handleFile(f); - }} - /> -
+
); return ( @@ -256,6 +216,14 @@ export default function FileUpload({ {warning &&

{warning}

} {currentFile ? : } + +
); }