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