From 0fb577b71ff7075a0cafe770ee7e469f31f2d347 Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Mon, 13 Jul 2026 22:17:38 +0200 Subject: [PATCH] feat(chat): add useChatStream hook over the stream store --- .../lib/chat/stream-engine/use-chat-stream.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 surfsense_web/lib/chat/stream-engine/use-chat-stream.ts diff --git a/surfsense_web/lib/chat/stream-engine/use-chat-stream.ts b/surfsense_web/lib/chat/stream-engine/use-chat-stream.ts new file mode 100644 index 000000000..ccfb32481 --- /dev/null +++ b/surfsense_web/lib/chat/stream-engine/use-chat-stream.ts @@ -0,0 +1,18 @@ +"use client"; + +import { useCallback, useSyncExternalStore } from "react"; +import { chatStreamStore, type ThreadStreamState } from "./store"; + +/** + * Subscribe to the durable streaming state for a given thread. + * + * Returns the live ``ThreadStreamState`` while a turn is streaming (or + * pending re-hydration from the DB), or ``null`` when the thread has no + * active overlay — in which case the page falls back to its DB-hydrated + * messages. State lives in the module-level {@link chatStreamStore}, so it + * survives the chat page unmounting during in-app navigation. + */ +export function useChatStream(threadId: number | null): ThreadStreamState | null { + const getSnapshot = useCallback(() => chatStreamStore.getSnapshot(threadId), [threadId]); + return useSyncExternalStore(chatStreamStore.subscribe, getSnapshot, () => null); +}