Open History to see the last commit
1import type { SearchMiddleware } from "@tanstack/react-router";
2
3import { validateSettingsScopeSearch, type SettingsScopeSearch } from "./settingsScope";
4
5export function validateSettingsRouteSearch(raw: Record<string, unknown>) {
6 return validateSettingsScopeSearch(
7 typeof raw.environmentId === "string" && raw.machine === undefined && raw.project === undefined
8 ? { ...raw, machine: raw.environmentId }
9 : raw,
10 );
11}
12
13const SCOPE_KEYS = [
14 "project",
15 "machine",
16 "checkout",
17] as const satisfies readonly (keyof SettingsScopeSearch)[];
18const TARGET_INPUT_KEYS = [...SCOPE_KEYS, "environmentId"];
19
20export const retainSettingsScope: SearchMiddleware<SettingsScopeSearch> = ({ search, next }) => {
21 const result = next(search);
22 if (TARGET_INPUT_KEYS.some((key) => Object.hasOwn(result, key))) return result;
23 const previousScope = Object.fromEntries(
24 SCOPE_KEYS.filter((key) => search[key] !== undefined).map((key) => [key, search[key]]),
25 );
26 return { ...previousScope, ...result };
27};