Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

_chat.settings.tsx

228 lines8.4 KB

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 );
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} requires a supporting environment. Choose one to continue.", {
95 title: m(searchTarget.title),
96 })
97 : gt(
98 "{title} requires a supporting environment. Connect or update an environment to continue.",
99 { title: m(searchTarget.title) },
100 )}
101 </SettingsScopeNotice>
102 );
103 }
104 if (
105 scope.kind !== "unavailable" &&
106 searchTarget &&
107 !isSettingsSearchScopeAvailable(searchTarget.scope, scope.kind)
108 ) {
109 const target =
110 searchTarget.scope === "environment" ||
111 searchTarget.scope === "project" ||
112 searchTarget.scope === "checkout"
113 ? searchTarget.scope
114 : "all";
115 return (
116 <SettingsScopeNotice target={target} targetId={hash}>
117 {gt(
118 "{title} is not available for the selected target. Choose its owning scope to continue.",
119 { title: m(searchTarget.title) },
120 )}
121 </SettingsScopeNotice>
122 );
123 }
124 if (SETTINGS_DEVICE_ONLY_PATHS.has(pathname) || pathname === "/settings/projects") {
125 return children;
126 }
127 if (scope.kind === "unavailable")
128 return (
129 <SettingsPageContainer>
130 <p className="text-sm text-muted-foreground">{scope.message}</p>
131 </SettingsPageContainer>
132 );
133 if (scope.kind === "environment" && connectedEnvironments.length === 0) {
134 return (
135 <SettingsPageContainer>
136 <p className="text-sm text-muted-foreground">
137 <T>
138 Reconnect <Var>{scope.label}</Var> to change its settings.
139 </T>
140 </p>
141 </SettingsPageContainer>
142 );
143 }
144 return children;
146​
147function SettingsContentLayout() {
148 const gt = useGT();
149 const contentArea = workspaceAreaProps("settings", 1, gt("Settings content"));
150 const navPathname = useLocation({ select: (location) => location.pathname });
151 const pathname = useRenderedSettingsPathname();
152 const { search, environment } = useSettingsScope();
153 const contentScopeKey =
154 pathname === "/settings/diagnostics" || pathname === "/settings/providers"
155 ? environment?.environmentId
156 : JSON.stringify(search);
157 const [restoreSignal, setRestoreSignal] = useState(0);
158​
159 return (
160 <AppRouteModal label={gt("Settings")}>
161 <div className="relative flex min-h-0 flex-1">
162 <SettingsSidebarNav pathname={navPathname} />
163 <div {...contentArea} className="flex min-h-0 min-w-0 flex-1 flex-col">
164 <header className="flex h-12 shrink-0 items-center gap-3 border-b ps-5 pe-2">
165 <SettingsBreadcrumb pathname={pathname} />
166 <div className="ms-auto flex shrink-0 items-center gap-1">
167 {pathname === "/settings/general" ? (
168 <RestoreDeviceDefaultsButton
169 onRestored={() => setRestoreSignal((value) => value + 1)}
170 />
171 ) : null}
172 <AppRouteModalClose />
173 </div>
174 </header>
175 <div key={`${contentScopeKey}:${restoreSignal}`} className="min-h-0 flex flex-1 flex-col">
176 <SettingsScopeBoundary pathname={pathname}>
177 <Outlet />
178 </SettingsScopeBoundary>
179 </div>
180 </div>
181 </div>
182 </AppRouteModal>
183 );
185​
186function SettingsRouteLayout() {
187 const rawSearch = Route.useSearch();
188 const navigate = Route.useNavigate();
189 const router = useRouter();
190 const pathname = useRenderedSettingsPathname();
191 return (
192 <SettingsScopeProvider
193 search={rawSearch}
194 singleEnvironment={pathname === "/settings/providers" || pathname === "/settings/secrets"}
195 deviceOnly={SETTINGS_DEVICE_ONLY_PATHS.has(pathname)}
196 onChange={(next) => {
197 // Send every axis so retain middleware recognizes "all" as an explicit target.
198 void navigate({
199 // Read at call time: subscribing would hand every scope consumer a new context value on
200 // each click. A pending navigation's target wins over the page still on screen.
201 to: router.state.location.pathname,
202 search: () => ({
203 project: next.project,
204 machine: next.machine,
205 checkout: next.checkout,
206 }),
207 hash: "",
208 resetScroll: false,
209 });
210 }}
211 >
212 <SettingsContentLayout />
213 </SettingsScopeProvider>
214 );
216​
217export const Route = createFileRoute("/_chat/settings")({
218 validateSearch: validateSettingsRouteSearch,
219 search: { middlewares: [retainSettingsScope] },
220 beforeLoad: async ({ location }) => {
221 if (location.pathname === "/settings") {
222 throw redirect({ to: "/settings/general", replace: true });
223 }
224 const moved = movedSettingsPath(location.pathname, location.hash);
225 if (moved) throw redirect({ to: moved, search: true, hash: true, replace: true });
226 },
227 component: SettingsRouteLayout,
228});