UNPKG

@assistant-ui/react

Version:

React components for AI chat.

59 lines (58 loc) 1.65 kB
"use client"; // src/context/providers/TextContentPartProvider.tsx import { useEffect, useState } from "react"; import { create } from "zustand"; import { ContentPartContext } from "../react/ContentPartContext.mjs"; import { writableStore } from "../ReadonlyStore.mjs"; import { ContentPartRuntimeImpl } from "../../api/ContentPartRuntime.mjs"; import { jsx } from "react/jsx-runtime"; var COMPLETE_STATUS = { type: "complete" }; var RUNNING_STATUS = { type: "running" }; var TextContentPartProvider = ({ children, text, isRunning }) => { const [context] = useState(() => { const useContentPartRuntime = create( // TODO () => new ContentPartRuntimeImpl(null, null, null) ); const useContentPart = create(() => ({ status: isRunning ? RUNNING_STATUS : COMPLETE_STATUS, part: { type: "text", text }, type: "text", text })); return { useContentPartRuntime, useContentPart }; }); useEffect(() => { const state = context.useContentPart.getState(); const textUpdated = state.text !== text; const targetStatus = isRunning ? RUNNING_STATUS : COMPLETE_STATUS; const statusUpdated = state.status !== targetStatus; if (!textUpdated && !statusUpdated) return; writableStore(context.useContentPart).setState( { type: "text", text, part: { type: "text", text }, status: targetStatus }, true ); }, [context, isRunning, text]); return /* @__PURE__ */ jsx(ContentPartContext.Provider, { value: context, children }); }; export { TextContentPartProvider }; //# sourceMappingURL=TextContentPartProvider.mjs.map