@assistant-ui/react
Version:
Typescript/React library for AI Chat
57 lines (56 loc) • 2.31 kB
JavaScript
"use client";
// src/primitives/composer/ComposerAttachments.tsx
import { memo, useMemo } from "react";
import { useComposer, useComposerRuntime } from "../../context/index.mjs";
import { useThreadComposerAttachment } from "../../context/react/AttachmentContext.mjs";
import { AttachmentRuntimeProvider } from "../../context/providers/AttachmentRuntimeProvider.mjs";
import { jsx } from "react/jsx-runtime";
var getComponent = (components, attachment) => {
const type = attachment.type;
switch (type) {
case "image":
return components?.Image ?? components?.Attachment;
case "document":
return components?.Document ?? components?.Attachment;
case "file":
return components?.File ?? components?.Attachment;
default:
const _exhaustiveCheck = type;
throw new Error(`Unknown attachment type: ${_exhaustiveCheck}`);
}
};
var AttachmentComponent = ({ components }) => {
const Component = useThreadComposerAttachment(
(a) => getComponent(components, a)
);
if (!Component) return null;
return /* @__PURE__ */ jsx(Component, {});
};
var ComposerAttachmentImpl = ({ components, attachmentIndex }) => {
const composerRuntime = useComposerRuntime();
const runtime = useMemo(
() => composerRuntime.getAttachmentByIndex(attachmentIndex),
[composerRuntime, attachmentIndex]
);
return /* @__PURE__ */ jsx(AttachmentRuntimeProvider, { runtime, children: /* @__PURE__ */ jsx(AttachmentComponent, { components }) });
};
var ComposerAttachment = memo(
ComposerAttachmentImpl,
(prev, next) => prev.attachmentIndex === next.attachmentIndex && prev.components?.Image === next.components?.Image && prev.components?.Document === next.components?.Document && prev.components?.File === next.components?.File && prev.components?.Attachment === next.components?.Attachment
);
var ComposerPrimitiveAttachments = ({ components }) => {
const attachmentsCount = useComposer((s) => s.attachments.length);
return Array.from({ length: attachmentsCount }, (_, index) => /* @__PURE__ */ jsx(
ComposerAttachment,
{
attachmentIndex: index,
components
},
index
));
};
ComposerPrimitiveAttachments.displayName = "ComposerPrimitive.Attachments";
export {
ComposerPrimitiveAttachments
};
//# sourceMappingURL=ComposerAttachments.mjs.map