UNPKG

@assistant-ui/react

Version:

TypeScript/React library for AI Chat

57 lines 2.11 kB
"use client"; import { jsx as _jsx } from "react/jsx-runtime"; import { forwardRef, useCallback, useState } from "react"; import { Slot } from "@radix-ui/react-slot"; import { useAssistantApi } from "../../context/index.js"; export const ComposerPrimitiveAttachmentDropzone = forwardRef(({ disabled, asChild = false, children, ...rest }, ref) => { const [isDragging, setIsDragging] = useState(false); const api = useAssistantApi(); const handleDragEnterCapture = useCallback((e) => { if (disabled) return; e.preventDefault(); setIsDragging(true); }, [disabled]); const handleDragOverCapture = useCallback((e) => { if (disabled) return; e.preventDefault(); if (!isDragging) setIsDragging(true); }, [disabled, isDragging]); const handleDragLeaveCapture = useCallback((e) => { if (disabled) return; e.preventDefault(); const next = e.relatedTarget; if (next && e.currentTarget.contains(next)) { return; } setIsDragging(false); }, [disabled]); const handleDrop = useCallback(async (e) => { if (disabled) return; e.preventDefault(); setIsDragging(false); for (const file of e.dataTransfer.files) { try { await api.composer().addAttachment(file); } catch (error) { console.error("Failed to add attachment:", error); } } }, [disabled, api]); const dragProps = { onDragEnterCapture: handleDragEnterCapture, onDragOverCapture: handleDragOverCapture, onDragLeaveCapture: handleDragLeaveCapture, onDropCapture: handleDrop, }; const Comp = asChild ? Slot : "div"; return (_jsx(Comp, { ...(isDragging ? { "data-dragging": "true" } : null), ref: ref, ...dragProps, ...rest, children: children })); }); ComposerPrimitiveAttachmentDropzone.displayName = "ComposerPrimitive.AttachmentDropzone"; //# sourceMappingURL=ComposerAttachmentDropzone.js.map