2026-03-25 09:27:12 +08:00
|
|
|
import { FileImage, Upload } from "lucide-react";
|
|
|
|
|
import { type DragEvent, useCallback, useState } from "react";
|
2026-03-22 03:01:23 +08:00
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
|
|
|
|
interface DropzoneProps {
|
|
|
|
|
onFiles?: (files: File[]) => void;
|
|
|
|
|
accept?: string;
|
|
|
|
|
multiple?: boolean;
|
2026-03-22 04:03:30 +08:00
|
|
|
/** Files that have already been dropped (for showing count & list). */
|
|
|
|
|
currentFiles?: File[];
|
2026-03-22 03:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-21 09:59:57 +08:00
|
|
|
// Browsers may not map certain formats (HEIC, JXL, RAW, etc.) to image/* in file pickers.
|
2026-04-11 23:27:44 +08:00
|
|
|
// Append explicit extensions so they are selectable.
|
|
|
|
|
function expandAccept(accept?: string): string | undefined {
|
|
|
|
|
if (!accept?.includes("image/*")) return accept;
|
2026-04-21 09:59:57 +08:00
|
|
|
return `${accept},.heic,.heif,.hif,.jxl,.ico,.dng,.cr2,.nef,.arw,.orf,.rw2,.tga,.psd,.exr,.hdr`;
|
2026-04-11 23:27:44 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-22 04:03:30 +08:00
|
|
|
export function Dropzone({ onFiles, accept, multiple = true, currentFiles = [] }: DropzoneProps) {
|
2026-04-11 23:27:44 +08:00
|
|
|
const resolvedAccept = expandAccept(accept);
|
2026-03-22 03:01:23 +08:00
|
|
|
const [isDragging, setIsDragging] = useState(false);
|
|
|
|
|
|
|
|
|
|
const handleDrag = useCallback((e: DragEvent) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
if (e.type === "dragenter" || e.type === "dragover") setIsDragging(true);
|
|
|
|
|
else if (e.type === "dragleave") setIsDragging(false);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const handleDrop = useCallback(
|
|
|
|
|
(e: DragEvent) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
setIsDragging(false);
|
|
|
|
|
const files = Array.from(e.dataTransfer.files);
|
|
|
|
|
if (files.length > 0) onFiles?.(files);
|
|
|
|
|
},
|
2026-03-25 09:27:12 +08:00
|
|
|
[onFiles],
|
2026-03-22 03:01:23 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleClick = () => {
|
|
|
|
|
const input = document.createElement("input");
|
|
|
|
|
input.type = "file";
|
|
|
|
|
input.multiple = multiple;
|
2026-04-11 23:27:44 +08:00
|
|
|
if (resolvedAccept) input.accept = resolvedAccept;
|
2026-03-22 03:01:23 +08:00
|
|
|
input.onchange = (e) => {
|
|
|
|
|
const files = Array.from((e.target as HTMLInputElement).files || []);
|
|
|
|
|
if (files.length > 0) onFiles?.(files);
|
|
|
|
|
};
|
|
|
|
|
input.click();
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-22 04:03:30 +08:00
|
|
|
const hasMultipleFiles = currentFiles.length > 1;
|
|
|
|
|
|
2026-03-22 03:01:23 +08:00
|
|
|
return (
|
2026-03-26 01:10:13 +08:00
|
|
|
<section
|
|
|
|
|
aria-label="File drop zone"
|
2026-03-22 03:01:23 +08:00
|
|
|
onDragEnter={handleDrag}
|
|
|
|
|
onDragOver={handleDrag}
|
|
|
|
|
onDragLeave={handleDrag}
|
|
|
|
|
onDrop={handleDrop}
|
|
|
|
|
className={cn(
|
2026-03-26 01:10:13 +08:00
|
|
|
"flex flex-col items-center justify-center rounded-2xl border-2 border-dashed transition-colors min-h-[400px] mx-auto max-w-2xl w-full",
|
2026-03-22 03:01:23 +08:00
|
|
|
isDragging
|
|
|
|
|
? "border-primary bg-primary/5"
|
2026-03-25 09:27:12 +08:00
|
|
|
: "border-border bg-muted/30 hover:border-primary/50 hover:bg-muted/50",
|
2026-03-22 03:01:23 +08:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<div className="flex flex-col items-center gap-4 p-8">
|
|
|
|
|
<div className="text-3xl font-bold text-muted-foreground/30">
|
2026-04-14 20:55:42 +08:00
|
|
|
<span className="text-primary/30">ashim</span>
|
2026-03-22 03:01:23 +08:00
|
|
|
</div>
|
2026-03-26 01:10:13 +08:00
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={handleClick}
|
|
|
|
|
className="flex items-center gap-2 px-6 py-2.5 rounded-lg border border-primary text-primary hover:bg-primary/5 transition-colors text-sm font-medium"
|
|
|
|
|
>
|
2026-03-22 03:01:23 +08:00
|
|
|
<Upload className="h-4 w-4" />
|
|
|
|
|
Upload from computer
|
|
|
|
|
</button>
|
2026-03-25 09:27:12 +08:00
|
|
|
<p className="text-sm text-muted-foreground">Drop files here or click the upload button</p>
|
2026-03-22 04:03:30 +08:00
|
|
|
|
|
|
|
|
{/* Show file count badge and list when multiple files are dropped */}
|
|
|
|
|
{hasMultipleFiles && (
|
|
|
|
|
<div className="flex flex-col items-center gap-2 mt-2">
|
|
|
|
|
<span className="inline-flex items-center gap-1.5 px-3 py-1 rounded-full bg-primary/10 text-primary text-xs font-medium">
|
|
|
|
|
<FileImage className="h-3.5 w-3.5" />
|
|
|
|
|
{currentFiles.length} files selected
|
|
|
|
|
</span>
|
|
|
|
|
<div className="max-h-32 overflow-y-auto w-full max-w-xs">
|
2026-03-26 01:10:13 +08:00
|
|
|
{currentFiles.map((f) => (
|
2026-03-22 04:03:30 +08:00
|
|
|
<div
|
2026-03-26 01:10:13 +08:00
|
|
|
key={f.name}
|
2026-03-22 04:03:30 +08:00
|
|
|
className="flex items-center justify-between text-xs text-muted-foreground px-2 py-0.5"
|
|
|
|
|
>
|
|
|
|
|
<span className="truncate">{f.name}</span>
|
|
|
|
|
<span className="shrink-0 ml-2">{(f.size / 1024).toFixed(0)} KB</span>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-03-22 03:01:23 +08:00
|
|
|
</div>
|
2026-03-26 01:10:13 +08:00
|
|
|
</section>
|
2026-03-22 03:01:23 +08:00
|
|
|
);
|
|
|
|
|
}
|