Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

settingsScopeNavigation.ts

32 lines1.1 KB

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