mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-10 00:02:40 +02:00
Refactor chat session state hook to use useShape
This commit is contained in:
parent
6a31e79ede
commit
69f2460d18
2 changed files with 28 additions and 1 deletions
28
surfsense_web/hooks/use-chat-session-state.ts
Normal file
28
surfsense_web/hooks/use-chat-session-state.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useShape } from "@electric-sql/react";
|
||||||
|
import type { ChatSessionState } from "@/contracts/types/chat-session-state.types";
|
||||||
|
|
||||||
|
const ELECTRIC_URL = process.env.NEXT_PUBLIC_ELECTRIC_URL || "http://localhost:5133";
|
||||||
|
|
||||||
|
export function useChatSessionState(threadId: number | null) {
|
||||||
|
const { data, isLoading, isError, error } = useShape<ChatSessionState>({
|
||||||
|
url: `${ELECTRIC_URL}/v1/shape`,
|
||||||
|
params: {
|
||||||
|
table: "chat_session_state",
|
||||||
|
where: `thread_id = ${threadId}`,
|
||||||
|
},
|
||||||
|
// Skip fetching if no threadId
|
||||||
|
...(threadId ? {} : { url: undefined as unknown as string }),
|
||||||
|
});
|
||||||
|
|
||||||
|
const sessionState = data?.[0] ?? null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
sessionState,
|
||||||
|
isAiResponding: !!sessionState?.ai_responding_to_user_id,
|
||||||
|
respondingToUserId: sessionState?.ai_responding_to_user_id ?? null,
|
||||||
|
loading: isLoading,
|
||||||
|
error: isError ? error : null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -228,7 +228,6 @@ export async function initElectric(userId: string): Promise<ElectricClient> {
|
||||||
CREATE INDEX IF NOT EXISTS idx_documents_search_space_type ON documents(search_space_id, document_type);
|
CREATE INDEX IF NOT EXISTS idx_documents_search_space_type ON documents(search_space_id, document_type);
|
||||||
`);
|
`);
|
||||||
|
|
||||||
// Create the chat_comment_mentions table schema in PGlite
|
|
||||||
await db.exec(`
|
await db.exec(`
|
||||||
CREATE TABLE IF NOT EXISTS chat_comment_mentions (
|
CREATE TABLE IF NOT EXISTS chat_comment_mentions (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue