UNPKG

@assistant-ui/react

Version:

Typescript/React library for AI Chat

58 lines (57 loc) 2.35 kB
"use client"; // src/primitives/message/MessageAttachments.tsx import { memo, useMemo } from "react"; import { useMessage, useMessageRuntime } from "../../context/index.mjs"; import { useMessageAttachment } 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 = useMessageAttachment((a) => getComponent(components, a)); if (!Component) return null; return /* @__PURE__ */ jsx(Component, {}); }; var MessageAttachmentImpl = ({ components, attachmentIndex }) => { const messageRuntime = useMessageRuntime(); const runtime = useMemo( () => messageRuntime.getAttachmentByIndex(attachmentIndex), [messageRuntime, attachmentIndex] ); return /* @__PURE__ */ jsx(AttachmentRuntimeProvider, { runtime, children: /* @__PURE__ */ jsx(AttachmentComponent, { components }) }); }; var MessageAttachment = memo( MessageAttachmentImpl, (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 MessagePrimitiveAttachments = ({ components }) => { const attachmentsCount = useMessage((message) => { if (message.role !== "user") return 0; return message.attachments.length; }); return Array.from({ length: attachmentsCount }, (_, index) => /* @__PURE__ */ jsx( MessageAttachment, { attachmentIndex: index, components }, index )); }; MessagePrimitiveAttachments.displayName = "MessagePrimitive.Attachments"; export { MessagePrimitiveAttachments }; //# sourceMappingURL=MessageAttachments.mjs.map