mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-02 19:55:18 +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";
|
"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]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue