diff --git a/surfsense_web/features/chat-artifacts/hooks/use-sync-chat-artifacts.ts b/surfsense_web/features/chat-artifacts/hooks/use-sync-chat-artifacts.ts new file mode 100644 index 000000000..e7991d846 --- /dev/null +++ b/surfsense_web/features/chat-artifacts/hooks/use-sync-chat-artifacts.ts @@ -0,0 +1,22 @@ +import type { ThreadMessageLike } from "@assistant-ui/react"; +import { useSetAtom } from "jotai"; +import { useEffect, useMemo } from "react"; +import { collectArtifacts } from "../lib/collect-artifacts"; +import { chatArtifactsAtom } from "../state/artifacts-panel.atom"; + +/** + * Keep `chatArtifactsAtom` in sync with the active thread's messages so the + * right-panel sidebar (rendered in the layout shell, outside the chat runtime) + * can read the deliverable list. Clears on unmount and on thread switch (a new + * `messages` array recomputes to the new thread's artifacts). + */ +export function useSyncChatArtifacts(messages: readonly ThreadMessageLike[]): void { + const setArtifacts = useSetAtom(chatArtifactsAtom); + const artifacts = useMemo(() => collectArtifacts(messages), [messages]); + + useEffect(() => { + setArtifacts(artifacts); + }, [artifacts, setArtifacts]); + + useEffect(() => () => setArtifacts([]), [setArtifacts]); +}