From a381ea94fb952c02b0513b63e77dd62b962055f2 Mon Sep 17 00:00:00 2001 From: "t3-code[bot]" <269035359+t3-code[bot]@users.noreply.github.com> Date: Sun, 9 Aug 2026 12:39:53 +0000 Subject: [PATCH] fix(usage): wait for environment readiness --- .../src/components/AndroidAnchoredMenu.tsx | 7 ++- .../src/features/usage/UsageRouteScreen.tsx | 7 +-- apps/mobile/src/state/usage.test.ts | 25 +++++++---- apps/mobile/src/state/usage.ts | 21 ++++++--- .../mobile/src/state/usageEnvironmentScope.ts | 19 ++++---- apps/web/src/components/usage/UsagePage.tsx | 23 ++++++---- apps/web/src/state/usage.test.ts | 43 +++++++++++-------- apps/web/src/state/usage.ts | 21 ++++++--- apps/web/src/state/usageEnvironmentScope.ts | 12 +----- 9 files changed, 105 insertions(+), 73 deletions(-) diff --git a/apps/mobile/src/components/AndroidAnchoredMenu.tsx b/apps/mobile/src/components/AndroidAnchoredMenu.tsx index ebc0388512..255635f67a 100644 --- a/apps/mobile/src/components/AndroidAnchoredMenu.tsx +++ b/apps/mobile/src/components/AndroidAnchoredMenu.tsx @@ -279,7 +279,12 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) { key={action.id ?? `${index}-${action.title}`} android_ripple={{ color: rippleColor }} accessibilityRole="menuitem" - accessibilityState={{ disabled, selected: action.state === "on" }} + accessibilityState={{ + disabled, + ...(action.state === undefined + ? {} + : { selected: action.state === "on" }), + }} disabled={disabled} className={cn( "min-h-11 flex-row items-center gap-2.5 px-3.5 py-2.5", diff --git a/apps/mobile/src/features/usage/UsageRouteScreen.tsx b/apps/mobile/src/features/usage/UsageRouteScreen.tsx index 5daebf90c0..dee258b528 100644 --- a/apps/mobile/src/features/usage/UsageRouteScreen.tsx +++ b/apps/mobile/src/features/usage/UsageRouteScreen.tsx @@ -473,9 +473,10 @@ function UsageCoverageNotice(props: { ); const unavailable = props.environments.filter( (environment) => - environment.phase === "available" || - environment.phase === "offline" || - environment.phase === "error", + !environment.isPending && + (environment.phase === "available" || + environment.phase === "offline" || + environment.phase === "error"), ); const failed = props.environments.filter((environment) => environment.error !== null); const stale = props.environments.filter((environment) => diff --git a/apps/mobile/src/state/usage.test.ts b/apps/mobile/src/state/usage.test.ts index d556819709..089f5712a9 100644 --- a/apps/mobile/src/state/usage.test.ts +++ b/apps/mobile/src/state/usage.test.ts @@ -4,7 +4,6 @@ import { describe, expect, it } from "vite-plus/test"; import { getEnvironmentUsageLoadingState, - isEnvironmentUsageSettling, resolveEnvironmentUsageScope, type EnvironmentUsageOption, } from "./usageEnvironmentScope"; @@ -19,23 +18,31 @@ const environment = ( }); describe("mobile usage environment scope", () => { - it("distinguishes connection progress from terminal states", () => { - expect(isEnvironmentUsageSettling("available")).toBe(false); - expect(isEnvironmentUsageSettling("connecting")).toBe(true); - expect(isEnvironmentUsageSettling("reconnecting")).toBe(true); - expect(isEnvironmentUsageSettling("offline")).toBe(false); - expect(isEnvironmentUsageSettling("error")).toBe(false); + it("does not wait indefinitely for a reconnecting environment", () => { + expect( + getEnvironmentUsageLoadingState([ + { phase: "reconnecting", isPending: false, summary: null, error: null }, + ]), + ).toEqual({ isPending: false, isPartial: false }); }); it("does not let an offline environment hold healthy totals open", () => { expect( getEnvironmentUsageLoadingState([ - { phase: "connected", summary: {}, error: null }, - { phase: "offline", summary: null, error: null }, + { phase: "connected", isPending: false, summary: {}, error: null }, + { phase: "offline", isPending: false, summary: null, error: null }, ]), ).toEqual({ isPending: false, isPartial: false }); }); + it("waits for an initial available connection state", () => { + expect( + getEnvironmentUsageLoadingState([ + { phase: "available", isPending: true, summary: null, error: null }, + ]), + ).toEqual({ isPending: true, isPartial: false }); + }); + it("isolates a selected environment and falls back when it disappears", () => { const options = [environment("healthy", "connected"), environment("down", "offline")]; diff --git a/apps/mobile/src/state/usage.ts b/apps/mobile/src/state/usage.ts index 4f4749101d..7300747499 100644 --- a/apps/mobile/src/state/usage.ts +++ b/apps/mobile/src/state/usage.ts @@ -22,11 +22,11 @@ import { useCallback, useMemo } from "react"; import { mergeUsage, type EnvironmentUsage, type MergedUsage } from "@t3tools/shared/usageMerge"; import { appAtomRegistry } from "./atom-registry"; +import { environmentCatalog } from "../connection/catalog"; import { environmentPresentations } from "./presentation"; import { serverEnvironment } from "./server"; import { getEnvironmentUsageLoadingState, - isEnvironmentUsageSettling, resolveEnvironmentUsageScope, type EnvironmentUsageOption, } from "./usageEnvironmentScope"; @@ -41,6 +41,7 @@ export interface EnvironmentUsageStatus extends EnvironmentUsageOption { } interface UsageAtomValue { + readonly isCatalogReady: boolean; readonly options: readonly EnvironmentUsageOption[]; readonly environments: readonly EnvironmentUsageStatus[]; readonly selectedEnvironmentId: EnvironmentId | null; @@ -54,6 +55,7 @@ interface UsageAtomKey { const usageByWindowAtom = Atom.family((key: string) => Atom.make((get): UsageAtomValue => { const { input, selectedEnvironmentId } = JSON.parse(key) as UsageAtomKey; + const catalog = get(environmentCatalog.catalogValueAtom); const presentations = get(environmentPresentations.presentationsAtom); const options = Array.from(presentations, ([environmentId, presentation]) => ({ environmentId, @@ -65,20 +67,27 @@ const usageByWindowAtom = Atom.family((key: string) => const environments: EnvironmentUsageStatus[] = []; for (const option of scope.environments) { const { environmentId } = option; + const connectionResult = get(environmentCatalog.stateAtom(environmentId)); // Keep reading the environment-scoped atom while disconnected so a prior - // successful value remains visible. Its waiting state only affects the UI - // while the connection is actively settling. + // successful value remains visible. Only a connected query can keep the UI loading. const result = get(serverEnvironment.usageSummary({ environmentId, input })); const failed = option.phase === "connected" && result._tag === "Failure"; environments.push({ ...option, - isPending: isEnvironmentUsageSettling(option.phase) && result.waiting, + isPending: + (option.phase === "available" && connectionResult.waiting) || + (option.phase === "connected" && result.waiting), error: failed ? "This environment could not report usage." : null, summary: Option.getOrNull(AsyncResult.value(result)), }); } - return { options, environments, selectedEnvironmentId: scope.selectedEnvironmentId }; + return { + isCatalogReady: catalog.isReady, + options, + environments, + selectedEnvironmentId: scope.selectedEnvironmentId, + }; }).pipe(Atom.withLabel(`mobile-usage:${key}`)), ); @@ -146,7 +155,7 @@ export function useUsage( options: value.options, environments: value.environments, selectedEnvironmentId: value.selectedEnvironmentId, - isPending: loadingState.isPending, + isPending: !value.isCatalogReady || loadingState.isPending, isPartial: loadingState.isPartial, refresh, }; diff --git a/apps/mobile/src/state/usageEnvironmentScope.ts b/apps/mobile/src/state/usageEnvironmentScope.ts index c5a48c42a9..51e080b781 100644 --- a/apps/mobile/src/state/usageEnvironmentScope.ts +++ b/apps/mobile/src/state/usageEnvironmentScope.ts @@ -7,11 +7,6 @@ export interface EnvironmentUsageOption { readonly phase: EnvironmentConnectionPhase; } -/** Connection phases where a usage answer may still arrive without user action. */ -export function isEnvironmentUsageSettling(phase: EnvironmentConnectionPhase): boolean { - return phase === "connecting" || phase === "reconnecting" || phase === "connected"; -} - export function resolveEnvironmentUsageScope( options: readonly EnvironmentUsageOption[], selectedEnvironmentId: EnvironmentId | null, @@ -35,20 +30,22 @@ export function resolveEnvironmentUsageScope( interface EnvironmentUsageLoadingEntry { readonly phase: EnvironmentConnectionPhase; + readonly isPending: boolean; readonly summary: unknown | null; readonly error: string | null; } +export function isEnvironmentUsageStillReporting( + environment: EnvironmentUsageLoadingEntry, +): boolean { + return environment.isPending && environment.summary === null && environment.error === null; +} + export function getEnvironmentUsageLoadingState( environments: readonly EnvironmentUsageLoadingEntry[], ): { readonly isPending: boolean; readonly isPartial: boolean } { const answeredCount = environments.filter((environment) => environment.summary !== null).length; - const stillReporting = environments.filter( - (environment) => - isEnvironmentUsageSettling(environment.phase) && - environment.summary === null && - environment.error === null, - ).length; + const stillReporting = environments.filter(isEnvironmentUsageStillReporting).length; return { isPending: answeredCount === 0 && stillReporting > 0, diff --git a/apps/web/src/components/usage/UsagePage.tsx b/apps/web/src/components/usage/UsagePage.tsx index 7320a7b391..97a935bf1e 100644 --- a/apps/web/src/components/usage/UsagePage.tsx +++ b/apps/web/src/components/usage/UsagePage.tsx @@ -473,9 +473,10 @@ function UsageCoverageNotice({ ); const unavailable = environments.filter( (environment) => - environment.phase === "available" || - environment.phase === "offline" || - environment.phase === "error", + !environment.isPending && + (environment.phase === "available" || + environment.phase === "offline" || + environment.phase === "error"), ); const failed = environments.filter((environment) => environment.error !== null); const stale = environments.filter((environment) => @@ -560,15 +561,19 @@ function UsageDeviceStrip({ ); } - if ( - environment.error !== null || - environment.phase === "available" || - environment.phase === "offline" || - environment.phase === "error" - ) { + if (environment.error !== null || !environment.isPending) { + const status = + environment.error !== null + ? "could not report usage" + : environment.phase === "available" + ? "not connected" + : environment.phase === "error" + ? "unavailable" + : environment.phase; return ( diff --git a/apps/web/src/state/usage.test.ts b/apps/web/src/state/usage.test.ts index 5215d4aa3d..4bb40f8939 100644 --- a/apps/web/src/state/usage.test.ts +++ b/apps/web/src/state/usage.test.ts @@ -4,7 +4,6 @@ import { describe, expect, it } from "vite-plus/test"; import { getEnvironmentUsageLoadingState, - isEnvironmentUsageSettling, isEnvironmentUsageStillReporting, resolveEnvironmentUsageScope, type EnvironmentUsageOption, @@ -25,38 +24,47 @@ describe("usage environment scope", () => { const scope = resolveEnvironmentUsageScope(options, null); expect(scope.environments).toEqual(options); - expect(isEnvironmentUsageSettling(scope.environments[0]!.phase)).toBe(true); - expect(isEnvironmentUsageSettling(scope.environments[1]!.phase)).toBe(false); }); it("finishes with healthy totals when another environment is offline", () => { expect( getEnvironmentUsageLoadingState([ - { phase: "connected", summary: {}, error: null }, - { phase: "offline", summary: null, error: null }, + { phase: "connected", isPending: false, summary: {}, error: null }, + { phase: "offline", isPending: false, summary: null, error: null }, ]), ).toEqual({ isPending: false, isPartial: false }); }); it("returns immediately when the only environment is offline", () => { expect( - getEnvironmentUsageLoadingState([{ phase: "offline", summary: null, error: null }]), + getEnvironmentUsageLoadingState([ + { phase: "offline", isPending: false, summary: null, error: null }, + ]), ).toEqual({ isPending: false, isPartial: false }); }); it("keeps healthy totals visible while another environment reconnects", () => { expect( getEnvironmentUsageLoadingState([ - { phase: "connected", summary: {}, error: null }, - { phase: "reconnecting", summary: null, error: null }, + { phase: "connected", isPending: false, summary: {}, error: null }, + { phase: "reconnecting", isPending: false, summary: null, error: null }, ]), - ).toEqual({ isPending: false, isPartial: true }); + ).toEqual({ isPending: false, isPartial: false }); + }); + + it("does not wait indefinitely for a reconnecting environment", () => { + expect( + getEnvironmentUsageLoadingState([ + { phase: "reconnecting", isPending: false, summary: null, error: null }, + ]), + ).toEqual({ isPending: false, isPartial: false }); }); it("uses the same reporting state for loading and device progress", () => { expect( isEnvironmentUsageStillReporting({ - phase: "reconnecting", + phase: "connected", + isPending: true, summary: null, error: null, }), @@ -64,19 +72,19 @@ describe("usage environment scope", () => { expect( isEnvironmentUsageStillReporting({ phase: "connected", + isPending: false, summary: null, error: "failed", }), ).toBe(false); }); - it("shows connection transitions as settling but leaves idle entries terminal", () => { - expect(isEnvironmentUsageSettling("available")).toBe(false); - expect(isEnvironmentUsageSettling("connecting")).toBe(true); - expect(isEnvironmentUsageSettling("reconnecting")).toBe(true); - expect(isEnvironmentUsageSettling("connected")).toBe(true); - expect(isEnvironmentUsageSettling("offline")).toBe(false); - expect(isEnvironmentUsageSettling("error")).toBe(false); + it("keeps an initial available connection pending until its state resolves", () => { + expect( + getEnvironmentUsageLoadingState([ + { phase: "available", isPending: true, summary: null, error: null }, + ]), + ).toEqual({ isPending: true, isPartial: false }); }); it("keeps a selected offline environment terminal and isolated", () => { @@ -87,7 +95,6 @@ describe("usage environment scope", () => { expect(scope.selectedEnvironmentId).toBe("down"); expect(scope.environments).toEqual([environment("down", "offline")]); - expect(isEnvironmentUsageSettling(scope.environments[0]!.phase)).toBe(false); }); it("falls back to all environments when the selection disappears", () => { diff --git a/apps/web/src/state/usage.ts b/apps/web/src/state/usage.ts index f93ca89c37..131ccaa39b 100644 --- a/apps/web/src/state/usage.ts +++ b/apps/web/src/state/usage.ts @@ -19,12 +19,12 @@ import { AsyncResult, Atom } from "effect/unstable/reactivity"; import { useCallback, useMemo } from "react"; import { mergeUsage, type EnvironmentUsage, type MergedUsage } from "@t3tools/shared/usageMerge"; +import { environmentCatalog } from "../connection/catalog"; import { appAtomRegistry } from "../rpc/atomRegistry"; import { environmentPresentations } from "./presentation"; import { serverEnvironment } from "./server"; import { getEnvironmentUsageLoadingState, - isEnvironmentUsageSettling, resolveEnvironmentUsageScope, type EnvironmentUsageOption, } from "./usageEnvironmentScope"; @@ -39,6 +39,7 @@ export interface EnvironmentUsageStatus extends EnvironmentUsageOption { } interface UsageAtomValue { + readonly isCatalogReady: boolean; readonly options: readonly EnvironmentUsageOption[]; readonly environments: readonly EnvironmentUsageStatus[]; readonly selectedEnvironmentId: EnvironmentId | null; @@ -52,6 +53,7 @@ interface UsageAtomKey { const usageByWindowAtom = Atom.family((key: string) => Atom.make((get): UsageAtomValue => { const { input, selectedEnvironmentId } = JSON.parse(key) as UsageAtomKey; + const catalog = get(environmentCatalog.catalogValueAtom); const presentations = get(environmentPresentations.presentationsAtom); const options = Array.from(presentations, ([environmentId, presentation]) => ({ environmentId, @@ -63,20 +65,27 @@ const usageByWindowAtom = Atom.family((key: string) => const environments: EnvironmentUsageStatus[] = []; for (const option of scope.environments) { const { environmentId } = option; + const connectionResult = get(environmentCatalog.stateAtom(environmentId)); // Keep reading the environment-scoped atom while disconnected so a prior - // successful value remains visible. Its waiting state only affects the UI - // while the connection is actively settling. + // successful value remains visible. Only a connected query can keep the UI loading. const result = get(serverEnvironment.usageSummary({ environmentId, input })); const failed = option.phase === "connected" && result._tag === "Failure"; environments.push({ ...option, - isPending: isEnvironmentUsageSettling(option.phase) && result.waiting, + isPending: + (option.phase === "available" && connectionResult.waiting) || + (option.phase === "connected" && result.waiting), error: failed ? "This environment could not report usage." : null, summary: Option.getOrNull(AsyncResult.value(result)), }); } - return { options, environments, selectedEnvironmentId: scope.selectedEnvironmentId }; + return { + isCatalogReady: catalog.isReady, + options, + environments, + selectedEnvironmentId: scope.selectedEnvironmentId, + }; }).pipe(Atom.withLabel(`web-usage:${key}`)), ); @@ -144,7 +153,7 @@ export function useUsage( options: value.options, environments: value.environments, selectedEnvironmentId: value.selectedEnvironmentId, - isPending: loadingState.isPending, + isPending: !value.isCatalogReady || loadingState.isPending, isPartial: loadingState.isPartial, refresh, }; diff --git a/apps/web/src/state/usageEnvironmentScope.ts b/apps/web/src/state/usageEnvironmentScope.ts index fc9157a647..51e080b781 100644 --- a/apps/web/src/state/usageEnvironmentScope.ts +++ b/apps/web/src/state/usageEnvironmentScope.ts @@ -7,11 +7,6 @@ export interface EnvironmentUsageOption { readonly phase: EnvironmentConnectionPhase; } -/** Connection phases where a usage answer may still arrive without user action. */ -export function isEnvironmentUsageSettling(phase: EnvironmentConnectionPhase): boolean { - return phase === "connecting" || phase === "reconnecting" || phase === "connected"; -} - export function resolveEnvironmentUsageScope( options: readonly EnvironmentUsageOption[], selectedEnvironmentId: EnvironmentId | null, @@ -35,6 +30,7 @@ export function resolveEnvironmentUsageScope( interface EnvironmentUsageLoadingEntry { readonly phase: EnvironmentConnectionPhase; + readonly isPending: boolean; readonly summary: unknown | null; readonly error: string | null; } @@ -42,11 +38,7 @@ interface EnvironmentUsageLoadingEntry { export function isEnvironmentUsageStillReporting( environment: EnvironmentUsageLoadingEntry, ): boolean { - return ( - isEnvironmentUsageSettling(environment.phase) && - environment.summary === null && - environment.error === null - ); + return environment.isPending && environment.summary === null && environment.error === null; } export function getEnvironmentUsageLoadingState(