Skip to content

Commit 87a79e8

Browse files
committed
fix(web): providers opened from a checkout link target that checkout's machine
Signed-off-by: Yordis Prieto <[email protected]>
1 parent 7baefd2 commit 87a79e8

4 files changed

Lines changed: 49 additions & 15 deletions

File tree

‎apps/web/src/components/settings/settingsScope.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
SidebarProjectSnapshot,
77
} from "../../sidebarProjectGrouping";
88
import { resolveSettingsScope, validateSettingsScopeSearch } from "./settingsScope";
9+
import { selectSingleEnvironmentScope } from "./settingsScopeAxis";
910

1011
const laptopId = EnvironmentId.make("laptop");
1112
const serverId = EnvironmentId.make("server");
@@ -203,3 +204,35 @@ describe("settings scope resolution", () => {
203204
});
204205
});
205206
});
207+
208+
describe("single-environment scope", () => {
209+
const connected = environments.map((environment) => ({
210+
...environment,
211+
connection: { phase: "connected" as const },
212+
}));
213+
const select = (search: Parameters<typeof resolveSettingsScope>[0]) =>
214+
selectSingleEnvironmentScope(
215+
search,
216+
resolveSettingsScope(search, groups, environments),
217+
connected,
218+
laptopId,
219+
);
220+
221+
it("targets the checkout's environment for a link without a machine", () => {
222+
expect(select({ project: "t3code", checkout: third.physicalProjectKey })).toEqual({
223+
machine: serverId,
224+
});
225+
});
226+
227+
it("narrows to where the project is registered", () => {
228+
expect(select({ project: "other" })).toEqual({ machine: serverId });
229+
});
230+
231+
it("falls back to the primary environment when the project is gone", () => {
232+
expect(select({ project: "missing" })).toEqual({ machine: laptopId });
233+
});
234+
235+
it("keeps an explicit machine and drops the project", () => {
236+
expect(select({ project: "other", machine: laptopId })).toEqual({ machine: laptopId });
237+
});
238+
});

‎apps/web/src/components/settings/settingsScopeAxis.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export function selectProjectAxis(search: SettingsScopeSearch, value: string): S
5454
return next;
5555
}
5656

57-
/** Provider configuration always belongs to one environment, including project scopes. */
57+
/**
58+
* Provider configuration always belongs to one environment. A project or
59+
* checkout only picks which one when no machine is chosen and never reaches
60+
* the result, so a stale project cannot leave the page without a target.
61+
*/
5862
export function selectSingleEnvironmentScope(
5963
search: SettingsScopeSearch,
6064
scope: ResolvedSettingsScope,
@@ -64,14 +68,14 @@ export function selectSingleEnvironmentScope(
6468
}[],
6569
primaryEnvironmentId: string | null,
6670
): SettingsScopeSearch {
67-
if (search.machine || scope.kind === "unavailable") return search;
68-
const selectedIds = new Set(scope.environmentIds);
69-
const candidates = environments.filter((environment) =>
70-
selectedIds.has(environment.environmentId),
71-
);
71+
if (search.machine) return { machine: search.machine };
72+
const selectedIds = scope.kind === "unavailable" ? null : new Set(scope.environmentIds);
73+
const candidates = selectedIds
74+
? environments.filter((environment) => selectedIds.has(environment.environmentId))
75+
: environments;
7276
const selected =
7377
candidates.find((environment) => environment.environmentId === primaryEnvironmentId) ??
7478
candidates.find((environment) => environment.connection.phase === "connected") ??
7579
candidates[0];
76-
return selected ? { ...search, machine: selected.environmentId } : search;
80+
return selected ? { machine: selected.environmentId } : {};
7781
}

‎apps/web/src/routes/settings.providers.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { useSettingsScope } from "../components/settings/SettingsScopeContext";
66

77
/**
88
* Providers are machine state, so the page shows one environment at a time:
9-
* the chosen one from the settings scope selector. The project is ignored.
9+
* the chosen one from the settings scope selector.
10+
* A project crumb narrows candidates to where that project is registered.
1011
*/
1112
function SettingsProvidersRoute() {
1213
const target = Route.useSearch();

‎apps/web/src/routes/settings.tsx‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Outlet, createFileRoute, redirect, useLocation } from "@tanstack/react-router";
2-
import { useMemo, useState, type ReactNode } from "react";
2+
import { useState, type ReactNode } from "react";
33
import { RotateCcwIcon } from "lucide-react";
44
import { Button } from "../components/ui/button";
55
import { useSettingsRestore } from "../components/settings/SettingsPanels";
@@ -155,14 +155,10 @@ function SettingsRouteLayout() {
155155
const navigate = Route.useNavigate();
156156
const pathname = useLocation({ select: (location) => location.pathname });
157157
const environmentOnly = SETTINGS_ENVIRONMENT_ONLY_PATHS.has(pathname);
158-
const search = useMemo(
159-
() => (environmentOnly ? (rawSearch.machine ? { machine: rawSearch.machine } : {}) : rawSearch),
160-
[environmentOnly, rawSearch],
161-
);
162158
return (
163159
<SettingsScopeProvider
164-
search={search}
165-
singleEnvironment={pathname === "/settings/providers"}
160+
search={rawSearch}
161+
singleEnvironment={environmentOnly}
166162
onChange={(next) => {
167163
// Send every axis so the retain middleware sees an explicit target
168164
// even when the choice is "all", which is the absence of a key.

0 commit comments

Comments
 (0)