UNPKG

@assistant-ui/react

Version:

Open-source TypeScript/React library for building production-grade AI chat experiences

76 lines (75 loc) • 2.59 kB
"use client"; import { createActionButton } from "../../utils/createActionButton.js"; import { c } from "@assistant-ui/tap/react-shim/compiler-runtime"; import { useComposerAddAttachment } from "@assistant-ui/core/react"; import { useInsertionEffect, useRef } from "@assistant-ui/tap/react-shim"; import { useAui } from "@assistant-ui/store"; // src/primitives/composer/ComposerAddAttachment.ts const useComposerAddAttachment$1 = (t0) => { const $ = c(10); let t1; if ($[0] !== t0) { t1 = t0 === void 0 ? {} : t0; $[0] = t0; $[1] = t1; } else t1 = $[1]; const { multiple: t2 } = t1; const multiple = t2 === void 0 ? true : t2; const { disabled, addAttachment } = useComposerAddAttachment(); const aui = useAui(); const scopeGenerationRef = useRef(0); let t3; if ($[2] === Symbol.for("react.memo_cache_sentinel")) { t3 = () => () => { scopeGenerationRef.current = scopeGenerationRef.current + 1; }; $[2] = t3; } else t3 = $[2]; let t4; if ($[3] !== aui.composer || $[4] !== aui.optional.threadListItem) { t4 = [aui.composer, aui.optional.threadListItem]; $[3] = aui.composer; $[4] = aui.optional.threadListItem; $[5] = t4; } else t4 = $[5]; useInsertionEffect(t3, t4); let t5; if ($[6] !== addAttachment || $[7] !== aui.composer || $[8] !== multiple) { t5 = () => { const scopeGeneration = scopeGenerationRef.current; const input = document.createElement("input"); input.type = "file"; input.multiple = multiple; input.hidden = true; const attachmentAccept = aui.composer.getState().attachmentAccept; if (attachmentAccept !== "*") input.accept = attachmentAccept; document.body.appendChild(input); input.onchange = async (e) => { const fileList = e.target.files; if (!fileList) return; const attachmentPromises = Array.from(fileList, async (file) => { try { if (scopeGenerationRef.current !== scopeGeneration) return; await addAttachment(file); } catch {} }); document.body.removeChild(input); await Promise.all(attachmentPromises); }; input.oncancel = () => { if (!input.files || input.files.length === 0) document.body.removeChild(input); }; input.click(); }; $[6] = addAttachment; $[7] = aui.composer; $[8] = multiple; $[9] = t5; } else t5 = $[9]; const callback = t5; if (disabled) return null; return callback; }; const ComposerPrimitiveAddAttachment = createActionButton("ComposerPrimitive.AddAttachment", useComposerAddAttachment$1, ["multiple"]); export { ComposerPrimitiveAddAttachment }; //# sourceMappingURL=ComposerAddAttachment.js.map