From 84966ed00dbbf04378fcbc2e32f4d571010223eb Mon Sep 17 00:00:00 2001 From: gsknnft Date: Mon, 6 Apr 2026 11:08:41 -0400 Subject: [PATCH] fix(office): persist per-floor selectedAgentId on focusLocalAgent + wire officeFloors runtime state PR #96 findings: - Medium #1: focusLocalAgent now writes selectedAgentId back to floorRosterCache so handleSelectFloor restores the agent the user last picked on each floor rather than snapping back to the hydration-time suggestion. - Medium #2: Add useEffect that calls settingsCoordinator.schedulePatch with officeFloors[activeFloorId] patch on every status/gatewayUrl change, writing status, gatewayUrl, lastKnownGoodAt, lastErrorCode, and lastErrorMessage so the persisted floor runtime state actually tracks live connection transitions. --- src/features/office/screens/OfficeScreen.tsx | 35 +++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/features/office/screens/OfficeScreen.tsx b/src/features/office/screens/OfficeScreen.tsx index e0bd93c..761aed2 100644 --- a/src/features/office/screens/OfficeScreen.tsx +++ b/src/features/office/screens/OfficeScreen.tsx @@ -1175,8 +1175,15 @@ export function OfficeScreen({ setChatOpen(true); } dispatch({ type: "selectAgent", agentId }); + // Keep per-floor cache in sync so handleSelectFloor can restore the last + // agent the user selected when they switch back to this floor. + setFloorRosterCache((prev) => { + const current = prev[activeFloorId]; + if (!current || current.selectedAgentId === agentId) return prev; + return { ...prev, [activeFloorId]: { ...current, selectedAgentId: agentId } }; + }); }, - [dispatch], + [activeFloorId, dispatch], ); const handleSelectFloor = useCallback( (floorId: FloorId) => { @@ -1417,6 +1424,32 @@ export function OfficeScreen({ }; }, [gatewayUrl, loadStudioSettings]); + // Wire officeFloors persisted runtime state so the status / gateway fields + // actually reflect live connection transitions (not just stay at defaults). + useEffect(() => { + const key = gatewayUrl.trim(); + if (!key) return; + const patch = + status === "connected" + ? { + status: "connected" as const, + gatewayUrl: key, + lastKnownGoodAt: Date.now(), + lastErrorCode: null, + lastErrorMessage: null, + } + : status === "connecting" + ? { status: "connecting" as const } + : gatewayError + ? { + status: "error" as const, + lastErrorCode: "GATEWAY_ERROR", + lastErrorMessage: gatewayError, + } + : { status: "disconnected" as const }; + settingsCoordinator.schedulePatch({ officeFloors: { [activeFloorId]: patch } }, 0); + }, [activeFloorId, gatewayError, gatewayUrl, settingsCoordinator, status]); + const loadAgents = useCallback(async (options?: { forceSettings?: boolean; minIntervalMs?: number;