@assistant-ui/react
Version:
React components for AI chat.
86 lines (85 loc) • 3.1 kB
JavaScript
"use client";
// src/context/react/AttachmentContext.ts
import { createContext, useContext } from "react";
import { createContextStoreHook } from "./utils/createContextStoreHook.mjs";
var AttachmentContext = createContext(
null
);
function useAttachmentContext(options) {
const context = useContext(AttachmentContext);
if (!options?.optional && !context)
throw new Error(
"This component must be used within a ComposerPrimitive.Attachments or MessagePrimitive.Attachments component."
);
return context;
}
function useThreadComposerAttachmentContext(options) {
const context = useAttachmentContext(options);
if (!context) return null;
if (context.source !== "thread-composer")
throw new Error(
"This component must be used within a thread's ComposerPrimitive.Attachments component."
);
return context;
}
function useEditComposerAttachmentContext(options) {
const context = useAttachmentContext(options);
if (!context) return null;
if (context.source !== "edit-composer")
throw new Error(
"This component must be used within a messages's ComposerPrimitive.Attachments component."
);
return context;
}
function useMessageAttachmentContext(options) {
const context = useAttachmentContext(options);
if (!context) return null;
if (context.source !== "message")
throw new Error(
"This component must be used within a MessagePrimitive.Attachments component."
);
return context;
}
function useAttachmentRuntime(options) {
const attachmentRuntime = useAttachmentContext(options);
if (!attachmentRuntime) return null;
return attachmentRuntime.useAttachmentRuntime();
}
function useThreadComposerAttachmentRuntime(options) {
const attachmentRuntime = useThreadComposerAttachmentContext(options);
if (!attachmentRuntime) return null;
return attachmentRuntime.useAttachmentRuntime();
}
function useEditComposerAttachmentRuntime(options) {
const attachmentRuntime = useEditComposerAttachmentContext(options);
if (!attachmentRuntime) return null;
return attachmentRuntime.useAttachmentRuntime();
}
function useMessageAttachmentRuntime(options) {
const attachmentRuntime = useMessageAttachmentContext(options);
if (!attachmentRuntime) return null;
return attachmentRuntime.useAttachmentRuntime();
}
var { useAttachment } = createContextStoreHook(
useAttachmentContext,
"useAttachment"
);
var { useAttachment: useThreadComposerAttachment } = createContextStoreHook(useThreadComposerAttachmentContext, "useAttachment");
var { useAttachment: useEditComposerAttachment } = createContextStoreHook(useEditComposerAttachmentContext, "useAttachment");
var { useAttachment: useMessageAttachment } = createContextStoreHook(
useMessageAttachmentContext,
"useAttachment"
);
export {
AttachmentContext,
useAttachment,
useAttachmentContext,
useAttachmentRuntime,
useEditComposerAttachment,
useEditComposerAttachmentRuntime,
useMessageAttachment,
useMessageAttachmentRuntime,
useThreadComposerAttachment,
useThreadComposerAttachmentRuntime
};
//# sourceMappingURL=AttachmentContext.mjs.map