fix(usage): wait for environment readiness

This commit is contained in:
t3-code[bot] committed 2026-08-09 12:48:44 +00:00
1 parent 6b9bcebcc8
commit a381ea94fb
9 files changed
+105 -73

No files matched your search

@@ -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",
@@ -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) =>
+16 -9
View File
@@ -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")];
+15 -6
View File
@@ -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,
};
+8 -11
View File
@@ -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,
+14 -9
View File
@@ -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({
</span>
);
}
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 (
<span
key={environment.environmentId}
aria-label={`${environment.label}, ${status}`}
className="flex items-center gap-1 text-destructive"
>
<XIcon className="size-3" aria-hidden />
+25 -18
View File
@@ -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", () => {
+15 -6
View File
@@ -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,
};
+2 -10
View File
@@ -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(