Skip to content

Commit 95db9de

Browse files
only21milclaude
andcommitted
fix(clients): a route added after a reorder no longer disappears
The routes list kept a dragged order after it was saved. Once routes were added and removed so the count matched again, the old order hid the new route and left the remaining one without a Remove button. The held order now drops as soon as the saved order matches it or the saved routes stop being the same set, in both the web and mobile lists. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d53873b commit 95db9de

5 files changed

Lines changed: 127 additions & 12 deletions

File tree

‎apps/mobile/src/features/settings/EnvironmentRoutesSection.tsx‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
connectionRouteLabel,
1111
connectionRoutes,
1212
isLearned,
13+
pendingRouteOrder,
1314
} from "@t3tools/client-runtime/connection";
1415
import { type EnvironmentId, sessionGrantsScope } from "@t3tools/contracts";
1516
import { AUTH_SCOPE_OPTIONS } from "@t3tools/shared/authScopeOptions";
@@ -64,12 +65,10 @@ export function EnvironmentRoutesSection({
6465
const saved = entry === undefined ? [] : connectionRoutes(entry);
6566
const savedIds = saved.map((route) => connectionRouteId(route.target));
6667
const [pending, setPending] = useState<ReadonlyArray<string> | null>(null);
67-
const order =
68-
pending !== null &&
69-
pending.length === savedIds.length &&
70-
pending.some((id, index) => id !== savedIds[index])
71-
? pending
72-
: savedIds;
68+
const shownPending = pendingRouteOrder(pending, savedIds);
69+
// Dropped during render so a stale order never outlives the save it was for.
70+
if (pending !== null && shownPending === null) setPending(null);
71+
const order = shownPending ?? savedIds;
7372
const [drag, setDrag] = useState<{ readonly id: string; readonly translation: number } | null>(
7473
null,
7574
);
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import {
2+
BearerConnectionProfile,
3+
BearerConnectionTarget,
4+
type ConnectionCatalogEntry,
5+
type ConnectionRoute,
6+
} from "@t3tools/client-runtime/connection";
7+
import { EnvironmentId } from "@t3tools/contracts";
8+
import * as Option from "effect/Option";
9+
import { expect, it, vi } from "vite-plus/test";
10+
import { userEvent } from "vite-plus/test/browser";
11+
12+
vi.mock(import("~/state/session"), async (importOriginal) => ({
13+
...(await importOriginal()),
14+
usePreparedConnection: (() => Option.none()) as never,
15+
}));
16+
17+
vi.mock(import("~/state/use-atom-command"), async (importOriginal) => ({
18+
...(await importOriginal()),
19+
useAtomCommand: (() => async () => ({ _tag: "Success" })) as never,
20+
}));
21+
22+
import { render } from "../../../test/render";
23+
import type { EnvironmentPresentation } from "~/state/environments";
24+
import { EnvironmentRoutesList } from "./EnvironmentRoutesList";
25+
26+
const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
27+
28+
function direct(id: string, httpBaseUrl: string): ConnectionRoute {
29+
return {
30+
target: new BearerConnectionTarget({
31+
environmentId: ENVIRONMENT_ID,
32+
label: "Desk",
33+
connectionId: id,
34+
}),
35+
profile: Option.some(
36+
new BearerConnectionProfile({
37+
connectionId: id,
38+
environmentId: ENVIRONMENT_ID,
39+
label: "Desk",
40+
httpBaseUrl,
41+
wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
42+
}),
43+
),
44+
};
45+
}
46+
47+
const LAN = direct("lan", "http://192.168.1.10:3773/");
48+
const TAILNET = direct("tailnet", "https://desk.tail1234.ts.net/");
49+
const PUBLIC = direct("public", "https://desk.example.com/");
50+
51+
function routesList(routes: ReadonlyArray<ConnectionRoute>) {
52+
const [first, ...rest] = routes;
53+
const entry: ConnectionCatalogEntry = {
54+
target: first!.target,
55+
profile: first!.profile,
56+
alternateRoutes: rest,
57+
enabled: true,
58+
};
59+
const environment: EnvironmentPresentation = {
60+
environmentId: ENVIRONMENT_ID,
61+
label: "Desk",
62+
displayUrl: null,
63+
relayManaged: false,
64+
entry,
65+
connection: { phase: "available", error: null, traceId: null },
66+
serverConfig: null,
67+
};
68+
return <EnvironmentRoutesList environment={environment} onAddRoute={() => undefined} />;
69+
}
70+
71+
it("shows a route added after a reorder was saved, once another route is removed", async () => {
72+
await using screen = await render(routesList([LAN, TAILNET]));
73+
await userEvent.click(screen.getByRole("button", { name: "Reorder LAN, position 1" }));
74+
await userEvent.keyboard(" ");
75+
await userEvent.keyboard("{ArrowDown}");
76+
await userEvent.keyboard(" ");
77+
await expect
78+
.element(screen.getByRole("button", { name: "Reorder Tailscale, position 1" }))
79+
.toBeVisible();
80+
81+
await screen.rerender(routesList([TAILNET, LAN]));
82+
await screen.rerender(routesList([TAILNET, LAN, PUBLIC]));
83+
await screen.rerender(routesList([TAILNET, PUBLIC]));
84+
85+
await expect
86+
.element(screen.getByRole("button", { name: "Reorder desk.example.com, position 2" }))
87+
.toBeVisible();
88+
expect(screen.getByRole("listitem").elements()).toHaveLength(2);
89+
await expect
90+
.element(screen.getByRole("button", { name: "Remove desk.example.com route" }))
91+
.toBeVisible();
92+
});

‎apps/web/src/components/settings/EnvironmentRoutesList.tsx‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
connectionRouteLabel,
2424
connectionRoutes,
2525
isLearned,
26+
pendingRouteOrder,
2627
} from "@t3tools/client-runtime/connection";
2728
import { GripVerticalIcon, PlusIcon, XIcon } from "lucide-react";
2829
import { useState } from "react";
@@ -46,12 +47,10 @@ export function EnvironmentRoutesList({
4647
const saved = connectionRoutes(environment.entry);
4748
const savedIds = saved.map((route) => connectionRouteId(route.target));
4849
const [pending, setPending] = useState<ReadonlyArray<string> | null>(null);
49-
const order: Array<string> =
50-
pending !== null &&
51-
pending.length === savedIds.length &&
52-
pending.some((id, index) => id !== savedIds[index])
53-
? [...pending]
54-
: savedIds;
50+
const shownPending = pendingRouteOrder(pending, savedIds);
51+
// Dropped during render so a stale order never outlives the save it was for.
52+
if (pending !== null && shownPending === null) setPending(null);
53+
const order: Array<string> = shownPending === null ? savedIds : [...shownPending];
5554
const byId = new Map(saved.map((route) => [connectionRouteId(route.target), route]));
5655
const routes = order.flatMap((id) => byId.get(id) ?? []);
5756

‎packages/client-runtime/src/connection/routes.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
insertRoute,
1818
entryWithRoutes,
1919
mergeLearnedRoutes,
20+
pendingRouteOrder,
2021
removedWithRelay,
2122
routesAfterRemoving,
2223
sendsTokenInClear,
@@ -87,6 +88,14 @@ describe("connection routes", () => {
8788
const repaired = direct("lan", "http://192.168.1.11:3773/");
8889
expect(upsertRoute([RELAY, LAN], repaired)).toEqual([RELAY, repaired]);
8990
});
91+
92+
it("shows a dragged order only until the saved routes catch up or change", () => {
93+
const dragged = ["tailnet", "lan"];
94+
expect(pendingRouteOrder(dragged, ["lan", "tailnet"])).toBe(dragged);
95+
expect(pendingRouteOrder(dragged, ["tailnet", "lan"])).toBeNull();
96+
expect(pendingRouteOrder(dragged, ["lan", "public"])).toBeNull();
97+
expect(pendingRouteOrder(dragged, ["lan", "tailnet", "public"])).toBeNull();
98+
});
9099
});
91100

92101
describe("learned routes", () => {

‎packages/client-runtime/src/connection/routes.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,22 @@ export function routesAfterRemoving(
422422
});
423423
}
424424

425+
/**
426+
* A dragged route order shows until the saved routes catch up with it. It is
427+
* stale, and returns null, once the saved order matches it or the saved routes
428+
* are no longer the same set: kept any longer, it would hide a route added
429+
* later whenever the count happened to match again.
430+
*/
431+
export function pendingRouteOrder(
432+
pending: ReadonlyArray<string> | null,
433+
savedIds: ReadonlyArray<string>,
434+
): ReadonlyArray<string> | null {
435+
if (pending === null || pending.length !== savedIds.length) return null;
436+
const saved = new Set(savedIds);
437+
if (!pending.every((id) => saved.has(id))) return null;
438+
return pending.some((id, index) => id !== savedIds[index]) ? pending : null;
439+
}
440+
425441
export function removedWithRelay(entry: ConnectionCatalogEntry): boolean {
426442
return routesAfterRemoving(connectionRoutes(entry), RELAY_ROUTE_ID).length === 0;
427443
}

0 commit comments

Comments
 (0)