Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(chat): eliminate Skeleton flash on new-chat first message (#2518)
Naiyuan Qing81b62fc8d341
diff --git a/packages/views/chat/components/chat-window.tsx b/packages/views/chat/components/chat-window.tsxindex fcc753f326..945dab2317 100644--- a/packages/views/chat/components/chat-window.tsx+++ b/packages/views/chat/components/chat-window.tsx@@ -210,6 +210,12 @@ export function ChatWindow() {// session-dropdown's existing localized `window.untitled` fallback kicks// in. A follow-up task may back-fill the real title from the first user// message — until then this keeps the session list scannable across locales.+ //+ // NOTE: ensureSession does NOT flip `activeSessionId` itself. Callers must+ // seed `chatKeys.messages(sessionId)` in the Query cache BEFORE calling+ // `setActiveSession(sessionId)`, otherwise the first useQuery subscription+ // for the new key reports `isLoading: true` and renders ChatMessageSkeleton+ // for one frame (the "new-chat first-message" white flash).const sessionPromiseRef = useRef<Promise<string | null> | null>(null);const ensureSession = useCallback(async (titleSeed: string): Promise<string | null> => {@@ -223,7 +229,6 @@ export function ChatWindow() {agent_id: activeAgent.id,title: titleSeed.slice(0, 50),});- setActiveSession(session.id);return session.id;} finally {sessionPromiseRef.current = null;@@ -232,16 +237,25 @@ export function ChatWindow() {sessionPromiseRef.current = promise;return promise;},- [activeSessionId, activeAgent, createSession, setActiveSession],+ [activeSessionId, activeAgent, createSession],);const handleUploadFile = useCallback(async (file: File) => {const sessionId = await ensureSession("");if (!sessionId) return null;+ // Prime the messages cache as empty before flipping activeSessionId so+ // ChatMessageList mounts directly (no Skeleton frame). Skip the write+ // when an entry already exists — a concurrent handleSend may have+ // seeded an optimistic message we must not clobber.+ qc.setQueryData<ChatMessage[]>(+ chatKeys.messages(sessionId),+ (old) => old ?? [],+ );+ setActiveSession(sessionId);return uploadWithToast(file, { chatSessionId: sessionId });},- [ensureSession, uploadWithToast],+ [ensureSession, uploadWithToast, qc, setActiveSession],);const handleSend = useCallback(@@ -287,6 +301,12 @@ export function ChatWindow() {task_id: null,created_at: sentAt,};+ // Seed cache BEFORE flipping activeSessionId. If we set the active+ // session first, useQuery's first subscription to the new key sees no+ // cached data and renders ChatMessageSkeleton for one frame — the+ // "new-chat first-message" white flash. Priming the cache first means+ // the very first read after activeSessionId flips hits data+ // synchronously and ChatMessageList mounts directly.qc.setQueryData<ChatMessage[]>(chatKeys.messages(sessionId),(old) => (old ? [...old, optimistic] : [optimistic]),@@ -301,6 +321,9 @@ export function ChatWindow() {status: "queued",created_at: sentAt,});+ // Cache primed → safe to publish the new active session. Idempotent+ // when the session was already active (existing-conversation send).+ setActiveSession(sessionId);apiLogger.debug("sendChatMessage.optimistic", { sessionId, optimisticId: optimistic.id });const result = await api.sendChatMessage(sessionId, finalContent, attachmentIds);@@ -325,6 +348,7 @@ export function ChatWindow() {anchorCandidate,ensureSession,qc,+ setActiveSession,],);