@assistant-ui/react
Version:
Typescript/React library for AI Chat
65 lines • 2.31 kB
JavaScript
"use client";
import { createContext } from "react";
import { createContextHook } from "./utils/createContextHook";
import { createStateHookForRuntime } from "./utils/createStateHookForRuntime";
const AttachmentContext = createContext(
null
);
const useAttachmentContext = createContextHook(
AttachmentContext,
"a ComposerPrimitive.Attachments or MessagePrimitive.Attachments component"
);
function useAttachmentRuntime(options) {
const attachmentRuntime = useAttachmentContext(options);
if (!attachmentRuntime) return null;
return attachmentRuntime.useAttachmentRuntime();
}
function useThreadComposerAttachmentRuntime(options) {
const attachmentRuntime = useAttachmentRuntime(options);
if (!attachmentRuntime) return null;
if (attachmentRuntime.source !== "thread-composer")
throw new Error(
"This component must be used within a thread's ComposerPrimitive.Attachments component."
);
return attachmentRuntime;
}
function useEditComposerAttachmentRuntime(options) {
const attachmentRuntime = useAttachmentRuntime(options);
if (!attachmentRuntime) return null;
if (attachmentRuntime.source !== "edit-composer")
throw new Error(
"This component must be used within a message's ComposerPrimitive.Attachments component."
);
return attachmentRuntime;
}
function useMessageAttachmentRuntime(options) {
const attachmentRuntime = useAttachmentRuntime(options);
if (!attachmentRuntime) return null;
if (attachmentRuntime.source !== "message")
throw new Error(
"This component must be used within a MessagePrimitive.Attachments component."
);
return attachmentRuntime;
}
const useAttachment = createStateHookForRuntime(useAttachmentRuntime);
const useThreadComposerAttachment = createStateHookForRuntime(
useThreadComposerAttachmentRuntime
);
const useEditComposerAttachment = createStateHookForRuntime(
useEditComposerAttachmentRuntime
);
const useMessageAttachment = createStateHookForRuntime(
useMessageAttachmentRuntime
);
export {
AttachmentContext,
useAttachment,
useAttachmentRuntime,
useEditComposerAttachment,
useEditComposerAttachmentRuntime,
useMessageAttachment,
useMessageAttachmentRuntime,
useThreadComposerAttachment,
useThreadComposerAttachmentRuntime
};
//# sourceMappingURL=AttachmentContext.js.map