{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "comp-331",
  "type": "registry:component",
  "title": "Comp 331",
  "description": "Comp 331",
  "files": [
    {
      "path": "registry/ui-basic/comp-331.tsx",
      "content": "\"use client\";\n\nimport { useId, useState } from \"react\";\n\nimport { Button } from \"@/components/ui/button\";\nimport {\n\tDialog,\n\tDialogClose,\n\tDialogContent,\n\tDialogDescription,\n\tDialogFooter,\n\tDialogHeader,\n\tDialogTitle,\n\tDialogTrigger,\n} from \"@/components/ui/dialog\";\nimport { Input } from \"@/components/ui/input\";\nimport { Label } from \"@/components/ui/label\";\nimport { Textarea } from \"@/components/ui/textarea\";\nimport { CheckIcon, ImagePlusIcon, XIcon } from \"lucide-react\";\n\nimport { useCharacterLimit } from \"../utilities/useCharacterLimit\";\nimport { useFileUpload } from \"../utilities/usefileUpload\";\n\n// Pretend we have initial image files\nconst initialBgImage = [\n\t{\n\t\tname: \"profile-bg.jpg\",\n\t\tsize: 1528737,\n\t\ttype: \"image/jpeg\",\n\t\turl: \"/profile-bg.jpg\",\n\t\tid: \"profile-bg-123456789\",\n\t},\n];\n\nconst initialAvatarImage = [\n\t{\n\t\tname: \"avatar-72-01.jpg\",\n\t\tsize: 1528737,\n\t\ttype: \"image/jpeg\",\n\t\turl: \"/avatar-72-01.jpg\",\n\t\tid: \"avatar-123456789\",\n\t},\n];\n\nexport default function Component() {\n\tconst id = useId();\n\n\tconst maxLength = 180;\n\tconst {\n\t\tvalue,\n\t\tcharacterCount,\n\t\thandleChange,\n\t\tmaxLength: limit,\n\t} = useCharacterLimit({\n\t\tmaxLength,\n\t\tinitialValue:\n\t\t\t\"Hey, I am Margaret, a web developer who loves turning ideas into amazing websites!\",\n\t});\n\n\treturn (\n\t\t<Dialog>\n\t\t\t<DialogTrigger asChild>\n\t\t\t\t<Button variant=\"outline\">Edit profile</Button>\n\t\t\t</DialogTrigger>\n\t\t\t<DialogContent className=\"flex flex-col gap-0 overflow-y-visible p-0 sm:max-w-lg [&>button:last-child]:top-3.5\">\n\t\t\t\t<DialogHeader className=\"contents space-y-0 text-left\">\n\t\t\t\t\t<DialogTitle className=\"border-b px-6 py-4 text-base\">\n\t\t\t\t\t\tEdit profile\n\t\t\t\t\t</DialogTitle>\n\t\t\t\t</DialogHeader>\n\t\t\t\t<DialogDescription className=\"sr-only\">\n\t\t\t\t\tMake changes to your profile here. You can change your photo and\n\t\t\t\t\tset a username.\n\t\t\t\t</DialogDescription>\n\t\t\t\t<div className=\"overflow-y-auto\">\n\t\t\t\t\t<ProfileBg />\n\t\t\t\t\t<Avatar />\n\t\t\t\t\t<div className=\"px-6 pt-4 pb-6\">\n\t\t\t\t\t\t<form className=\"space-y-4\">\n\t\t\t\t\t\t\t<div className=\"flex flex-col gap-4 sm:flex-row\">\n\t\t\t\t\t\t\t\t<div className=\"flex-1 space-y-2\">\n\t\t\t\t\t\t\t\t\t<Label htmlFor={`${id}-first-name`}>\n\t\t\t\t\t\t\t\t\t\tFirst name\n\t\t\t\t\t\t\t\t\t</Label>\n\t\t\t\t\t\t\t\t\t<Input\n\t\t\t\t\t\t\t\t\t\tid={`${id}-first-name`}\n\t\t\t\t\t\t\t\t\t\tplaceholder=\"Matt\"\n\t\t\t\t\t\t\t\t\t\tdefaultValue=\"Margaret\"\n\t\t\t\t\t\t\t\t\t\ttype=\"text\"\n\t\t\t\t\t\t\t\t\t\trequired\n\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t<div className=\"flex-1 space-y-2\">\n\t\t\t\t\t\t\t\t\t<Label htmlFor={`${id}-last-name`}>Last name</Label>\n\t\t\t\t\t\t\t\t\t<Input\n\t\t\t\t\t\t\t\t\t\tid={`${id}-last-name`}\n\t\t\t\t\t\t\t\t\t\tplaceholder=\"Welsh\"\n\t\t\t\t\t\t\t\t\t\tdefaultValue=\"Villard\"\n\t\t\t\t\t\t\t\t\t\ttype=\"text\"\n\t\t\t\t\t\t\t\t\t\trequired\n\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t<div className=\"not-first:*:mt-2\">\n\t\t\t\t\t\t\t\t<Label htmlFor={`${id}-username`}>Username</Label>\n\t\t\t\t\t\t\t\t<div className=\"relative\">\n\t\t\t\t\t\t\t\t\t<Input\n\t\t\t\t\t\t\t\t\t\tid={`${id}-username`}\n\t\t\t\t\t\t\t\t\t\tclassName=\"peer pe-9\"\n\t\t\t\t\t\t\t\t\t\tplaceholder=\"Username\"\n\t\t\t\t\t\t\t\t\t\tdefaultValue=\"margaret-villard-69\"\n\t\t\t\t\t\t\t\t\t\ttype=\"text\"\n\t\t\t\t\t\t\t\t\t\trequired\n\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t\t<div className=\"text-muted-foreground/80 pointer-events-none absolute inset-y-0 inset-e-0 flex items-center justify-center pe-3 peer-disabled:opacity-50\">\n\t\t\t\t\t\t\t\t\t\t<CheckIcon\n\t\t\t\t\t\t\t\t\t\t\tsize={16}\n\t\t\t\t\t\t\t\t\t\t\tclassName=\"text-emerald-500\"\n\t\t\t\t\t\t\t\t\t\t\taria-hidden=\"true\"\n\t\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t<div className=\"not-first:*:mt-2\">\n\t\t\t\t\t\t\t\t<Label htmlFor={`${id}-website`}>Website</Label>\n\t\t\t\t\t\t\t\t<div className=\"flex rounded-md shadow-2xs\">\n\t\t\t\t\t\t\t\t\t<span className=\"border-input bg-background text-muted-foreground -z-10 inline-flex items-center rounded-s-md border px-3 text-sm\">\n\t\t\t\t\t\t\t\t\t\thttps://\n\t\t\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t\t\t\t<Input\n\t\t\t\t\t\t\t\t\t\tid={`${id}-website`}\n\t\t\t\t\t\t\t\t\t\tclassName=\"-ms-px rounded-s-none shadow-none\"\n\t\t\t\t\t\t\t\t\t\tplaceholder=\"yourwebsite.com\"\n\t\t\t\t\t\t\t\t\t\tdefaultValue=\"www.margaret.com\"\n\t\t\t\t\t\t\t\t\t\ttype=\"text\"\n\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t<div className=\"not-first:*:mt-2\">\n\t\t\t\t\t\t\t\t<Label htmlFor={`${id}-bio`}>Biography</Label>\n\t\t\t\t\t\t\t\t<Textarea\n\t\t\t\t\t\t\t\t\tid={`${id}-bio`}\n\t\t\t\t\t\t\t\t\tplaceholder=\"Write a few sentences about yourself\"\n\t\t\t\t\t\t\t\t\tdefaultValue={value}\n\t\t\t\t\t\t\t\t\tmaxLength={maxLength}\n\t\t\t\t\t\t\t\t\tonChange={handleChange}\n\t\t\t\t\t\t\t\t\taria-describedby={`${id}-description`}\n\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t<p\n\t\t\t\t\t\t\t\t\tid={`${id}-description`}\n\t\t\t\t\t\t\t\t\tclassName=\"text-muted-foreground mt-2 text-right text-xs\"\n\t\t\t\t\t\t\t\t\trole=\"status\"\n\t\t\t\t\t\t\t\t\taria-live=\"polite\"\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t<span className=\"tabular-nums\">\n\t\t\t\t\t\t\t\t\t\t{limit - characterCount}\n\t\t\t\t\t\t\t\t\t</span>{\" \"}\n\t\t\t\t\t\t\t\t\tcharacters left\n\t\t\t\t\t\t\t\t</p>\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t</form>\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n\t\t\t\t<DialogFooter className=\"border-t px-6 py-4\">\n\t\t\t\t\t<DialogClose asChild>\n\t\t\t\t\t\t<Button type=\"button\" variant=\"outline\">\n\t\t\t\t\t\t\tCancel\n\t\t\t\t\t\t</Button>\n\t\t\t\t\t</DialogClose>\n\t\t\t\t\t<DialogClose asChild>\n\t\t\t\t\t\t<Button type=\"button\">Save changes</Button>\n\t\t\t\t\t</DialogClose>\n\t\t\t\t</DialogFooter>\n\t\t\t</DialogContent>\n\t\t</Dialog>\n\t);\n}\n\nfunction ProfileBg() {\n\tconst [{ files }, { removeFile, openFileDialog, getInputProps }] =\n\t\tuseFileUpload({\n\t\t\taccept: \"image/*\",\n\t\t\tinitialFiles: initialBgImage,\n\t\t});\n\n\tconst currentImage = files[0]?.preview || null;\n\n\treturn (\n\t\t<div className=\"h-32\">\n\t\t\t<div className=\"bg-muted relative flex size-full items-center justify-center overflow-hidden\">\n\t\t\t\t{currentImage && (\n\t\t\t\t\t<img\n\t\t\t\t\t\tclassName=\"size-full object-cover\"\n\t\t\t\t\t\tsrc={currentImage}\n\t\t\t\t\t\talt={\n\t\t\t\t\t\t\tfiles[0]?.preview\n\t\t\t\t\t\t\t\t? \"Preview of uploaded image\"\n\t\t\t\t\t\t\t\t: \"Default profile background\"\n\t\t\t\t\t\t}\n\t\t\t\t\t\twidth={512}\n\t\t\t\t\t\theight={96}\n\t\t\t\t\t/>\n\t\t\t\t)}\n\t\t\t\t<div className=\"absolute inset-0 flex items-center justify-center gap-2\">\n\t\t\t\t\t<button\n\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\tclassName=\"focus-visible:border-ring focus-visible:ring-ring/50 z-50 flex size-10 cursor-pointer items-center justify-center rounded-full bg-black/60 text-white transition-[color,box-shadow] outline-hidden hover:bg-black/80 focus-visible:ring-[3px]\"\n\t\t\t\t\t\tonClick={openFileDialog}\n\t\t\t\t\t\taria-label={currentImage ? \"Change image\" : \"Upload image\"}\n\t\t\t\t\t>\n\t\t\t\t\t\t<ImagePlusIcon size={16} aria-hidden=\"true\" />\n\t\t\t\t\t</button>\n\t\t\t\t\t{currentImage && (\n\t\t\t\t\t\t<button\n\t\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\t\tclassName=\"focus-visible:border-ring focus-visible:ring-ring/50 z-50 flex size-10 cursor-pointer items-center justify-center rounded-full bg-black/60 text-white transition-[color,box-shadow] outline-hidden hover:bg-black/80 focus-visible:ring-[3px]\"\n\t\t\t\t\t\t\tonClick={() => removeFile(files[0]?.id)}\n\t\t\t\t\t\t\taria-label=\"Remove image\"\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<XIcon size={16} aria-hidden=\"true\" />\n\t\t\t\t\t\t</button>\n\t\t\t\t\t)}\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t\t<input\n\t\t\t\t{...getInputProps()}\n\t\t\t\tclassName=\"sr-only\"\n\t\t\t\taria-label=\"Upload image file\"\n\t\t\t/>\n\t\t</div>\n\t);\n}\n\nfunction Avatar() {\n\tconst [{ files }, { openFileDialog, getInputProps }] = useFileUpload({\n\t\taccept: \"image/*\",\n\t\tinitialFiles: initialAvatarImage,\n\t});\n\n\tconst currentImage = files[0]?.preview || null;\n\n\treturn (\n\t\t<div className=\"-mt-10 px-6\">\n\t\t\t<div className=\"border-background bg-muted relative flex size-20 items-center justify-center overflow-hidden rounded-full border-4 shadow-2xs shadow-black/10\">\n\t\t\t\t{currentImage && (\n\t\t\t\t\t<img\n\t\t\t\t\t\tsrc={currentImage}\n\t\t\t\t\t\tclassName=\"size-full object-cover\"\n\t\t\t\t\t\twidth={80}\n\t\t\t\t\t\theight={80}\n\t\t\t\t\t\talt=\"Profile image\"\n\t\t\t\t\t/>\n\t\t\t\t)}\n\t\t\t\t<button\n\t\t\t\t\ttype=\"button\"\n\t\t\t\t\tclassName=\"focus-visible:border-ring focus-visible:ring-ring/50 absolute flex size-8 cursor-pointer items-center justify-center rounded-full bg-black/60 text-white transition-[color,box-shadow] outline-hidden hover:bg-black/80 focus-visible:ring-[3px]\"\n\t\t\t\t\tonClick={openFileDialog}\n\t\t\t\t\taria-label=\"Change profile picture\"\n\t\t\t\t>\n\t\t\t\t\t<ImagePlusIcon size={16} aria-hidden=\"true\" />\n\t\t\t\t</button>\n\t\t\t\t<input\n\t\t\t\t\t{...getInputProps()}\n\t\t\t\t\tclassName=\"sr-only\"\n\t\t\t\t\taria-label=\"Upload profile picture\"\n\t\t\t\t/>\n\t\t\t</div>\n\t\t</div>\n\t);\n}\n",
      "type": "registry:ui"
    },
    {
      "path": "components/ui/button.tsx",
      "content": "import * as React from \"react\";\r\n\r\nimport { cn } from \"@/registry/utilities/cn\";\r\nimport { Slot } from \"@radix-ui/react-slot\";\r\nimport { cva, type VariantProps } from \"class-variance-authority\";\r\n\r\nconst buttonVariants = cva(\r\n\t\"inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm text-white hover:text-gray-400 font-medium ring-offset-background transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50\",\r\n\t{\r\n\t\tvariants: {\r\n\t\t\tvariant: {\r\n\t\t\t\tsimple:\r\n\t\t\t\t\t\"bg-secondary relative z-10 bg-transparent hover:border-secondary hover:bg-secondary/50  border border-transparent dark:text-white text-sm md:text-sm transition font-medium duration-200  rounded-md px-4 py-2  flex items-center justify-center\",\r\n\t\t\t\tdefault: \"bg-primary text-primary-foreground hover:bg-primary/90\",\r\n\t\t\t\tdestructive:\r\n\t\t\t\t\t\"bg-destructive text-destructive-foreground hover:bg-destructive/90\",\r\n\t\t\t\toutline:\r\n\t\t\t\t\t\"border border-input bg-background hover:bg-accent hover:text-accent-foreground\",\r\n\t\t\t\tsecondary:\r\n\t\t\t\t\t\"bg-secondary text-secondary-foreground hover:bg-secondary/80\",\r\n\t\t\t\tghost: \"hover:bg-accent hover:text-black hover:stroke-black dark:text-white text-black\",\r\n\t\t\t\tlink: \"text-primary underline-offset-4 hover:underline\",\r\n\t\t\t},\r\n\t\t\tsize: {\r\n\t\t\t\tdefault: \"h-10 px-4 py-2\",\r\n\t\t\t\tsm: \"h-9 rounded-md px-3\",\r\n\t\t\t\tlg: \"h-11 rounded-md px-8\",\r\n\t\t\t\ticon: \"h-10 w-10\",\r\n\t\t\t},\r\n\t\t},\r\n\t\tdefaultVariants: {\r\n\t\t\tvariant: \"default\",\r\n\t\t\tsize: \"default\",\r\n\t\t},\r\n\t}\r\n);\r\n\r\nexport interface ButtonProps\r\n\textends React.ButtonHTMLAttributes<HTMLButtonElement>,\r\n\t\tVariantProps<typeof buttonVariants> {\r\n\tasChild?: boolean;\r\n}\r\n\r\nconst Button = React.forwardRef<HTMLButtonElement, ButtonProps>(\r\n\t({ className, variant, size, asChild = false, ...props }, ref) => {\r\n\t\tconst Comp = asChild ? Slot : \"button\";\r\n\t\treturn (\r\n\t\t\t<Comp\r\n\t\t\t\tclassName={cn(buttonVariants({ variant, size, className }))}\r\n\t\t\t\tref={ref}\r\n\t\t\t\t{...props}\r\n\t\t\t/>\r\n\t\t);\r\n\t}\r\n);\r\nButton.displayName = \"Button\";\r\n\r\nexport { Button, buttonVariants };\r\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/utilities/cn.ts",
      "content": "import { ClassValue, clsx } from \"clsx\";\r\nimport { twMerge } from \"tailwind-merge\";\r\n\r\nexport function cn(...inputs: ClassValue[]) {\r\n\treturn twMerge(clsx(inputs));\r\n}\r\n",
      "type": "registry:ui"
    },
    {
      "path": "components/ui/input.tsx",
      "content": "// SHADCN UI GENERATED CODE\n\nimport React from \"react\";\n\nimport { cn } from \"@/registry/utilities/cn\";\n\nexport interface InputProps\n\textends React.InputHTMLAttributes<HTMLInputElement> {}\n\nconst Input = React.forwardRef<HTMLInputElement, InputProps>(\n\t({ className, type, ...props }, ref) => {\n\t\treturn (\n\t\t\t<input\n\t\t\t\ttype={type}\n\t\t\t\tclassName={cn(\n\t\t\t\t\t\"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50\",\n\t\t\t\t\tclassName\n\t\t\t\t)}\n\t\t\t\tref={ref}\n\t\t\t\t{...props}\n\t\t\t/>\n\t\t);\n\t}\n);\nInput.displayName = \"Input\";\n\nexport { Input };\n",
      "type": "registry:ui"
    },
    {
      "path": "components/ui/label.tsx",
      "content": "\"use client\";\n\nimport React from \"react\";\n\nimport { cn } from \"@/registry/utilities/cn\";\nimport * as LabelPrimitive from \"@radix-ui/react-label\";\nimport { cva, type VariantProps } from \"class-variance-authority\";\n\nconst labelVariants = cva(\n\t\"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70\"\n);\n\nconst Label = React.forwardRef<\n\tReact.ElementRef<typeof LabelPrimitive.Root>,\n\tReact.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> &\n\t\tVariantProps<typeof labelVariants>\n>(({ className, ...props }, ref) => (\n\t<LabelPrimitive.Root\n\t\tref={ref}\n\t\tclassName={cn(labelVariants(), className)}\n\t\t{...props}\n\t/>\n));\nLabel.displayName = LabelPrimitive.Root.displayName;\n\nexport { Label };\n",
      "type": "registry:ui"
    },
    {
      "path": "components/ui/textarea.tsx",
      "content": "// SHADCN UI GENERATED CODE\n\nimport React from \"react\";\n\nimport { cn } from \"@/registry/utilities/cn\";\n\nexport interface TextareaProps\n\textends React.TextareaHTMLAttributes<HTMLTextAreaElement> {}\n\nconst Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(\n\t({ className, ...props }, ref) => {\n\t\treturn (\n\t\t\t<textarea\n\t\t\t\tclassName={cn(\n\t\t\t\t\t\"flex min-h-[80px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50\",\n\t\t\t\t\tclassName\n\t\t\t\t)}\n\t\t\t\tref={ref}\n\t\t\t\t{...props}\n\t\t\t/>\n\t\t);\n\t}\n);\nTextarea.displayName = \"Textarea\";\n\nexport { Textarea };\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/utilities/useCharacterLimit.ts",
      "content": "\"use client\"\r\n\r\nimport { ChangeEvent, useState } from \"react\"\r\n\r\ntype UseCharacterLimitProps = {\r\n    maxLength: number\r\n    initialValue?: string\r\n}\r\n\r\nexport function useCharacterLimit({\r\n    maxLength,\r\n    initialValue = \"\",\r\n}: UseCharacterLimitProps) {\r\n    const [value, setValue] = useState(initialValue)\r\n    const [characterCount, setCharacterCount] = useState(initialValue.length)\r\n\r\n    const handleChange = (\r\n        e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>\r\n    ) => {\r\n        const newValue = e.target.value\r\n        if (newValue.length <= maxLength) {\r\n            setValue(newValue)\r\n            setCharacterCount(newValue.length)\r\n        }\r\n    }\r\n\r\n    return {\r\n        value,\r\n        characterCount,\r\n        handleChange,\r\n        maxLength,\r\n    }\r\n}",
      "type": "registry:ui"
    },
    {
      "path": "registry/utilities/usefileUpload.ts",
      "content": "\"use client\"\r\n\r\nimport type React from \"react\"\r\nimport {\r\n    useCallback,\r\n    useRef,\r\n    useState,\r\n    type ChangeEvent,\r\n    type DragEvent,\r\n    type InputHTMLAttributes,\r\n} from \"react\"\r\n\r\nexport type FileMetadata = {\r\n    name: string\r\n    size: number\r\n    type: string\r\n    url: string\r\n    id: string\r\n}\r\n\r\nexport type FileWithPreview = {\r\n    file: File | FileMetadata\r\n    id: string\r\n    preview?: string\r\n}\r\n\r\nexport type FileUploadOptions = {\r\n    maxFiles?: number // Only used when multiple is true, defaults to Infinity\r\n    maxSize?: number // in bytes\r\n    accept?: string\r\n    multiple?: boolean // Defaults to false\r\n    initialFiles?: FileMetadata[]\r\n    onFilesChange?: (files: FileWithPreview[]) => void // Callback when files change\r\n    onFilesAdded?: (addedFiles: FileWithPreview[]) => void // Callback when new files are added\r\n}\r\n\r\nexport type FileUploadState = {\r\n    files: FileWithPreview[]\r\n    isDragging: boolean\r\n    errors: string[]\r\n}\r\n\r\nexport type FileUploadActions = {\r\n    addFiles: (files: FileList | File[]) => void\r\n    removeFile: (id: string) => void\r\n    clearFiles: () => void\r\n    clearErrors: () => void\r\n    handleDragEnter: (e: DragEvent<HTMLElement>) => void\r\n    handleDragLeave: (e: DragEvent<HTMLElement>) => void\r\n    handleDragOver: (e: DragEvent<HTMLElement>) => void\r\n    handleDrop: (e: DragEvent<HTMLElement>) => void\r\n    handleFileChange: (e: ChangeEvent<HTMLInputElement>) => void\r\n    openFileDialog: () => void\r\n    getInputProps: (\r\n        props?: InputHTMLAttributes<HTMLInputElement>\r\n    ) => InputHTMLAttributes<HTMLInputElement> & {\r\n        ref: React.Ref<HTMLInputElement>\r\n    }\r\n}\r\n\r\nexport const useFileUpload = (\r\n    options: FileUploadOptions = {}\r\n): [FileUploadState, FileUploadActions] => {\r\n    const {\r\n        maxFiles = Infinity,\r\n        maxSize = Infinity,\r\n        accept = \"*\",\r\n        multiple = false,\r\n        initialFiles = [],\r\n        onFilesChange,\r\n        onFilesAdded,\r\n    } = options\r\n\r\n    const [state, setState] = useState<FileUploadState>({\r\n        files: initialFiles.map((file) => ({\r\n            file,\r\n            id: file.id,\r\n            preview: file.url,\r\n        })),\r\n        isDragging: false,\r\n        errors: [],\r\n    })\r\n\r\n    const inputRef = useRef<HTMLInputElement>(null)\r\n\r\n    const validateFile = useCallback(\r\n        (file: File | FileMetadata): string | null => {\r\n            if (file instanceof File) {\r\n                if (file.size > maxSize) {\r\n                    return `File \"${file.name}\" exceeds the maximum size of ${formatBytes(maxSize)}.`\r\n                }\r\n            } else {\r\n                if (file.size > maxSize) {\r\n                    return `File \"${file.name}\" exceeds the maximum size of ${formatBytes(maxSize)}.`\r\n                }\r\n            }\r\n\r\n            if (accept !== \"*\") {\r\n                const acceptedTypes = accept.split(\",\").map((type) => type.trim())\r\n                const fileType = file instanceof File ? file.type || \"\" : file.type\r\n                const fileExtension = `.${file instanceof File ? file.name.split(\".\").pop() : file.name.split(\".\").pop()}`\r\n\r\n                const isAccepted = acceptedTypes.some((type) => {\r\n                    if (type.startsWith(\".\")) {\r\n                        return fileExtension.toLowerCase() === type.toLowerCase()\r\n                    }\r\n                    if (type.endsWith(\"/*\")) {\r\n                        const baseType = type.split(\"/\")[0]\r\n                        return fileType.startsWith(`${baseType}/`)\r\n                    }\r\n                    return fileType === type\r\n                })\r\n\r\n                if (!isAccepted) {\r\n                    return `File \"${file instanceof File ? file.name : file.name}\" is not an accepted file type.`\r\n                }\r\n            }\r\n\r\n            return null\r\n        },\r\n        [accept, maxSize]\r\n    )\r\n\r\n    const createPreview = useCallback(\r\n        (file: File | FileMetadata): string | undefined => {\r\n            if (file instanceof File) {\r\n                return URL.createObjectURL(file)\r\n            }\r\n            return file.url\r\n        },\r\n        []\r\n    )\r\n\r\n    const generateUniqueId = useCallback((file: File | FileMetadata): string => {\r\n        if (file instanceof File) {\r\n            return `${file.name}-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`\r\n        }\r\n        return file.id\r\n    }, [])\r\n\r\n    const clearFiles = useCallback(() => {\r\n        setState((prev) => {\r\n            // Clean up object URLs\r\n            prev.files.forEach((file) => {\r\n                if (\r\n                    file.preview &&\r\n                    file.file instanceof File &&\r\n                    file.file.type.startsWith(\"image/\")\r\n                ) {\r\n                    URL.revokeObjectURL(file.preview)\r\n                }\r\n            })\r\n\r\n            if (inputRef.current) {\r\n                inputRef.current.value = \"\"\r\n            }\r\n\r\n            const newState = {\r\n                ...prev,\r\n                files: [],\r\n                errors: [],\r\n            }\r\n\r\n            onFilesChange?.(newState.files)\r\n            return newState\r\n        })\r\n    }, [onFilesChange])\r\n\r\n    const addFiles = useCallback(\r\n        (newFiles: FileList | File[]) => {\r\n            if (!newFiles || newFiles.length === 0) return\r\n\r\n            const newFilesArray = Array.from(newFiles)\r\n            const errors: string[] = []\r\n\r\n            // Clear existing errors when new files are uploaded\r\n            setState((prev) => ({ ...prev, errors: [] }))\r\n\r\n            // In single file mode, clear existing files first\r\n            if (!multiple) {\r\n                clearFiles()\r\n            }\r\n\r\n            // Check if adding these files would exceed maxFiles (only in multiple mode)\r\n            if (\r\n                multiple &&\r\n                maxFiles !== Infinity &&\r\n                state.files.length + newFilesArray.length > maxFiles\r\n            ) {\r\n                errors.push(`You can only upload a maximum of ${maxFiles} files.`)\r\n                setState((prev) => ({ ...prev, errors }))\r\n                return\r\n            }\r\n\r\n            const validFiles: FileWithPreview[] = []\r\n\r\n            newFilesArray.forEach((file) => {\r\n                // Only check for duplicates if multiple files are allowed\r\n                if (multiple) {\r\n                    const isDuplicate = state.files.some(\r\n                        (existingFile) =>\r\n                            existingFile.file.name === file.name &&\r\n                            existingFile.file.size === file.size\r\n                    )\r\n\r\n                    // Skip duplicate files silently\r\n                    if (isDuplicate) {\r\n                        return\r\n                    }\r\n                }\r\n\r\n                // Check file size\r\n                if (file.size > maxSize) {\r\n                    errors.push(\r\n                        multiple\r\n                            ? `Some files exceed the maximum size of ${formatBytes(maxSize)}.`\r\n                            : `File exceeds the maximum size of ${formatBytes(maxSize)}.`\r\n                    )\r\n                    return\r\n                }\r\n\r\n                const error = validateFile(file)\r\n                if (error) {\r\n                    errors.push(error)\r\n                } else {\r\n                    validFiles.push({\r\n                        file,\r\n                        id: generateUniqueId(file),\r\n                        preview: createPreview(file),\r\n                    })\r\n                }\r\n            })\r\n\r\n            // Only update state if we have valid files to add\r\n            if (validFiles.length > 0) {\r\n                // Call the onFilesAdded callback with the newly added valid files\r\n                onFilesAdded?.(validFiles)\r\n\r\n                setState((prev) => {\r\n                    const newFiles = !multiple\r\n                        ? validFiles\r\n                        : [...prev.files, ...validFiles]\r\n                    onFilesChange?.(newFiles)\r\n                    return {\r\n                        ...prev,\r\n                        files: newFiles,\r\n                        errors,\r\n                    }\r\n                })\r\n            } else if (errors.length > 0) {\r\n                setState((prev) => ({\r\n                    ...prev,\r\n                    errors,\r\n                }))\r\n            }\r\n\r\n            // Reset input value after handling files\r\n            if (inputRef.current) {\r\n                inputRef.current.value = \"\"\r\n            }\r\n        },\r\n        [\r\n            state.files,\r\n            maxFiles,\r\n            multiple,\r\n            maxSize,\r\n            validateFile,\r\n            createPreview,\r\n            generateUniqueId,\r\n            clearFiles,\r\n            onFilesChange,\r\n            onFilesAdded,\r\n        ]\r\n    )\r\n\r\n    const removeFile = useCallback(\r\n        (id: string) => {\r\n            setState((prev) => {\r\n                const fileToRemove = prev.files.find((file) => file.id === id)\r\n                if (\r\n                    fileToRemove &&\r\n                    fileToRemove.preview &&\r\n                    fileToRemove.file instanceof File &&\r\n                    fileToRemove.file.type.startsWith(\"image/\")\r\n                ) {\r\n                    URL.revokeObjectURL(fileToRemove.preview)\r\n                }\r\n\r\n                const newFiles = prev.files.filter((file) => file.id !== id)\r\n                onFilesChange?.(newFiles)\r\n\r\n                return {\r\n                    ...prev,\r\n                    files: newFiles,\r\n                    errors: [],\r\n                }\r\n            })\r\n        },\r\n        [onFilesChange]\r\n    )\r\n\r\n    const clearErrors = useCallback(() => {\r\n        setState((prev) => ({\r\n            ...prev,\r\n            errors: [],\r\n        }))\r\n    }, [])\r\n\r\n    const handleDragEnter = useCallback((e: DragEvent<HTMLElement>) => {\r\n        e.preventDefault()\r\n        e.stopPropagation()\r\n        setState((prev) => ({ ...prev, isDragging: true }))\r\n    }, [])\r\n\r\n    const handleDragLeave = useCallback((e: DragEvent<HTMLElement>) => {\r\n        e.preventDefault()\r\n        e.stopPropagation()\r\n\r\n        if (e.currentTarget.contains(e.relatedTarget as Node)) {\r\n            return\r\n        }\r\n\r\n        setState((prev) => ({ ...prev, isDragging: false }))\r\n    }, [])\r\n\r\n    const handleDragOver = useCallback((e: DragEvent<HTMLElement>) => {\r\n        e.preventDefault()\r\n        e.stopPropagation()\r\n    }, [])\r\n\r\n    const handleDrop = useCallback(\r\n        (e: DragEvent<HTMLElement>) => {\r\n            e.preventDefault()\r\n            e.stopPropagation()\r\n            setState((prev) => ({ ...prev, isDragging: false }))\r\n\r\n            // Don't process files if the input is disabled\r\n            if (inputRef.current?.disabled) {\r\n                return\r\n            }\r\n\r\n            if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {\r\n                // In single file mode, only use the first file\r\n                if (!multiple) {\r\n                    const file = e.dataTransfer.files[0]\r\n                    addFiles([file])\r\n                } else {\r\n                    addFiles(e.dataTransfer.files)\r\n                }\r\n            }\r\n        },\r\n        [addFiles, multiple]\r\n    )\r\n\r\n    const handleFileChange = useCallback(\r\n        (e: ChangeEvent<HTMLInputElement>) => {\r\n            if (e.target.files && e.target.files.length > 0) {\r\n                addFiles(e.target.files)\r\n            }\r\n        },\r\n        [addFiles]\r\n    )\r\n\r\n    const openFileDialog = useCallback(() => {\r\n        if (inputRef.current) {\r\n            inputRef.current.click()\r\n        }\r\n    }, [])\r\n\r\n    const getInputProps = useCallback(\r\n        (props: InputHTMLAttributes<HTMLInputElement> = {}) => {\r\n            return {\r\n                ...props,\r\n                type: \"file\" as const,\r\n                onChange: handleFileChange,\r\n                accept: props.accept || accept,\r\n                multiple: props.multiple !== undefined ? props.multiple : multiple,\r\n                ref: inputRef,\r\n            }\r\n        },\r\n        [accept, multiple, handleFileChange]\r\n    )\r\n\r\n    return [\r\n        state,\r\n        {\r\n            addFiles,\r\n            removeFile,\r\n            clearFiles,\r\n            clearErrors,\r\n            handleDragEnter,\r\n            handleDragLeave,\r\n            handleDragOver,\r\n            handleDrop,\r\n            handleFileChange,\r\n            openFileDialog,\r\n            getInputProps,\r\n        },\r\n    ]\r\n}\r\n\r\n// Helper function to format bytes to human-readable format\r\nexport const formatBytes = (bytes: number, decimals = 2): string => {\r\n    if (bytes === 0) return \"0 Bytes\"\r\n\r\n    const k = 1024\r\n    const dm = decimals < 0 ? 0 : decimals\r\n    const sizes = [\"Bytes\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\r\n\r\n    const i = Math.floor(Math.log(bytes) / Math.log(k))\r\n\r\n    return Number.parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + sizes[i]\r\n}",
      "type": "registry:ui"
    }
  ]
}