Centralize chat session state sync with single subscription via atom

This commit is contained in:
CREDO23 2026-01-22 19:04:23 +02:00
parent 4cb835f19f
commit 39d434b00c
4 changed files with 48 additions and 18 deletions

View file

@ -50,7 +50,8 @@ import {
type MessageRecord,
type ThreadRecord,
} from "@/lib/chat/thread-persistence";
import { useChatSessionState } from "@/hooks/use-chat-session-state";
import { chatSessionStateAtom } from "@/atoms/chat/chat-session-state.atom";
import { useChatSessionStateSync } from "@/hooks/use-chat-session-state";
import { useMessagesElectric } from "@/hooks/use-messages-electric";
import {
trackChatCreated,
@ -260,8 +261,11 @@ export default function NewChatPage() {
// Get current user for author info in shared chats
const { data: currentUser } = useAtomValue(currentUserAtom);
// Live collaboration: sync messages from other users via Electric SQL
const { isAiResponding, respondingToUserId } = useChatSessionState(threadId);
// Live collaboration: sync session state and messages via Electric SQL
useChatSessionStateSync(threadId);
const sessionState = useAtomValue(chatSessionStateAtom);
const isAiResponding = sessionState?.isAiResponding ?? false;
const respondingToUserId = sessionState?.respondingToUserId ?? null;
const { data: membersData } = useAtomValue(membersAtom);
const handleElectricMessagesUpdate = useCallback(