From 0119872db67a727a42995bb9c377d856e182ff99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Thu, 16 Apr 2026 21:56:40 -0400 Subject: [PATCH] fix(renderer): use snapshot.currentCompanyId as single source of truth (D-03) CompanySlice's standalone currentCompanyId field + setCurrentCompanyId action were never written by any caller, so useDesktopEvents.ts read null and fell through to full refresh for every entity event -- the granular patch machinery was dead code. - Remove currentCompanyId field + setCurrentCompanyId from CompanySlice - refreshBadges reads from snapshot.currentCompanyId - useDesktopEvents reads companyId from snapshot.currentCompanyId - Update store unit test to seed snapshot instead of calling removed action --- src/renderer/hooks/useDesktopEvents.ts | 2 +- src/renderer/state/slices/company.ts | 9 +-------- tests/unit/store.test.ts | 4 ++-- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/renderer/hooks/useDesktopEvents.ts b/src/renderer/hooks/useDesktopEvents.ts index 4aba88d3..e0994d67 100644 --- a/src/renderer/hooks/useDesktopEvents.ts +++ b/src/renderer/hooks/useDesktopEvents.ts @@ -13,7 +13,7 @@ export function useDesktopEvents( const store = useAppStore; async function handleGranularBatch(events: GranularEventEntry[]) { - const companyId = store.getState().currentCompanyId; + const companyId = store.getState().snapshot?.currentCompanyId ?? null; if (!companyId) { void refreshRef.current(); return; diff --git a/src/renderer/state/slices/company.ts b/src/renderer/state/slices/company.ts index a33a18f6..72f32a29 100644 --- a/src/renderer/state/slices/company.ts +++ b/src/renderer/state/slices/company.ts @@ -14,14 +14,12 @@ import type { SidebarBadges } from "@shared/contracts"; export interface CompanySlice { snapshot: ProfileSnapshot | null; - currentCompanyId: string | null; companyMetrics: CompanyMetrics | null; standupReport: StandupReport | null; inboxItems: InboxItem[]; badges: SidebarBadges | null; error: string | null; setSnapshot: (snapshot: ProfileSnapshot | null) => void; - setCurrentCompanyId: (id: string | null) => void; setCompanyMetrics: (metrics: CompanyMetrics | null) => void; setStandupReport: (report: StandupReport | null) => void; setInboxItems: (items: InboxItem[]) => void; @@ -46,7 +44,6 @@ export const createCompanySlice: StateCreator< CompanySlice > = (set, get) => ({ snapshot: null, - currentCompanyId: null, companyMetrics: null, standupReport: null, inboxItems: [], @@ -56,10 +53,6 @@ export const createCompanySlice: StateCreator< set((state) => { state.snapshot = snapshot; }), - setCurrentCompanyId: (id) => - set((state) => { - state.currentCompanyId = id; - }), setCompanyMetrics: (metrics) => set((state) => { state.companyMetrics = metrics; @@ -166,7 +159,7 @@ export const createCompanySlice: StateCreator< }), refreshBadges: async () => { - const companyId = get().currentCompanyId; + const companyId = get().snapshot?.currentCompanyId ?? null; if (!companyId) return; try { const result = await window.agentCompany.getSidebarBadges(companyId); diff --git a/tests/unit/store.test.ts b/tests/unit/store.test.ts index cb227aa0..8a3fed56 100644 --- a/tests/unit/store.test.ts +++ b/tests/unit/store.test.ts @@ -495,8 +495,8 @@ describe("CompanySlice patch methods", () => { const mockBadges = { tasks: 5, approvals: 2, runs: 0, messages: 3 }; mockGetSidebarBadges.mockResolvedValue({ ok: true, data: mockBadges }); - // Set current company ID first - useAppStore.getState().setCurrentCompanyId("company-1"); + // Seed snapshot with currentCompanyId -- refreshBadges reads from snapshot + useAppStore.getState().setSnapshot(makeSnapshot()); await useAppStore.getState().refreshBadges();