From a5548f9aeb19193687eb81454436b79b5bd87a4d Mon Sep 17 00:00:00 2001 From: Antonio-Faure <209759495+Antonio-Faure@users.noreply.github.com> Date: Wed, 2 Dec 2026 11:54:04 +0100 Subject: [PATCH] fix(sidebar): clear the workspace cache on auth transitions The sidebar glyph cache is module-scope, so it survived a logout. On an authenticated deployment the next account's first paint could show the previous account's workspace name, and a stale cached error could render after the endpoint recovered. Move the cache into lib/chat-workspaces-cache.ts and reset it from invalidateAuthStatusCache(): login, logout and the cross-tab auth boundary all pass through that function. The hook stays the only reader and writer of the rows. Add tests for resetting the cache, for the empty remount after a reset, and for auth invalidation clearing the rows. --- web/hooks/useChatWorkspaces.ts | 27 +++++++------ web/lib/auth.ts | 4 ++ web/lib/chat-workspaces-cache.ts | 35 +++++++++++++++++ web/tests/chat-workspaces-cache.spec.tsx | 50 ++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 web/lib/chat-workspaces-cache.ts diff --git a/web/hooks/useChatWorkspaces.ts b/web/hooks/useChatWorkspaces.ts index 9b61a7ebf..62855b13d 100644 --- a/web/hooks/useChatWorkspaces.ts +++ b/web/hooks/useChatWorkspaces.ts @@ -2,20 +2,18 @@ import { useEffect, useState } from "react"; +import { + readCachedChatWorkspaces, + readCachedChatWorkspacesError, + writeCachedChatWorkspaces, + writeCachedChatWorkspacesError, +} from "@/lib/chat-workspaces-cache"; import { subscribeSessionChanges } from "@/lib/session-events"; import { listWorkspaces, type ChatWorkspaceRegistration, } from "@/lib/workspaces-api"; -// The sidebar mounts a fresh workspace menu when it collapses or expands. -// Starting that menu with an empty list makes the workspace glyph fall back -// to "Workspace unavailable" for one render, which flashes a different letter -// and tint. Keep the last successful rows at module scope so a remount paints -// the known workspace immediately and refreshes behind it. -let cachedWorkspaces: ChatWorkspaceRegistration[] | null = null; -let cachedError = ""; - /** * The workspaces a conversation can be filed under. * @@ -25,12 +23,16 @@ let cachedError = ""; * from three different surfaces (composer pill, sidebar row menu, chat header), * so every surface reloads on the same signal the session list uses instead of * each keeping its own idea of what exists. + * + * The last successful rows live in ``lib/chat-workspaces-cache`` so a sidebar + * remount paints the known workspace immediately instead of the fallback. That + * module knows nothing about React; auth transitions clear it on logout/login. */ export function useChatWorkspaces() { const [workspaces, setWorkspaces] = useState( - () => cachedWorkspaces ?? [], + readCachedChatWorkspaces, ); - const [error, setError] = useState(cachedError); + const [error, setError] = useState(readCachedChatWorkspacesError); useEffect(() => { let alive = true; @@ -38,15 +40,14 @@ export function useChatWorkspaces() { void listWorkspaces(true) .then((rows) => { const next = rows.filter((row) => row.kind === "workspace"); - cachedWorkspaces = next; - cachedError = ""; + writeCachedChatWorkspaces(next); if (!alive) return; setWorkspaces(next); setError(""); }) .catch((err) => { const message = err instanceof Error ? err.message : String(err); - cachedError = message; + writeCachedChatWorkspacesError(message); if (alive) setError(message); }); refresh(); diff --git a/web/lib/auth.ts b/web/lib/auth.ts index cf690b730..cdf0a3b13 100644 --- a/web/lib/auth.ts +++ b/web/lib/auth.ts @@ -1,4 +1,5 @@ import { invalidateClientCache } from "./client-cache"; +import { resetChatWorkspacesCache } from "./chat-workspaces-cache"; import { browserStorage } from "@/shared/storage"; import { apiFetch, apiUrl, setRuntimeAuthEnabled } from "@/lib/api"; import { updateRecoveryPrincipal } from "@/lib/recovery-principal"; @@ -44,6 +45,9 @@ let cachedAuthStatus: { value: AuthStatus | null; expiresAt: number } | null = null; export function invalidateAuthStatusCache(): void { + // Workspace names are account-scoped. A login, logout or cross-tab auth + // boundary must not let the next mount paint the previous account's rows. + resetChatWorkspacesCache(); invalidateClientCache("navigation:"); authGeneration += 1; authStatusRequest = null; diff --git a/web/lib/chat-workspaces-cache.ts b/web/lib/chat-workspaces-cache.ts new file mode 100644 index 000000000..3d553b39c --- /dev/null +++ b/web/lib/chat-workspaces-cache.ts @@ -0,0 +1,35 @@ +import type { ChatWorkspaceRegistration } from "@/lib/workspaces-api"; + +// The sidebar mounts a fresh workspace menu when it collapses or expands. +// Starting that menu with an empty list makes the workspace glyph fall back to +// "Workspace unavailable" for one render, which flashes a different letter and +// tint. Keep the last successful rows at module scope so a remount paints the +// known workspace immediately and refreshes behind it. +// +// Workspace names are account-scoped: auth transitions call the reset below so +// a login, logout or cross-tab boundary never paints the previous account's +// rows on the next mount. +let cachedWorkspaces: ChatWorkspaceRegistration[] | null = null; +let cachedError = ""; + +export function readCachedChatWorkspaces(): ChatWorkspaceRegistration[] { + return cachedWorkspaces ?? []; +} + +export function readCachedChatWorkspacesError(): string { + return cachedError; +} + +export function writeCachedChatWorkspaces(rows: ChatWorkspaceRegistration[]): void { + cachedWorkspaces = rows; + cachedError = ""; +} + +export function writeCachedChatWorkspacesError(message: string): void { + cachedError = message; +} + +export function resetChatWorkspacesCache(): void { + cachedWorkspaces = null; + cachedError = ""; +} diff --git a/web/tests/chat-workspaces-cache.spec.tsx b/web/tests/chat-workspaces-cache.spec.tsx index aa7ea07c4..9106a3537 100644 --- a/web/tests/chat-workspaces-cache.spec.tsx +++ b/web/tests/chat-workspaces-cache.spec.tsx @@ -40,3 +40,53 @@ test("a remounted sidebar keeps the last workspace while it refreshes", async () expect(second.result.current.workspaces[0]?.display_name).toBe("Anglais"); }); + +test("resetting the cache drops the previous account's rows", async () => { + const rows = [ + { + workspace_id: "ws-anglais", + display_name: "Anglais", + kind: "workspace", + status: "ready", + archived: false, + }, + ]; + listWorkspaces.mockResolvedValueOnce(rows); + + const { useChatWorkspaces } = await import("@/hooks/useChatWorkspaces"); + const { resetChatWorkspacesCache } = await import("@/lib/chat-workspaces-cache"); + const first = renderHook(() => useChatWorkspaces()); + await waitFor(() => expect(first.result.current.workspaces).toHaveLength(1)); + + resetChatWorkspacesCache(); + first.unmount(); + + listWorkspaces.mockReturnValue(new Promise(() => {})); + const second = renderHook(() => useChatWorkspaces()); + + expect(second.result.current.workspaces).toHaveLength(0); +}); + +test("auth invalidation clears the workspace cache", async () => { + const { readCachedChatWorkspaces, writeCachedChatWorkspaces } = await import( + "@/lib/chat-workspaces-cache" + ); + writeCachedChatWorkspaces([ + { + workspace_id: "ws-anglais", + display_name: "Anglais", + kind: "workspace", + follows_root: false, + path: "/anglais", + created_at: "2026-01-01T00:00:00Z", + status: "ready", + archived: false, + error: "", + }, + ]); + + const { invalidateAuthStatusCache } = await import("@/lib/auth"); + invalidateAuthStatusCache(); + + expect(readCachedChatWorkspaces()).toHaveLength(0); +});