{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chat-attachments",
  "title": "Chat Attachments",
  "description": "An AI chat composer with attachments via click, drag and paste.",
  "dependencies": [
    "@uploadcn/core@^0.1.0",
    "@uploadcn/react@^0.1.0",
    "cn"
  ],
  "registryDependencies": [
    "@uploadcn/upload"
  ],
  "files": [
    {
      "path": "registry/default/blocks/chat-attachments.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport {\n  type UploadAdapter,\n  type UploadItem as UploadItemData,\n  getUploadSummary,\n} from \"@uploadcn/core\"\nimport { compressImage } from \"@uploadcn/core/image\"\nimport {\n  Upload as UploadPrimitive,\n  useDropzone,\n  usePasteFiles,\n  useUploadSelector,\n  useUploader,\n} from \"@uploadcn/react\"\nimport { cn } from \"cn\"\n\nimport { IconPlaceholder } from \"@/components/icon-placeholder\"\nimport {\n  NativeButton,\n  UploadItem,\n  UploadItemActions,\n  UploadItemContent,\n  UploadItemDescription,\n  UploadItemMedia,\n  UploadItemProgress,\n  UploadItemRemove,\n  UploadItemRetry,\n  UploadItemTitle,\n  UploadList,\n  UploadTrigger,\n} from \"@/registry/default/ui/upload\"\n\n/** What most multimodal LLM APIs accept as attachments. */\nconst AI_ACCEPT = [\n  \"image/*\",\n  \"application/pdf\",\n  \"text/*\",\n  \".md\",\n  \".csv\",\n  \".json\",\n  \".docx\",\n]\n\nexport interface ChatMessage<TResult> {\n  text: string\n  attachments: UploadItemData<TResult>[]\n}\n\ninterface ChatAttachmentsProps<TResult> {\n  adapter?: UploadAdapter<TResult>\n  onSend: (message: ChatMessage<TResult>) => void | Promise<void>\n  placeholder?: string\n  maxFiles?: number\n  maxSize?: number\n  accept?: string[]\n  disabled?: boolean\n  className?: string\n}\n\n/** Images are downscaled for vision models: smaller uploads, same answers. */\nconst prepareForModel = (file: File) =>\n  compressImage(file, { maxWidth: 1568, maxHeight: 1568, type: \"image/webp\" })\n\n/**\n * A ChatGPT-style composer: attach by clicking, dragging onto the input or\n * pasting screenshots. Files upload while the user types; sending waits for\n * them to finish.\n */\nfunction ChatAttachments<TResult = unknown>({\n  adapter,\n  onSend,\n  placeholder = \"Ask anything, or attach files…\",\n  maxFiles = 10,\n  maxSize = 25 * 1000 * 1000,\n  accept = AI_ACCEPT,\n  disabled = false,\n  className,\n}: ChatAttachmentsProps<TResult>) {\n  const [text, setText] = React.useState(\"\")\n  const [isSending, setIsSending] = React.useState(false)\n  const formRef = React.useRef<HTMLFormElement>(null)\n  const inputId = React.useId()\n  const uploader = useUploader<TResult>({\n    adapter,\n    accept,\n    maxFiles,\n    maxSize,\n    transform: prepareForModel,\n  })\n  const summary = useUploadSelector(\n    uploader,\n    (state) => getUploadSummary(state.items),\n    (a, b) =>\n      a.total === b.total &&\n      a.isUploading === b.isUploading &&\n      a.counts.success === b.counts.success\n  )\n\n  const addFiles = React.useCallback(\n    (files: File[]) => void uploader.add(files),\n    [uploader]\n  )\n  const { isDragging, handlers } = useDropzone({\n    onDrop: addFiles,\n    accept,\n    disabled,\n  })\n  usePasteFiles({ onPaste: addFiles, target: formRef, enabled: !disabled })\n\n  const canSend =\n    !disabled &&\n    !isSending &&\n    !summary.isUploading &&\n    (text.trim().length > 0 || summary.counts.success > 0)\n\n  async function send(event?: React.FormEvent) {\n    event?.preventDefault()\n    if (!canSend) return\n    const attachments = uploader\n      .getState()\n      .items.filter((item) => item.status === \"success\")\n    setIsSending(true)\n    try {\n      await onSend({ text: text.trim(), attachments })\n      setText(\"\")\n      uploader.remove()\n    } finally {\n      setIsSending(false)\n    }\n  }\n\n  return (\n    <UploadPrimitive.Root\n      uploader={uploader}\n      disabled={disabled}\n      render={<div className={cn(\"w-full\", className)} />}\n    >\n      <form\n        ref={formRef}\n        onSubmit={send}\n        {...handlers}\n        data-dragging={isDragging || undefined}\n        className=\"relative flex flex-col gap-2 rounded-2xl border bg-background p-2 shadow-xs transition-[border-color,box-shadow] focus-within:border-ring focus-within:ring-3 focus-within:ring-ring/50 data-dragging:border-primary\"\n      >\n        {isDragging ? (\n          <div className=\"pointer-events-none absolute inset-0 z-10 flex animate-in items-center justify-center rounded-2xl bg-background/90 text-sm font-medium fade-in-0 motion-reduce:animate-none\">\n            Drop to attach\n          </div>\n        ) : null}\n        <UploadList\n          variant=\"inline\"\n          className=\"px-1 pt-1\"\n          aria-label=\"Attachments\"\n        >\n          <UploadItem variant=\"muted\" size=\"xs\" className=\"w-56 rounded-xl\">\n            <UploadItemMedia />\n            <UploadItemContent>\n              <UploadItemTitle />\n              <UploadItemDescription className=\"line-clamp-1\" />\n            </UploadItemContent>\n            <UploadItemActions>\n              <UploadItemRetry />\n              <UploadItemRemove />\n            </UploadItemActions>\n            <UploadItemProgress />\n          </UploadItem>\n        </UploadList>\n        <label htmlFor={inputId} className=\"sr-only\">\n          Message\n        </label>\n        <textarea\n          id={inputId}\n          value={text}\n          onChange={(event) => setText(event.target.value)}\n          onKeyDown={(event) => {\n            if (\n              event.key === \"Enter\" &&\n              !event.shiftKey &&\n              !event.nativeEvent.isComposing\n            ) {\n              event.preventDefault()\n              void send()\n            }\n          }}\n          rows={2}\n          placeholder={placeholder}\n          disabled={disabled}\n          className=\"field-sizing-content max-h-48 min-h-12 w-full resize-none bg-transparent px-2 py-1 text-sm outline-none placeholder:text-muted-foreground\"\n        />\n        <div className=\"flex items-center justify-between gap-2\">\n          <UploadTrigger\n            variant=\"ghost\"\n            size=\"icon-sm\"\n            aria-label=\"Attach files\"\n          >\n            <IconPlaceholder\n              lucide=\"PaperclipIcon\"\n              tabler=\"IconPaperclip\"\n              hugeicons=\"AttachmentIcon\"\n              phosphor=\"PaperclipIcon\"\n              remixicon=\"RiAttachmentLine\"\n            />\n          </UploadTrigger>\n          <div className=\"flex items-center gap-2\">\n            {summary.isUploading ? (\n              <span className=\"text-xs text-muted-foreground\">\n                Uploading attachments…\n              </span>\n            ) : null}\n            <NativeButton\n              type=\"submit\"\n              size=\"icon-sm\"\n              disabled={!canSend}\n              aria-label=\"Send message\"\n            >\n              <IconPlaceholder\n                lucide=\"ArrowUpIcon\"\n                tabler=\"IconArrowUp\"\n                hugeicons=\"ArrowUpIcon\"\n                phosphor=\"ArrowUpIcon\"\n                remixicon=\"RiArrowUpLine\"\n              />\n            </NativeButton>\n          </div>\n        </div>\n      </form>\n    </UploadPrimitive.Root>\n  )\n}\n\nexport { ChatAttachments, type ChatAttachmentsProps }\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "upload",
    "ai"
  ],
  "type": "registry:block"
}