Open History to see the last commit
1import { Outlet, createFileRoute, redirect, useLocation } from "@tanstack/react-router";
2import { useState, type ReactNode } from "react";
3import { RotateCcwIcon } from "lucide-react";
4import { Button } from "../components/ui/button";
5import { useSettingsRestore } from "../components/settings/SettingsPanels";
6
7import { SettingsBreadcrumb } from "../components/settings/SettingsBreadcrumb";
8import { SidebarInset } from "../components/ui/sidebar";
9import { useNavigateToMainApp } from "../components/sidebar/mainAppLocation";
10import { WorkspacePageHeader } from "../components/WorkspacePageHeader";
11import { isElectron } from "../env";
12import { useEscapeToGoBack } from "../hooks/useNavigateBack";
13import {
14 SettingsScopeProvider,
15 useSettingsScope,
16} from "../components/settings/SettingsScopeContext";
17import { useEnvironments } from "../state/environments";
18import { SettingsScopeNotice } from "../components/settings/SettingsScopeNotice";
19import { SETTINGS_DEVICE_ONLY_PATHS } from "../components/settings/SettingsScopeSentence";
20import { SettingsPageContainer } from "../components/settings/settingsLayout";
21import {
22 retainSettingsScope,
23 validateSettingsRouteSearch,
24} from "../components/settings/settingsScopeNavigation";
25import {
26 getSettingsSearchTargetScope,
27 getThreadAutoSettlementSearchAvailability,
28 isSettingsSearchScopeAvailable,
29} from "../components/settings/settingsSearch";
30
31function RestoreDeviceDefaultsButton({ onRestored }: { onRestored: () => void }) {
32 const { changedSettingLabels, restoreDefaults } = useSettingsRestore(onRestored);
33 return (
34 <Button
35 size="xs"
36 variant="ghost"
37 disabled={changedSettingLabels.length === 0}
38 onClick={() => void restoreDefaults()}
39 >
40 <RotateCcwIcon className="mx-1 size-3.5" />
41 Restore device defaults
42 </Button>
43 );
44}
45
46function SettingsScopeBoundary({ pathname, children }: { pathname: string; children: ReactNode }) {
47 const { scope, connectedEnvironments } = useSettingsScope();
48 const { environments } = useEnvironments();
49 const hash = useLocation({ select: (location) => location.hash });
50 const searchTarget = getSettingsSearchTargetScope(hash);
51 const autoSettlementAvailability = searchTarget?.requiresThreadAutoSettlement
52 ? getThreadAutoSettlementSearchAvailability(environments, scope)
53 : null;
54 if (
55 scope.kind !== "unavailable" &&
56 searchTarget &&
57 autoSettlementAvailability &&
58 !autoSettlementAvailability.isTargetAvailable
59 ) {
60 return (
61 <SettingsScopeNotice
62 target="environment"
63 targetId={hash}
64 eligibleEnvironmentIds={autoSettlementAvailability.eligibleEnvironmentIds}
65 >
66 {autoSettlementAvailability.eligibleEnvironmentIds.length > 0
67 ? `${searchTarget.title} requires a supporting environment. Choose one to continue.`
68 : `${searchTarget.title} requires a supporting environment. Connect or update an environment to continue.`}
69 </SettingsScopeNotice>
70 );
71 }
72 if (
73 scope.kind !== "unavailable" &&
74 searchTarget &&
75 !isSettingsSearchScopeAvailable(searchTarget.scope, scope.kind)
76 ) {
77 const target =
78 searchTarget.scope === "environment" ||
79 searchTarget.scope === "project" ||
80 searchTarget.scope === "checkout"
81 ? searchTarget.scope
82 : "all";
83 return (
84 <SettingsScopeNotice target={target} targetId={hash}>
85 {`${searchTarget.title} is not available for the selected target. Choose its owning scope to continue.`}
86 </SettingsScopeNotice>
87 );
88 }
89 if (SETTINGS_DEVICE_ONLY_PATHS.has(pathname) || pathname === "/settings/projects") {
90 return children;
91 }
92 if (scope.kind === "unavailable")
93 return (
94 <SettingsPageContainer>
95 <p className="text-sm text-muted-foreground">{scope.message}</p>
96 </SettingsPageContainer>
97 );
98 if (scope.kind === "environment" && connectedEnvironments.length === 0) {
99 return (
100 <SettingsPageContainer>
101 <p className="text-sm text-muted-foreground">
102 Reconnect {scope.label} to change its settings.
103 </p>
104 </SettingsPageContainer>
105 );
106 }
107 return children;
108}
109
110function SettingsContentLayout() {
111 const location = useLocation();
112 const navigateToMainApp = useNavigateToMainApp();
113 useEscapeToGoBack(navigateToMainApp);
114 const { search, environment } = useSettingsScope();
115 const contentScopeKey =
116 location.pathname === "/settings/diagnostics" || location.pathname === "/settings/providers"
117 ? environment?.environmentId
118 : JSON.stringify(search);
119 const [restoreSignal, setRestoreSignal] = useState(0);
120
121 return (
122 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none isolate">
123 <div className="flex min-h-0 min-w-0 flex-1 flex-col bg-background text-foreground">
124 <WorkspacePageHeader electron={isElectron}>
125 <div className="flex w-full items-center gap-3">
126 <SettingsBreadcrumb pathname={location.pathname} />
127 {location.pathname === "/settings/general" ? (
128 <div className="ms-auto flex shrink-0 items-center">
129 <RestoreDeviceDefaultsButton
130 onRestored={() => setRestoreSignal((value) => value + 1)}
131 />
132 </div>
133 ) : null}
134 </div>
135 </WorkspacePageHeader>
136
137 <div key={`${contentScopeKey}:${restoreSignal}`} className="min-h-0 flex flex-1 flex-col">
138 <SettingsScopeBoundary pathname={location.pathname}>
139 <Outlet />
140 </SettingsScopeBoundary>
141 </div>
142 </div>
143 </SidebarInset>
144 );
145}
146
147function SettingsRouteLayout() {
148 const rawSearch = Route.useSearch();
149 const navigate = Route.useNavigate();
150 const pathname = useLocation({ select: (location) => location.pathname });
151 return (
152 <SettingsScopeProvider
153 search={rawSearch}
154 singleEnvironment={pathname === "/settings/providers"}
155 onChange={(next) => {
156 // Send every axis so the retain middleware sees an explicit target
157 // even when the choice is "all", which is the absence of a key.
158 void navigate({
159 to: pathname,
160 search: () => ({
161 project: next.project,
162 machine: next.machine,
163 checkout: next.checkout,
164 }),
165 hash: "",
166 resetScroll: false,
167 });
168 }}
169 >
170 <SettingsContentLayout />
171 </SettingsScopeProvider>
172 );
173}
174
175export const Route = createFileRoute("/settings")({
176 validateSearch: validateSettingsRouteSearch,
177 search: { middlewares: [retainSettingsScope] },
178 beforeLoad: async ({ context, location }) => {
179 if (
180 context.authGateState.status !== "authenticated" &&
181 context.authGateState.status !== "hosted-static"
182 ) {
183 throw redirect({ to: "/pair", replace: true });
184 }
185
186 if (location.pathname === "/settings") {
187 throw redirect({ to: "/settings/general", replace: true });
188 }
189 },
190 component: SettingsRouteLayout,
191});