Skip to content

Commit 43f4cae

Browse files
fix(mobile): ignore stale usage environment selections
1 parent 0fc3d8c commit 43f4cae

3 files changed

Lines changed: 58 additions & 8 deletions

File tree

‎apps/mobile/src/features/usage/UsageRouteScreen.tsx‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { NativeStackScreenOptions } from "../../native/StackHeader";
2828
import { useUsage, type EnvironmentUsageStatus } from "../../state/usage";
2929
import { SettingsSection } from "../settings/components/SettingsSection";
3030
import { UsageDailyChart } from "./UsageDailyChart";
31+
import { toggleUsageEnvironment } from "./usageEnvironmentSelection";
3132
import { useRefreshLimits } from "./UsageLimitsSection";
3233
import { UsageLimitsSection } from "./UsageLimitsPooled";
3334
import { ControlPillMenu } from "../../components/ControlPill";
@@ -169,14 +170,7 @@ export function UsageRouteScreen() {
169170
return;
170171
}
171172
const id = EnvironmentId.make(value);
172-
setSelectedEnvironmentIds((selected) => {
173-
const next = new Set(
174-
selected ?? environments.map((environment) => environment.environmentId),
175-
);
176-
if (next.has(id)) next.delete(id);
177-
else next.add(id);
178-
return next.size === environments.length ? null : next;
179-
});
173+
setSelectedEnvironmentIds((selected) => toggleUsageEnvironment(selected, environments, id));
180174
},
181175
[environments],
182176
);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { EnvironmentId } from "@t3tools/contracts";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { toggleUsageEnvironment } from "./usageEnvironmentSelection";
5+
6+
const a = EnvironmentId.make("a");
7+
const b = EnvironmentId.make("b");
8+
const c = EnvironmentId.make("c");
9+
const removed = EnvironmentId.make("removed");
10+
const environments = [a, b, c].map((environmentId) => ({ environmentId }));
11+
12+
describe("usage environment selection", () => {
13+
it("can exclude an environment from all, then select all again", () => {
14+
const selected = toggleUsageEnvironment(null, environments, b);
15+
expect(selected).toEqual(new Set([a, c]));
16+
expect(toggleUsageEnvironment(selected, environments, b)).toBeNull();
17+
});
18+
19+
it("can deselect the last environment", () => {
20+
expect(toggleUsageEnvironment(new Set([a]), environments, a)).toEqual(new Set());
21+
});
22+
23+
it("does not count removed IDs toward selecting all current environments", () => {
24+
expect(toggleUsageEnvironment(new Set([a, removed]), environments, b)).toEqual(new Set([a, b]));
25+
});
26+
27+
it("returns to all mode despite stale IDs when every current environment is selected", () => {
28+
expect(toggleUsageEnvironment(new Set([a, c, removed]), environments, b)).toBeNull();
29+
});
30+
31+
it("ignores a menu action for an environment that was removed", () => {
32+
expect(toggleUsageEnvironment(new Set([a]), environments, removed)).toEqual(new Set([a]));
33+
});
34+
35+
it("includes newly connected environments only in all mode", () => {
36+
const expanded = [...environments, { environmentId: removed }];
37+
expect(toggleUsageEnvironment(null, expanded, a)).toEqual(new Set([b, c, removed]));
38+
expect(toggleUsageEnvironment(new Set([a, b, c]), expanded, a)).toEqual(new Set([b, c]));
39+
});
40+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { EnvironmentId } from "@t3tools/contracts";
2+
3+
/** Null follows all environments, including ones connected after the menu opened. */
4+
export function toggleUsageEnvironment(
5+
selected: ReadonlySet<EnvironmentId> | null,
6+
environments: readonly { readonly environmentId: EnvironmentId }[],
7+
toggledId: EnvironmentId,
8+
): ReadonlySet<EnvironmentId> | null {
9+
const ids = environments.map(({ environmentId }) => environmentId);
10+
const next = new Set(ids.filter((id) => selected === null || selected.has(id)));
11+
if (ids.includes(toggledId)) {
12+
if (next.has(toggledId)) next.delete(toggledId);
13+
else next.add(toggledId);
14+
}
15+
return ids.every((id) => next.has(id)) ? null : next;
16+
}

0 commit comments

Comments
 (0)