Skip to content
TypeScript

fix(clients): a route added after a reorder no longer disappears (#24716)

Victor Vogel9e97921ad81f
diff --git a/apps/mobile/src/features/settings/EnvironmentRoutesSection.tsx b/apps/mobile/src/features/settings/EnvironmentRoutesSection.tsx
index 842965dbf15..4b02247c613 100644
--- a/apps/mobile/src/features/settings/EnvironmentRoutesSection.tsx
+++ b/apps/mobile/src/features/settings/EnvironmentRoutesSection.tsx
@@ -10,6 +10,7 @@ import {
connectionRouteLabel,
connectionRoutes,
isLearned,
+ pendingRouteOrder,
} from "@t3tools/client-runtime/connection";
import { type EnvironmentId, sessionGrantsScope } from "@t3tools/contracts";
import {
@@ -67,12 +68,10 @@ export function EnvironmentRoutesSection({
const saved = entry === undefined ? [] : connectionRoutes(entry);
const savedIds = saved.map((route) => connectionRouteId(route.target));
const [pending, setPending] = useState<ReadonlyArray<string> | null>(null);
- const order =
- pending !== null &&
- pending.length === savedIds.length &&
- pending.some((id, index) => id !== savedIds[index])
- ? pending
- : savedIds;
+ const shownPending = pendingRouteOrder(pending, savedIds);
+ // Dropped during render so a stale order never outlives the save it was for.
+ if (pending !== null && shownPending === null) setPending(null);
+ const order = shownPending ?? savedIds;
const [drag, setDrag] = useState<{ readonly id: string; readonly translation: number } | null>(
null,
);
diff --git a/apps/web/src/components/settings/EnvironmentRoutesList.browser.test.tsx b/apps/web/src/components/settings/EnvironmentRoutesList.browser.test.tsx
new file mode 100644
index 00000000000..78a96ba4eba
--- /dev/null
+++ b/apps/web/src/components/settings/EnvironmentRoutesList.browser.test.tsx
@@ -0,0 +1,92 @@
+import {
+ BearerConnectionProfile,
+ BearerConnectionTarget,
+ type ConnectionCatalogEntry,
+ type ConnectionRoute,
+} from "@t3tools/client-runtime/connection";
+import { EnvironmentId } from "@t3tools/contracts";
+import * as Option from "effect/Option";
+import { expect, it, vi } from "vite-plus/test";
+import { userEvent } from "vite-plus/test/browser";
+
+vi.mock(import("~/state/session"), async (importOriginal) => ({
+ ...(await importOriginal()),
+ usePreparedConnection: (() => Option.none()) as never,
+}));
+
+vi.mock(import("~/state/use-atom-command"), async (importOriginal) => ({
+ ...(await importOriginal()),
+ useAtomCommand: (() => async () => ({ _tag: "Success" })) as never,
+}));
+
+import { render } from "../../../test/render";
+import type { EnvironmentPresentation } from "~/state/environments";
+import { EnvironmentRoutesList } from "./EnvironmentRoutesList";
+
+const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
+
+function direct(id: string, httpBaseUrl: string): ConnectionRoute {
+ return {
+ target: new BearerConnectionTarget({
+ environmentId: ENVIRONMENT_ID,
+ label: "Desk",
+ connectionId: id,
+ }),
+ profile: Option.some(
+ new BearerConnectionProfile({
+ connectionId: id,
+ environmentId: ENVIRONMENT_ID,
+ label: "Desk",
+ httpBaseUrl,
+ wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
+ }),
+ ),
+ };
+}
+
+const LAN = direct("lan", "http://192.168.1.10:3773/");
+const TAILNET = direct("tailnet", "https://desk.tail1234.ts.net/");
+const PUBLIC = direct("public", "https://desk.example.com/");
+
+function routesList(routes: ReadonlyArray<ConnectionRoute>) {
+ const [first, ...rest] = routes;
+ const entry: ConnectionCatalogEntry = {
+ target: first!.target,
+ profile: first!.profile,
+ alternateRoutes: rest,
+ enabled: true,
+ };
+ const environment: EnvironmentPresentation = {
+ environmentId: ENVIRONMENT_ID,
+ label: "Desk",
+ displayUrl: null,
+ relayManaged: false,
+ entry,
+ connection: { phase: "available", error: null, traceId: null },
+ serverConfig: null,
+ };
+ return <EnvironmentRoutesList environment={environment} onAddRoute={() => undefined} />;
+}
+
+it("shows a route added after a reorder was saved, once another route is removed", async () => {
+ await using screen = await render(routesList([LAN, TAILNET]));
+ await userEvent.click(screen.getByRole("button", { name: "Reorder LAN, position 1" }));
+ await userEvent.keyboard(" ");
+ await userEvent.keyboard("{ArrowDown}");
+ await userEvent.keyboard(" ");
+ await expect
+ .element(screen.getByRole("button", { name: "Reorder Tailscale, position 1" }))
+ .toBeVisible();
+
+ await screen.rerender(routesList([TAILNET, LAN]));
+ await screen.rerender(routesList([TAILNET, LAN, PUBLIC]));
+ await screen.rerender(routesList([TAILNET, PUBLIC]));
+
+ await expect
+ .element(screen.getByRole("button", { name: "Reorder desk.example.com, position 2" }))
+ .toBeVisible();
+ expect(screen.getByRole("listitem").elements()).toHaveLength(2);
+ await expect
+ .element(screen.getByRole("button", { name: "Remove desk.example.com route" }))
+ .toBeVisible();
+});
diff --git a/apps/web/src/components/settings/EnvironmentRoutesList.tsx b/apps/web/src/components/settings/EnvironmentRoutesList.tsx
index 18b36335f35..6603f380120 100644
--- a/apps/web/src/components/settings/EnvironmentRoutesList.tsx
+++ b/apps/web/src/components/settings/EnvironmentRoutesList.tsx
@@ -23,6 +23,7 @@ import {
connectionRouteLabel,
connectionRoutes,
isLearned,
+ pendingRouteOrder,
} from "@t3tools/client-runtime/connection";
import { GripVerticalIcon, PlusIcon, XIcon } from "lucide-react";
import { useState } from "react";
@@ -46,12 +47,10 @@ export function EnvironmentRoutesList({
const saved = connectionRoutes(environment.entry);
const savedIds = saved.map((route) => connectionRouteId(route.target));
const [pending, setPending] = useState<ReadonlyArray<string> | null>(null);
- const order: Array<string> =
- pending !== null &&
- pending.length === savedIds.length &&
- pending.some((id, index) => id !== savedIds[index])
- ? [...pending]
- : savedIds;
+ const shownPending = pendingRouteOrder(pending, savedIds);
+ // Dropped during render so a stale order never outlives the save it was for.
+ if (pending !== null && shownPending === null) setPending(null);
+ const order: Array<string> = shownPending === null ? savedIds : [...shownPending];
const byId = new Map(saved.map((route) => [connectionRouteId(route.target), route]));
const routes = order.flatMap((id) => byId.get(id) ?? []);
diff --git a/packages/client-runtime/src/connection/routes.test.ts b/packages/client-runtime/src/connection/routes.test.ts
index 44bca8a7a9a..013548eb0da 100644
--- a/packages/client-runtime/src/connection/routes.test.ts
+++ b/packages/client-runtime/src/connection/routes.test.ts
@@ -17,6 +17,7 @@ import {
insertRoute,
entryWithRoutes,
mergeLearnedRoutes,
+ pendingRouteOrder,
removedWithRelay,
routesAfterRemoving,
sendsTokenInClear,
@@ -87,6 +88,14 @@ describe("connection routes", () => {
const repaired = direct("lan", "http://192.168.1.11:3773/");
expect(upsertRoute([RELAY, LAN], repaired)).toEqual([RELAY, repaired]);
});
+
+ it("shows a dragged order only until the saved routes catch up or change", () => {
+ const dragged = ["tailnet", "lan"];
+ expect(pendingRouteOrder(dragged, ["lan", "tailnet"])).toBe(dragged);
+ expect(pendingRouteOrder(dragged, ["tailnet", "lan"])).toBeNull();
+ expect(pendingRouteOrder(dragged, ["lan", "public"])).toBeNull();
+ expect(pendingRouteOrder(dragged, ["lan", "tailnet", "public"])).toBeNull();
+ });
});
describe("learned routes", () => {
diff --git a/packages/client-runtime/src/connection/routes.ts b/packages/client-runtime/src/connection/routes.ts
index 23aa1efa36f..180954864e4 100644
--- a/packages/client-runtime/src/connection/routes.ts
+++ b/packages/client-runtime/src/connection/routes.ts
@@ -422,6 +422,22 @@ export function routesAfterRemoving(
});
}
+/**
+ * A dragged route order shows until the saved routes catch up with it. It is
+ * stale, and returns null, once the saved order matches it or the saved routes
+ * are no longer the same set: kept any longer, it would hide a route added
+ * later whenever the count happened to match again.
+ */
+export function pendingRouteOrder(
+ pending: ReadonlyArray<string> | null,
+ savedIds: ReadonlyArray<string>,
+): ReadonlyArray<string> | null {
+ if (pending === null || pending.length !== savedIds.length) return null;
+ const saved = new Set(savedIds);
+ if (!pending.every((id) => saved.has(id))) return null;
+ return pending.some((id, index) => id !== savedIds[index]) ? pending : null;
+}
+
export function removedWithRelay(entry: ConnectionCatalogEntry): boolean {
return routesAfterRemoving(connectionRoutes(entry), RELAY_ROUTE_ID).length === 0;
}