{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "file-upload-4",
  "dependencies": [
    "lucide-react",
    "react-icons"
  ],
  "registryDependencies": [
    "button",
    "card",
    "progress"
  ],
  "files": [
    {
      "path": "src/components/watermelon-ui/file-upload-4.tsx",
      "content": "import { useEffect, useRef, useState } from \"react\";\nimport {\n  FaCloudUploadAlt,\n  FaFileAlt,\n  FaFileImage,\n  FaFileWord,\n  FaSpinner,\n  FaTimes,\n} from \"react-icons/fa\";\nimport { Card, CardContent, CardHeader, CardTitle } from \"@/components/ui/card\";\nimport { Button } from \"@/components/ui/button\";\nimport { Progress } from \"@/components/ui/progress\";\n\ntype UploadStatus = \"uploading\" | \"completed\" | \"error\";\ntype UploadTone = \"default\" | \"danger\" | \"muted\";\n\ninterface UploadFile {\n  id: string;\n  file: File;\n  name: string;\n  size: string;\n  extension: string;\n  progress: number;\n  status: UploadStatus;\n}\n\nexport interface DocumentUploaderProps {\n  title?: string;\n  acceptedFormats?: string[];\n  maxFileSizeMb?: number;\n  minFileSizeMb?: number;\n  onClose?: () => void;\n  onUploadComplete?: (files: File[]) => void;\n}\n\nconst defaultAcceptedFormats = [\"jpg\", \"pdf\", \"svg\", \"png\", \"docx\"];\n\nfunction formatFileSize(bytes: number) {\n  if (bytes >= 1024 * 1024 * 1024) {\n    return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} gb`;\n  }\n\n  if (bytes >= 1024 * 1024) {\n    return `${(bytes / (1024 * 1024)).toFixed(1)} mb`;\n  }\n\n  return `${Math.max(1, Math.round(bytes / 1024))} kb`;\n}\n\nfunction getExtension(fileName: string) {\n  return fileName.split(\".\").pop()?.toLowerCase() ?? \"\";\n}\n\nfunction getFileTone(file: UploadFile): UploadTone {\n  if (file.status === \"error\") return \"danger\";\n  if (file.status === \"completed\") return \"muted\";\n  return \"default\";\n}\n\nfunction getFileIcon(extension: string) {\n  if ([\"jpg\", \"jpeg\", \"png\", \"svg\", \"webp\", \"gif\"].includes(extension)) {\n    return <FaFileImage className=\"text-muted-foreground h-7 w-7\" />;\n  }\n\n  if ([\"doc\", \"docx\"].includes(extension)) {\n    return <FaFileWord className=\"text-muted-foreground h-7 w-7\" />;\n  }\n\n  return <FaFileAlt className=\"text-muted-foreground h-7 w-7\" />;\n}\n\nexport default function DocumentUploader({\n  title = \"Upload Files\",\n  acceptedFormats = defaultAcceptedFormats,\n  maxFileSizeMb = 24_576,\n  minFileSizeMb = 20,\n  onUploadComplete,\n}: DocumentUploaderProps) {\n  const inputRef = useRef<HTMLInputElement>(null);\n  const [files, setFiles] = useState<UploadFile[]>([]);\n  const [isDragging, setIsDragging] = useState(false);\n\n  const accept = acceptedFormats.map((format) => `.${format}`).join(\",\");\n  const hasUploadingFiles = files.some((file) => file.status === \"uploading\");\n\n  useEffect(() => {\n    if (!hasUploadingFiles) return;\n\n    const interval = window.setInterval(() => {\n      setFiles((currentFiles) =>\n        currentFiles.map((file) => {\n          if (file.status !== \"uploading\") return file;\n\n          const nextProgress = Math.min(\n            file.progress + 8 + Math.random() * 12,\n            100,\n          );\n\n          return {\n            ...file,\n            progress: nextProgress,\n            status: nextProgress >= 100 ? \"completed\" : \"uploading\",\n          };\n        }),\n      );\n    }, 450);\n\n    return () => window.clearInterval(interval);\n  }, [hasUploadingFiles]);\n\n  useEffect(() => {\n    const completedFiles = files\n      .filter((file) => file.status === \"completed\")\n      .map((file) => file.file);\n\n    if (completedFiles.length && completedFiles.length === files.length) {\n      onUploadComplete?.(completedFiles);\n    }\n  }, [files, onUploadComplete]);\n\n  const addFiles = (selectedFiles: FileList | File[]) => {\n    const maxBytes = maxFileSizeMb * 1024 * 1024;\n    const acceptedSet = new Set(\n      acceptedFormats.map((format) => format.toLowerCase()),\n    );\n\n    setFiles((currentFiles) => [\n      ...currentFiles,\n      ...Array.from(selectedFiles).map((file) => {\n        const extension = getExtension(file.name);\n        const isAccepted = !acceptedSet.size || acceptedSet.has(extension);\n        const isTooLarge = file.size > maxBytes;\n        const status: UploadStatus =\n          isAccepted && !isTooLarge ? \"uploading\" : \"error\";\n\n        return {\n          id: `${file.name}-${file.lastModified}-${crypto.randomUUID()}`,\n          file,\n          name: file.name,\n          size: formatFileSize(file.size),\n          extension,\n          progress: status === \"uploading\" ? 6 : 100,\n          status,\n        };\n      }),\n    ]);\n  };\n\n  const removeFile = (id: string) => {\n    setFiles((currentFiles) => currentFiles.filter((file) => file.id !== id));\n  };\n\n  return (\n    <Card className=\"bg-card text-card-foreground mx-auto w-full max-w-3xl rounded shadow-none\">\n      <CardHeader>\n        <CardTitle className=\"text-xl font-normal tracking-tight\">\n          {title}\n        </CardTitle>\n      </CardHeader>\n\n      <CardContent className=\"grid gap-6 sm:grid-cols-[1.08fr_1fr]\">\n        <div className=\"space-y-2\">\n          <button\n            type=\"button\"\n            onClick={() => inputRef.current?.click()}\n            onDragEnter={(event) => {\n              event.preventDefault();\n              setIsDragging(true);\n            }}\n            onDragOver={(event) => event.preventDefault()}\n            onDragLeave={() => setIsDragging(false)}\n            onDrop={(event) => {\n              event.preventDefault();\n              setIsDragging(false);\n              addFiles(event.dataTransfer.files);\n            }}\n            className={`border-border bg-muted/30 flex aspect-[1.12] w-full flex-col items-center justify-center border border-dashed text-center transition-colors ${\n              isDragging\n                ? \"border-primary bg-primary/10\"\n                : \"hover:border-primary/60\"\n            }`}\n          >\n            <FaCloudUploadAlt className=\"text-muted-foreground/25 mb-4 h-14 w-14\" />\n            <span className=\"text-foreground text-lg font-normal\">\n              Drag files to upload\n            </span>\n            <span className=\"text-muted-foreground before:bg-border after:bg-border my-4 flex items-center gap-3 text-xs italic before:h-px before:w-10 after:h-px after:w-10\">\n              or select a file\n            </span>\n            <Button\n              type=\"button\"\n              onClick={(event) => {\n                event.stopPropagation();\n                inputRef.current?.click();\n              }}\n              className=\"rounded-none px-7\"\n            >\n              Choose File\n            </Button>\n          </button>\n\n          <input\n            ref={inputRef}\n            type=\"file\"\n            multiple\n            accept={accept}\n            className=\"sr-only\"\n            onChange={(event) => {\n              if (event.target.files) addFiles(event.target.files);\n              event.target.value = \"\";\n            }}\n          />\n\n          <div className=\"text-muted-foreground text-[10px] leading-tight font-medium\">\n            <p>Acceptable formats: {acceptedFormats.join(\", \")}</p>\n            <p>\n              Max file size is {maxFileSizeMb / 1024}GB and minimum size{\" \"}\n              {minFileSizeMb}MB\n            </p>\n          </div>\n        </div>\n\n        <div className=\"min-w-0\">\n          <div className=\"text-foreground mb-3 flex items-center gap-2 text-sm font-semibold\">\n            {hasUploadingFiles ? (\n              <FaSpinner className=\"text-muted-foreground h-3.5 w-3.5 animate-spin\" />\n            ) : null}\n            <span>{hasUploadingFiles ? \"Uploading...\" : \"Files\"}</span>\n          </div>\n\n          <div className=\"space-y-3\">\n            {files.length ? (\n              files.map((file) => {\n                const tone = getFileTone(file);\n\n                return (\n                  <div\n                    key={file.id}\n                    className=\"grid grid-cols-[auto_1fr_auto] gap-2\"\n                  >\n                    <div className=\"pt-1\">{getFileIcon(file.extension)}</div>\n                    <div className=\"min-w-0\">\n                      <div\n                        className={`flex items-baseline gap-3 text-sm ${\n                          tone === \"muted\"\n                            ? \"text-muted-foreground\"\n                            : \"text-foreground\"\n                        }`}\n                      >\n                        <span className=\"truncate\">{file.name}</span>\n                        <span className=\"text-muted-foreground shrink-0 text-[10px]\">\n                          {file.size}\n                        </span>\n                      </div>\n                      <Progress\n                        value={file.progress}\n                        className={`mt-2 h-1 ${\n                          tone === \"danger\"\n                            ? \"[&_[data-slot=progress-indicator]]:bg-destructive\"\n                            : tone === \"muted\"\n                              ? \"[&_[data-slot=progress-indicator]]:bg-primary\"\n                              : \"[&_[data-slot=progress-indicator]]:bg-chart-1\"\n                        }`}\n                      />\n                    </div>\n                    <Button\n                      type=\"button\"\n                      variant=\"ghost\"\n                      size=\"icon-xs\"\n                      onClick={() => removeFile(file.id)}\n                      aria-label={`Remove ${file.name}`}\n                      className=\"text-muted-foreground hover:text-foreground hover:bg-transparent\"\n                    >\n                      <FaTimes className=\"h-3 w-3\" />\n                    </Button>\n                  </div>\n                );\n              })\n            ) : (\n              <div className=\"border-border bg-muted/20 flex h-48 flex-col items-center justify-center border border-dashed text-center\">\n                <p className=\"text-foreground text-sm font-medium\">\n                  No files selected\n                </p>\n                <p className=\"text-muted-foreground mt-1 text-xs\">\n                  Uploaded files will appear here.\n                </p>\n              </div>\n            )}\n          </div>\n        </div>\n      </CardContent>\n    </Card>\n  );\n}\n",
      "type": "registry:ui",
      "target": "components/ui/file-upload-4.tsx"
    }
  ],
  "type": "registry:ui"
}
