Open History to see the last commit
1import { workspaceAreaProps } from "../lib/workspaceFocus";
2import { Outlet, createFileRoute, redirect, useLocation, useRouter } 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 { useRenderedSettingsPathname } from "../components/settings/useRenderedSettingsPathname";
21import {
22 retainSettingsScope,
23 validateSettingsRouteSearch,
24} from "../components/settings/settingsScopeNavigation";
25import {
26 getSettingsSearchTargetScope,
27 getThreadAutoSettlementSearchAvailability,
28 isSettingsSearchScopeAvailable,
29 movedSettingsPath,
30} from "../components/settings/settingsSearch";
31
32function RestoreDeviceDefaultsButton({ onRestored }: { onRestored: () => void }) {
33 const { changedSettingLabels, restoreDefaults } = useSettingsRestore(onRestored);
34 return (
35 <Button
36 size="xs"
37 variant="ghost"
38 disabled={changedSettingLabels.length === 0}
39 onClick={() => void restoreDefaults()}
40 >
41 <RotateCcwIcon className="size-3.5" />
42 <span className="max-sm:sr-only">
43 <T>Restore device defaults</T>
44 </span>
45 </Button>
46 );
47}
48
49function SettingsScopeBoundary({ pathname, children }: { pathname: string; children: ReactNode }) {
50 const { scope, connectedEnvironments } = useSettingsScope();
51 const { environments } = useEnvironments();
52 const gt = useGT();
53 const m = useMessages();
54 // A pending navigation's hash targets the page still loading, not the one on screen.
55 const hash = useLocation({
56 select: (location) => (location.pathname === pathname ? location.hash : ""),
57 });
58 const searchTarget = getSettingsSearchTargetScope(hash);
59 const agentFeedbackAvailability = searchTarget?.agentFeedbackOnly
60 ? {
61 eligibleEnvironmentIds: environments
62 .filter(
63 (environment) =>
64 environment.connection.phase === "connected" &&
65 environment.serverConfig?.environment.capabilities.agentFeedback === true,
66 )
67 .map((environment) => environment.environmentId),
68 isTargetAvailable:
69 connectedEnvironments.length > 0 &&
70 connectedEnvironments.every(
71 (environment) =>
72 environment.serverConfig?.environment.capabilities.agentFeedback === true,
73 ),
74 }
75 : null;
76 const targetAvailability =
77 agentFeedbackAvailability ??
78 (searchTarget?.requiresThreadAutoSettlement
79 ? getThreadAutoSettlementSearchAvailability(environments, scope)
80 : null);
81 if (
82 scope.kind !== "unavailable" &&
83 searchTarget &&
84 targetAvailability &&
85 !targetAvailability.isTargetAvailable
86 ) {
87 return (
88 <SettingsScopeNotice
89 target="environment"
90 targetId={hash}
91 eligibleEnvironmentIds={targetAvailability.eligibleEnvironmentIds}
92 >
93 {targetAvailability.eligibleEnvironmentIds.length > 0
94 ? gt`${{ title: m(searchTarget.title) }} requires a supporting environment. Choose one to continue.`
95 : gt`${{ title: m(searchTarget.title) }} requires a supporting environment. Connect or update an environment to continue.`}
96 </SettingsScopeNotice>
97 );
98 }
99 if (
100 scope.kind !== "unavailable" &&
101 searchTarget &&
102 !isSettingsSearchScopeAvailable(searchTarget.scope, scope.kind)
103 ) {
104 const target =
105 searchTarget.scope === "environment" ||
106 searchTarget.scope === "project" ||
107 searchTarget.scope === "checkout"
108 ? searchTarget.scope
109 : "all";
110 return (
111 <SettingsScopeNotice target={target} targetId={hash}>
112 {gt`${{ title: m(searchTarget.title) }} is not available for the selected target. Choose its owning scope to continue.`}
113 </SettingsScopeNotice>
114 );
115 }
116 if (SETTINGS_DEVICE_ONLY_PATHS.has(pathname) || pathname === "/settings/projects") {
117 return children;
118 }
119 if (scope.kind === "unavailable")
120 return (
121 <SettingsPageContainer>
122 <p className="text-sm text-muted-foreground">{scope.message}</p>
123 </SettingsPageContainer>
124 );
125 if (scope.kind === "environment" && connectedEnvironments.length === 0) {
126 return (
127 <SettingsPageContainer>
128 <p className="text-sm text-muted-foreground">
129 <T>
130 Reconnect <Var>{scope.label}</Var> to change its settings.
131 </T>
132 </p>
133 </SettingsPageContainer>
134 );
135 }
136 return children;
137}
138
139function SettingsContentLayout() {
140 const gt = useGT();
141 const contentArea = workspaceAreaProps("settings", 1, gt`Settings content`);
142 const navPathname = useLocation({ select: (location) => location.pathname });
143 const pathname = useRenderedSettingsPathname();
144 const { search, environment } = useSettingsScope();
145 const contentScopeKey =
146 pathname === "/settings/diagnostics" || pathname === "/settings/providers"
147 ? environment?.environmentId
148 : JSON.stringify(search);
149 const [restoreSignal, setRestoreSignal] = useState(0);
150
151 return (
152 <AppRouteModal label={gt`Settings`}>
153 <div className="relative flex min-h-0 flex-1">
154 <SettingsSidebarNav pathname={navPathname} />
155 <div {...contentArea} className="flex min-h-0 min-w-0 flex-1 flex-col">
156 <header className="flex h-12 shrink-0 items-center gap-3 border-b ps-5 pe-2">
157 <SettingsBreadcrumb pathname={pathname} />
158 <div className="ms-auto flex shrink-0 items-center gap-1">
159 {pathname === "/settings/general" ? (
160 <RestoreDeviceDefaultsButton
161 onRestored={() => setRestoreSignal((value) => value + 1)}
162 />
163 ) : null}
164 <AppRouteModalClose />
165 </div>
166 </header>
167 <div key={`${contentScopeKey}:${restoreSignal}`} className="min-h-0 flex flex-1 flex-col">
168 <SettingsScopeBoundary pathname={pathname}>
169 <Outlet />
170 </SettingsScopeBoundary>
171 </div>
172 </div>
173 </div>
174 </AppRouteModal>
175 );
176}
177
178function SettingsRouteLayout() {
179 const rawSearch = Route.useSearch();
180 const navigate = Route.useNavigate();
181 const router = useRouter();
182 const pathname = useRenderedSettingsPathname();
183 return (
184 <SettingsScopeProvider
185 search={rawSearch}
186 singleEnvironment={pathname === "/settings/providers" || pathname === "/settings/secrets"}
187 deviceOnly={SETTINGS_DEVICE_ONLY_PATHS.has(pathname)}
188 onChange={(next) => {
189 // Send every axis so retain middleware recognizes "all" as an explicit target.
190 void navigate({
191 // Read at call time: subscribing would hand every scope consumer a new context value on
192 // each click. A pending navigation's target wins over the page still on screen.
193 to: router.state.location.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});