From 87224b0239693269a9df607258d1923177955420 Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Mon, 22 Jun 2026 22:36:26 +0200 Subject: [PATCH] feat: sync thread artifacts to panel state --- .../hooks/use-sync-chat-artifacts.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 surfsense_web/features/chat-artifacts/hooks/use-sync-chat-artifacts.ts 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]); +}