Open History to see the last commit
1import { workspaceAreaProps } from "../lib/workspaceFocus";
2import { Outlet, createFileRoute, redirect, useLocation } from "@tanstack/react-router";
3import { useState, type ReactNode } from "react";
4import { T, useGT, useMessages, Var } from "gt-react";
5import { RotateCcwIcon } from "lucide-react";
6import { Button } from "../components/ui/button";
7import { useSettingsRestore } from "../components/settings/SettingsPanels";
8
9import { AppRouteModal, AppRouteModalClose } from "../components/AppRouteModal";
10import { SettingsBreadcrumb } from "../components/settings/SettingsBreadcrumb";
11import { SettingsSidebarNav } from "../components/settings/SettingsSidebarNav";
12import {
13 SettingsScopeProvider,
14 useSettingsScope,
15} from "../components/settings/SettingsScopeContext";
16import { useEnvironments } from "../state/environments";
17import { SettingsScopeNotice } from "../components/settings/SettingsScopeNotice";
18import { SETTINGS_DEVICE_ONLY_PATHS } from "../components/settings/SettingsScopeSentence";
19import { SettingsPageContainer } from "../components/settings/settingsLayout";
20import {
21 retainSettingsScope,
22 validateSettingsRouteSearch,
23} from "../components/settings/settingsScopeNavigation";
24import {
25 getSettingsSearchTargetScope,
26 getThreadAutoSettlementSearchAvailability,
27 isSettingsSearchScopeAvailable,
28 movedSettingsPath,
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 <span className="max-sm:sr-only">
42 <T>Restore device defaults</T>
43 </span>
44 </Button>
45 );
46}
47
48function SettingsScopeBoundary({ pathname, children }: { pathname: string; children: ReactNode }) {
49 const { scope, connectedEnvironments } = useSettingsScope();
50 const { environments } = useEnvironments();
51 const gt = useGT();
52 const m = useMessages();
53 const hash = useLocation({ select: (location) => location.hash });
54 const searchTarget = getSettingsSearchTargetScope(hash);
55 const agentFeedbackAvailability = searchTarget?.agentFeedbackOnly
56 ? {
57 eligibleEnvironmentIds: environments
58 .filter(
59 (environment) =>
60 environment.connection.phase === "connected" &&
61 environment.serverConfig?.environment.capabilities.agentFeedback === true,
62 )
63 .map((environment) => environment.environmentId),
64 isTargetAvailable:
65 connectedEnvironments.length > 0 &&
66 connectedEnvironments.every(
67 (environment) =>
68 environment.serverConfig?.environment.capabilities.agentFeedback === true,
69 ),
70 }
71 : null;
72 const targetAvailability =
73 agentFeedbackAvailability ??
74 (searchTarget?.requiresThreadAutoSettlement
75 ? getThreadAutoSettlementSearchAvailability(environments, scope)
76 : null);
77 if (
78 scope.kind !== "unavailable" &&
79 searchTarget &&
80 targetAvailability &&
81 !targetAvailability.isTargetAvailable
82 ) {
83 return (
84 <SettingsScopeNotice
85 target="environment"
86 targetId={hash}
87 eligibleEnvironmentIds={targetAvailability.eligibleEnvironmentIds}
88 >
89 {targetAvailability.eligibleEnvironmentIds.length > 0
90 ? gt("{title} requires a supporting environment. Choose one to continue.", {
91 title: m(searchTarget.title),
92 })
93 : gt(
94 "{title} requires a supporting environment. Connect or update an environment to continue.",
95 { title: m(searchTarget.title) },
96 )}
97 </SettingsScopeNotice>
98 );
99 }
100 if (
101 scope.kind !== "unavailable" &&
102 searchTarget &&
103 !isSettingsSearchScopeAvailable(searchTarget.scope, scope.kind)
104 ) {
105 const target =
106 searchTarget.scope === "environment" ||
107 searchTarget.scope === "project" ||
108 searchTarget.scope === "checkout"
109 ? searchTarget.scope
110 : "all";
111 return (
112 <SettingsScopeNotice target={target} targetId={hash}>
113 {gt(
114 "{title} is not available for the selected target. Choose its owning scope to continue.",
115 { title: m(searchTarget.title) },
116 )}
117 </SettingsScopeNotice>
118 );
119 }
120 if (SETTINGS_DEVICE_ONLY_PATHS.has(pathname) || pathname === "/settings/projects") {
121 return children;
122 }
123 if (scope.kind === "unavailable")
124 return (
125 <SettingsPageContainer>
126 <p className="text-sm text-muted-foreground">{scope.message}</p>
127 </SettingsPageContainer>
128 );
129 if (scope.kind === "environment" && connectedEnvironments.length === 0) {
130 return (
131 <SettingsPageContainer>
132 <p className="text-sm text-muted-foreground">
133 <T>
134 Reconnect <Var>{scope.label}</Var> to change its settings.
135 </T>
136 </p>
137 </SettingsPageContainer>
138 );
139 }
140 return children;
141}
142
143function SettingsContentLayout() {
144 const gt = useGT();
145 const contentArea = workspaceAreaProps("settings", 1, gt("Settings content"));
146 const location = useLocation();
147 const { search, environment } = useSettingsScope();
148 const contentScopeKey =
149 location.pathname === "/settings/diagnostics" || location.pathname === "/settings/providers"
150 ? environment?.environmentId
151 : JSON.stringify(search);
152 const [restoreSignal, setRestoreSignal] = useState(0);
153
154 return (
155 <AppRouteModal label={gt("Settings")}>
156 <div className="relative flex min-h-0 flex-1">
157 <SettingsSidebarNav pathname={location.pathname} />
158 <div {...contentArea} className="flex min-h-0 min-w-0 flex-1 flex-col">
159 <header className="flex h-12 shrink-0 items-center gap-3 border-b ps-5 pe-2">
160 <SettingsBreadcrumb pathname={location.pathname} />
161 <div className="ms-auto flex shrink-0 items-center gap-1">
162 {location.pathname === "/settings/general" ? (
163 <RestoreDeviceDefaultsButton
164 onRestored={() => setRestoreSignal((value) => value + 1)}
165 />
166 ) : null}
167 <AppRouteModalClose />
168 </div>
169 </header>
170 <div key={`${contentScopeKey}:${restoreSignal}`} className="min-h-0 flex flex-1 flex-col">
171 <SettingsScopeBoundary pathname={location.pathname}>
172 <Outlet />
173 </SettingsScopeBoundary>
174 </div>
175 </div>
176 </div>
177 </AppRouteModal>
178 );
179}
180
181function SettingsRouteLayout() {
182 const rawSearch = Route.useSearch();
183 const navigate = Route.useNavigate();
184 const pathname = useLocation({ select: (location) => location.pathname });
185 return (
186 <SettingsScopeProvider
187 search={rawSearch}
188 singleEnvironment={pathname === "/settings/providers" || pathname === "/settings/secrets"}
189 deviceOnly={SETTINGS_DEVICE_ONLY_PATHS.has(pathname)}
190 onChange={(next) => {
191 // Send every axis so retain middleware recognizes "all" as an explicit target.
192 void navigate({
193 to: pathname,
194 search: () => ({
195 project: next.project,
196 machine: next.machine,
197 checkout: next.checkout,
198 }),
199 hash: "",
200 resetScroll: false,
201 });
202 }}
203 >
204 <SettingsContentLayout />
205 </SettingsScopeProvider>
206 );
207}
208
209export const Route = createFileRoute("/_chat/settings")({
210 validateSearch: validateSettingsRouteSearch,
211 search: { middlewares: [retainSettingsScope] },
212 beforeLoad: async ({ location }) => {
213 if (location.pathname === "/settings") {
214 throw redirect({ to: "/settings/general", replace: true });
215 }
216 const moved = movedSettingsPath(location.pathname, location.hash);
217 if (moved) throw redirect({ to: moved, search: true, hash: true, replace: true });
218 },
219 component: SettingsRouteLayout,
220});