From b29c9bbcdec5faca01d115dd73ad91261474d885 Mon Sep 17 00:00:00 2001 From: "t3-code[bot]" <269035359+t3-code[bot]@users.noreply.github.com> Date: Sun, 9 Aug 2026 22:12:35 +0000 Subject: [PATCH] fix usage environment cache handling --- .../src/features/usage/UsageRouteScreen.tsx | 4 ++- apps/mobile/src/state/usage.ts | 34 +++++++++++-------- apps/web/src/state/usage.ts | 34 +++++++++++-------- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/apps/mobile/src/features/usage/UsageRouteScreen.tsx b/apps/mobile/src/features/usage/UsageRouteScreen.tsx index dee258b528..303a2c9be6 100644 --- a/apps/mobile/src/features/usage/UsageRouteScreen.tsx +++ b/apps/mobile/src/features/usage/UsageRouteScreen.tsx @@ -89,7 +89,9 @@ export function UsageRouteScreen() { // The pull spinner tracks re-scans of connected environments that have // answered before. The initial scan renders its own placeholder. - const refreshing = environments.some((entry) => entry.isPending && entry.summary !== null); + const refreshing = environments.some( + (entry) => entry.phase === "connected" && entry.isPending && entry.summary !== null, + ); return ( diff --git a/apps/mobile/src/state/usage.ts b/apps/mobile/src/state/usage.ts index b53b182c29..c1438173e7 100644 --- a/apps/mobile/src/state/usage.ts +++ b/apps/mobile/src/state/usage.ts @@ -44,17 +44,15 @@ interface UsageAtomValue { readonly isCatalogReady: boolean; readonly options: readonly EnvironmentUsageOption[]; readonly environments: readonly EnvironmentUsageStatus[]; - readonly selectedEnvironmentId: EnvironmentId | null; } interface UsageAtomKey { readonly input: UsageSummaryInput; - readonly selectedEnvironmentId: EnvironmentId | null; } const usageByWindowAtom = Atom.family((key: string) => Atom.make((get): UsageAtomValue => { - const { input, selectedEnvironmentId } = JSON.parse(key) as UsageAtomKey; + const { input } = JSON.parse(key) as UsageAtomKey; const catalog = get(environmentCatalog.catalogValueAtom); const presentations = get(environmentPresentations.presentationsAtom); const options = Array.from(presentations, ([environmentId, presentation]) => ({ @@ -62,10 +60,11 @@ const usageByWindowAtom = Atom.family((key: string) => label: presentation.entry.target.label, phase: presentation.connection.phase, })); - const scope = resolveEnvironmentUsageScope(options, selectedEnvironmentId); + // Keep every environment subscribed while this time window is mounted. Filtering + // only the view avoids evicting sibling usage caches when switching scopes. const environments: EnvironmentUsageStatus[] = []; - for (const option of scope.environments) { + for (const option of options) { const { environmentId } = option; const connectionResult = get(environmentCatalog.stateAtom(environmentId)); // Keep reading the environment-scoped atom while disconnected so a prior @@ -88,7 +87,6 @@ const usageByWindowAtom = Atom.family((key: string) => isCatalogReady: catalog.isReady, options, environments, - selectedEnvironmentId: scope.selectedEnvironmentId, }; }).pipe(Atom.withLabel(`mobile-usage:${key}`)), ); @@ -118,25 +116,31 @@ export function useUsage( untilDay: input.untilDay, timeZone: input.timeZone, }, - selectedEnvironmentId, }), - [input.sinceDay, input.untilDay, input.timeZone, selectedEnvironmentId], + [input.sinceDay, input.untilDay, input.timeZone], ); const atom = usageByWindowAtom(key); const value = useAtomValue(atom); + const scope = resolveEnvironmentUsageScope(value.options, selectedEnvironmentId); + const environments = useMemo(() => { + if (scope.selectedEnvironmentId === null) return value.environments; + return value.environments.filter( + (environment) => environment.environmentId === scope.selectedEnvironmentId, + ); + }, [scope.selectedEnvironmentId, value.environments]); const refresh = useCallback(() => { const { input } = JSON.parse(key) as UsageAtomKey; - for (const environment of value.environments) { + for (const environment of environments) { if (environment.phase !== "connected") continue; appAtomRegistry.refresh( serverEnvironment.usageSummary({ environmentId: environment.environmentId, input }), ); } - }, [key, value.environments]); + }, [environments, key]); const merged = useMemo(() => { - const answered: EnvironmentUsage[] = value.environments.flatMap((environment) => + const answered: EnvironmentUsage[] = environments.flatMap((environment) => environment.summary === null ? [] : [ @@ -148,15 +152,15 @@ export function useUsage( ], ); return mergeUsage(answered, USAGE_CONTRACT_VERSION); - }, [value.environments]); + }, [environments]); - const loadingState = getEnvironmentUsageLoadingState(value.environments); + const loadingState = getEnvironmentUsageLoadingState(environments); return { merged, options: value.options, - environments: value.environments, - selectedEnvironmentId: value.selectedEnvironmentId, + environments, + selectedEnvironmentId: scope.selectedEnvironmentId, isPending: !value.isCatalogReady || loadingState.isPending, isPartial: loadingState.isPartial, refresh, diff --git a/apps/web/src/state/usage.ts b/apps/web/src/state/usage.ts index 32703e01c3..bb6f82c7e5 100644 --- a/apps/web/src/state/usage.ts +++ b/apps/web/src/state/usage.ts @@ -42,17 +42,15 @@ interface UsageAtomValue { readonly isCatalogReady: boolean; readonly options: readonly EnvironmentUsageOption[]; readonly environments: readonly EnvironmentUsageStatus[]; - readonly selectedEnvironmentId: EnvironmentId | null; } interface UsageAtomKey { readonly input: UsageSummaryInput; - readonly selectedEnvironmentId: EnvironmentId | null; } const usageByWindowAtom = Atom.family((key: string) => Atom.make((get): UsageAtomValue => { - const { input, selectedEnvironmentId } = JSON.parse(key) as UsageAtomKey; + const { input } = JSON.parse(key) as UsageAtomKey; const catalog = get(environmentCatalog.catalogValueAtom); const presentations = get(environmentPresentations.presentationsAtom); const options = Array.from(presentations, ([environmentId, presentation]) => ({ @@ -60,10 +58,11 @@ const usageByWindowAtom = Atom.family((key: string) => label: presentation.entry.target.label, phase: presentation.connection.phase, })); - const scope = resolveEnvironmentUsageScope(options, selectedEnvironmentId); + // Keep every environment subscribed while this time window is mounted. Filtering + // only the view avoids evicting sibling usage caches when switching scopes. const environments: EnvironmentUsageStatus[] = []; - for (const option of scope.environments) { + for (const option of options) { const { environmentId } = option; const connectionResult = get(environmentCatalog.stateAtom(environmentId)); // Keep reading the environment-scoped atom while disconnected so a prior @@ -86,7 +85,6 @@ const usageByWindowAtom = Atom.family((key: string) => isCatalogReady: catalog.isReady, options, environments, - selectedEnvironmentId: scope.selectedEnvironmentId, }; }).pipe(Atom.withLabel(`web-usage:${key}`)), ); @@ -116,25 +114,31 @@ export function useUsage( untilDay: input.untilDay, timeZone: input.timeZone, }, - selectedEnvironmentId, }), - [input.sinceDay, input.untilDay, input.timeZone, selectedEnvironmentId], + [input.sinceDay, input.untilDay, input.timeZone], ); const atom = usageByWindowAtom(key); const value = useAtomValue(atom); + const scope = resolveEnvironmentUsageScope(value.options, selectedEnvironmentId); + const environments = useMemo(() => { + if (scope.selectedEnvironmentId === null) return value.environments; + return value.environments.filter( + (environment) => environment.environmentId === scope.selectedEnvironmentId, + ); + }, [scope.selectedEnvironmentId, value.environments]); const refresh = useCallback(() => { const { input } = JSON.parse(key) as UsageAtomKey; - for (const environment of value.environments) { + for (const environment of environments) { if (environment.phase !== "connected") continue; appAtomRegistry.refresh( serverEnvironment.usageSummary({ environmentId: environment.environmentId, input }), ); } - }, [key, value.environments]); + }, [environments, key]); const merged = useMemo(() => { - const answered: EnvironmentUsage[] = value.environments.flatMap((environment) => + const answered: EnvironmentUsage[] = environments.flatMap((environment) => environment.summary === null ? [] : [ @@ -146,15 +150,15 @@ export function useUsage( ], ); return mergeUsage(answered, USAGE_CONTRACT_VERSION); - }, [value.environments]); + }, [environments]); - const loadingState = getEnvironmentUsageLoadingState(value.environments); + const loadingState = getEnvironmentUsageLoadingState(environments); return { merged, options: value.options, - environments: value.environments, - selectedEnvironmentId: value.selectedEnvironmentId, + environments, + selectedEnvironmentId: scope.selectedEnvironmentId, isPending: !value.isCatalogReady || loadingState.isPending, isPartial: loadingState.isPartial, refresh,