mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-31 19:45:15 +02:00
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:
parent
6ad5ead320
commit
b27061e44a
1 changed files with 7 additions and 16 deletions
|
|
@ -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]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue