feat: sync thread artifacts to panel state

This commit is contained in:
CREDO23 2026-06-22 22:36:26 +02:00
parent 64c3db60bd
commit 87224b0239

View file

@ -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]);
}