Files
t3code/apps/web/src/components/settings/settingsScopeNavigation.ts
T

33 lines
1.2 KiB
TypeScript

import type { SearchMiddleware } from "@tanstack/react-router";
import { validateSettingsScopeSearch, type SettingsScopeSearch } from "./settingsScope";
/** Accept legacy provider links without replacing an explicit settings scope. */
export function validateSettingsRouteSearch(raw: Record<string, unknown>) {
return validateSettingsScopeSearch(
typeof raw.environmentId === "string" && raw.machine === undefined && raw.project === undefined
? { ...raw, machine: raw.environmentId }
: raw,
);
}
const SCOPE_KEYS = [
"project",
"machine",
"checkout",
] as const satisfies readonly (keyof SettingsScopeSearch)[];
const TARGET_INPUT_KEYS = [...SCOPE_KEYS, "environmentId"];
/**
* Category links keep the target, while an explicit target replaces the entire
* previous selection. `environmentId` is the legacy provider deep-link target.
*/
export const retainSettingsScope: SearchMiddleware<SettingsScopeSearch> = ({ search, next }) => {
const result = next(search);
if (TARGET_INPUT_KEYS.some((key) => Object.hasOwn(result, key))) return result;
const previousScope = Object.fromEntries(
SCOPE_KEYS.filter((key) => search[key] !== undefined).map((key) => [key, search[key]]),
);
return { ...previousScope, ...result };
};