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";
import { useShape } from "@electric-sql/react";
import { useSetAtom } from "jotai";
import { useEffect } from "react";
import { chatSessionStateAtom } from "@/atoms/chat/chat-session-state.atom";
import type { ChatSessionState } from "@/contracts/types/chat-session-state.types";
const ELECTRIC_URL = process.env.NEXT_PUBLIC_ELECTRIC_URL || "http://localhost:5133";
import { queries } from "@/zero/queries";
import { useQuery } from "@rocicorp/zero/react";
/**
* 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.
*/
export function useChatSessionStateSync(threadId: number | null) {
const setSessionState = useSetAtom(chatSessionStateAtom);
const { data } = useShape<ChatSessionState>({
url: `${ELECTRIC_URL}/v1/shape`,
params: {
table: "chat_session_state",
where: `thread_id = ${threadId ?? -1}`,
},
});
const [row] = useQuery(queries.chatSession.byThread({ threadId: threadId ?? -1 }));
useEffect(() => {
if (!threadId) {
@ -29,11 +21,10 @@ export function useChatSessionStateSync(threadId: number | null) {
return;
}
const row = data?.[0];
setSessionState({
threadId,
isAiResponding: !!row?.ai_responding_to_user_id,
respondingToUserId: row?.ai_responding_to_user_id ?? null,
isAiResponding: !!row?.aiRespondingToUserId,
respondingToUserId: row?.aiRespondingToUserId ?? null,
});
}, [threadId, data, setSessionState]);
}, [threadId, row, setSessionState]);
}