feat: rewrite use-chat-session-state hook from Electric to Zero

Replace @electric-sql/react useShape with @rocicorp/zero/react useQuery.
Same Jotai atom update, same logic, same consumer contract.
This commit is contained in:
CREDO23 2026-03-23 18:56:32 +02:00
parent 6ad5ead320
commit b27061e44a

View file

@ -1,27 +1,19 @@
"use client"; "use client";
import { useShape } from "@electric-sql/react";
import { useSetAtom } from "jotai"; import { useSetAtom } from "jotai";
import { useEffect } from "react"; import { useEffect } from "react";
import { chatSessionStateAtom } from "@/atoms/chat/chat-session-state.atom"; import { chatSessionStateAtom } from "@/atoms/chat/chat-session-state.atom";
import type { ChatSessionState } from "@/contracts/types/chat-session-state.types"; import { queries } from "@/zero/queries";
import { useQuery } from "@rocicorp/zero/react";
const ELECTRIC_URL = process.env.NEXT_PUBLIC_ELECTRIC_URL || "http://localhost:5133";
/** /**
* Syncs chat session state for a thread via Electric SQL. * Syncs chat session state for a thread via Zero.
* Call once per thread (in page.tsx). Updates global atom. * Call once per thread (in page.tsx). Updates global atom.
*/ */
export function useChatSessionStateSync(threadId: number | null) { export function useChatSessionStateSync(threadId: number | null) {
const setSessionState = useSetAtom(chatSessionStateAtom); const setSessionState = useSetAtom(chatSessionStateAtom);
const { data } = useShape<ChatSessionState>({ const [row] = useQuery(queries.chatSession.byThread({ threadId: threadId ?? -1 }));
url: `${ELECTRIC_URL}/v1/shape`,
params: {
table: "chat_session_state",
where: `thread_id = ${threadId ?? -1}`,
},
});
useEffect(() => { useEffect(() => {
if (!threadId) { if (!threadId) {
@ -29,11 +21,10 @@ export function useChatSessionStateSync(threadId: number | null) {
return; return;
} }
const row = data?.[0];
setSessionState({ setSessionState({
threadId, threadId,
isAiResponding: !!row?.ai_responding_to_user_id, isAiResponding: !!row?.aiRespondingToUserId,
respondingToUserId: row?.ai_responding_to_user_id ?? null, respondingToUserId: row?.aiRespondingToUserId ?? null,
}); });
}, [threadId, data, setSessionState]); }, [threadId, row, setSessionState]);
} }