2026-01-20 18:26:58 +02:00
|
|
|
"use client";
|
|
|
|
|
|
2026-03-24 16:07:28 +02:00
|
|
|
import { useQuery } from "@rocicorp/zero/react";
|
2026-01-22 19:04:23 +02:00
|
|
|
import { useSetAtom } from "jotai";
|
|
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { chatSessionStateAtom } from "@/atoms/chat/chat-session-state.atom";
|
2026-03-23 18:56:32 +02:00
|
|
|
import { queries } from "@/zero/queries";
|
2026-01-20 18:26:58 +02:00
|
|
|
|
2026-01-20 19:48:28 +02:00
|
|
|
/**
|
2026-03-23 18:56:32 +02:00
|
|
|
* Syncs chat session state for a thread via Zero.
|
2026-01-22 19:04:23 +02:00
|
|
|
* Call once per thread (in page.tsx). Updates global atom.
|
2026-01-20 19:48:28 +02:00
|
|
|
*/
|
2026-01-22 19:04:23 +02:00
|
|
|
export function useChatSessionStateSync(threadId: number | null) {
|
|
|
|
|
const setSessionState = useSetAtom(chatSessionStateAtom);
|
|
|
|
|
|
2026-03-23 18:56:32 +02:00
|
|
|
const [row] = useQuery(queries.chatSession.byThread({ threadId: threadId ?? -1 }));
|
2026-01-20 18:26:58 +02:00
|
|
|
|
2026-01-22 19:04:23 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!threadId) {
|
|
|
|
|
setSessionState(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-20 18:26:58 +02:00
|
|
|
|
2026-01-22 19:04:23 +02:00
|
|
|
setSessionState({
|
|
|
|
|
threadId,
|
2026-03-23 18:56:32 +02:00
|
|
|
isAiResponding: !!row?.aiRespondingToUserId,
|
|
|
|
|
respondingToUserId: row?.aiRespondingToUserId ?? null,
|
2026-01-22 19:04:23 +02:00
|
|
|
});
|
2026-03-23 18:56:32 +02:00
|
|
|
}, [threadId, row, setSessionState]);
|
2026-01-20 18:26:58 +02:00
|
|
|
}
|