"use client" import { useNotification } from "@/app/context" import { uploadFilesAction } from "@/app/files/actions" import { AlertCircle, CloudUpload, Loader2 } from "lucide-react" import { useRouter } from "next/navigation" import { startTransition, useEffect, useRef, useState } from "react" export default function ScreenDropArea({ children }: { children: React.ReactNode }) { const router = useRouter() const { showNotification } = useNotification() const [isDragging, setIsDragging] = useState(false) const [isUploading, setIsUploading] = useState(false) const [uploadError, setUploadError] = useState("") const dragCounter = useRef(0) const handleDragEnter = (e: React.DragEvent) => { e.preventDefault() e.stopPropagation() dragCounter.current++ if (dragCounter.current === 1) { setIsDragging(true) } } const handleDragOver = (e: React.DragEvent) => { e.preventDefault() e.stopPropagation() } const handleDragLeave = (e: React.DragEvent) => { e.preventDefault() e.stopPropagation() dragCounter.current-- if (dragCounter.current === 0) { setIsDragging(false) } } const handleDrop = async (e: React.DragEvent) => { e.preventDefault() e.stopPropagation() // Reset counter and dragging state dragCounter.current = 0 setIsDragging(false) setIsUploading(true) setUploadError("") const files = e.dataTransfer.files if (files && files.length > 0) { try { const formData = new FormData() for (let i = 0; i < files.length; i++) { formData.append("files", files[i]) } startTransition(async () => { const result = await uploadFilesAction(null, formData) if (result.success) { showNotification({ code: "sidebar.unsorted", message: "new" }) setTimeout(() => showNotification({ code: "sidebar.unsorted", message: "" }), 3000) router.push("/unsorted") } else { setUploadError(result.error ? result.error : "Something went wrong...") } setIsUploading(false) }) } catch (error) { console.error("Upload error:", error) setIsUploading(false) setUploadError(error instanceof Error ? error.message : "Something went wrong...") } } } // Add event listeners to document body useEffect(() => { document.body.addEventListener("dragenter", handleDragEnter as any) document.body.addEventListener("dragover", handleDragOver as any) document.body.addEventListener("dragleave", handleDragLeave as any) document.body.addEventListener("drop", handleDrop as any) return () => { document.body.removeEventListener("dragenter", handleDragEnter as any) document.body.removeEventListener("dragover", handleDragOver as any) document.body.removeEventListener("dragleave", handleDragLeave as any) document.body.removeEventListener("drop", handleDrop as any) } }, [isDragging]) return (
{children} {isDragging && (

Drop Files to Upload

Drop anywhere on the screen

)} {isUploading && (

Uploading...

)} {uploadError && (

Upload Error

{uploadError}

)}
) }