UNPKG

@assistant-ui/react

Version:

React components for AI chat.

48 lines (47 loc) 1.44 kB
"use client"; // src/context/providers/AttachmentRuntimeProvider.tsx import { useEffect, useMemo, useState } from "react"; import { create } from "zustand"; import { AttachmentContext } from "../react/AttachmentContext.mjs"; import { writableStore } from "../ReadonlyStore.mjs"; import { jsx } from "react/jsx-runtime"; var useAttachmentRuntimeStore = (runtime) => { const [store] = useState(() => create(() => runtime)); useEffect(() => { writableStore(store).setState(runtime, true); }, [runtime, store]); return store; }; var useAttachmentStore = (runtime) => { const [store] = useState(() => create(() => runtime.getState())); useEffect(() => { const updateState = () => writableStore(store).setState(runtime.getState(), true); updateState(); return runtime.subscribe(updateState); }, [runtime, store]); return store; }; var AttachmentRuntimeProvider = ({ runtime, children }) => { const useAttachmentRuntime = useAttachmentRuntimeStore(runtime); const useAttachment = useAttachmentStore(runtime); const source = useAttachment((s) => s.source); const context = useMemo(() => { return { source, useAttachmentRuntime, useAttachment }; }, [useAttachmentRuntime, useAttachment]); return /* @__PURE__ */ jsx(AttachmentContext.Provider, { value: context, children }); }; export { AttachmentRuntimeProvider }; //# sourceMappingURL=AttachmentRuntimeProvider.mjs.map