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

settings.tsx

193 lines6.9 KB

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 );
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 // Device-local pages ignore the scope entirely; the project page follows
90 // remembered members while a grouping change replaces its URL key.
91 if (SETTINGS_DEVICE_ONLY_PATHS.has(pathname) || pathname === "/settings/projects") {
92 return children;
93 }
94 // Keep the scope sentence on screen so the selection can be changed back.
95 if (scope.kind === "unavailable")
96 return (
97 <SettingsPageContainer>
98 <p className="text-sm text-muted-foreground">{scope.message}</p>
99 </SettingsPageContainer>
100 );
101 if (scope.kind === "environment" && connectedEnvironments.length === 0) {
102 return (
103 <SettingsPageContainer>
104 <p className="text-sm text-muted-foreground">
105 Reconnect {scope.label} to change its settings.
106 </p>
107 </SettingsPageContainer>
108 );
109 }
110 return children;
112​
113function SettingsContentLayout() {
114 const location = useLocation();
115 const navigateToMainApp = useNavigateToMainApp();
116 useEscapeToGoBack(navigateToMainApp);
117 const { search } = useSettingsScope();
118 const [restoreSignal, setRestoreSignal] = useState(0);
119​
120 return (
121 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none isolate">
122 <div className="flex min-h-0 min-w-0 flex-1 flex-col bg-background text-foreground">
123 <WorkspacePageHeader electron={isElectron}>
124 <div className="flex w-full items-center gap-3">
125 <SettingsBreadcrumb pathname={location.pathname} />
126 {location.pathname === "/settings/general" ? (
127 <div className="ms-auto flex shrink-0 items-center">
128 <RestoreDeviceDefaultsButton
129 onRestored={() => setRestoreSignal((value) => value + 1)}
130 />
131 </div>
132 ) : null}
133 </div>
134 </WorkspacePageHeader>
135​
136 <div
137 key={`${JSON.stringify(search)}:${restoreSignal}`}
138 className="min-h-0 flex flex-1 flex-col"
139 >
140 <SettingsScopeBoundary pathname={location.pathname}>
141 <Outlet />
142 </SettingsScopeBoundary>
143 </div>
144 </div>
145 </SidebarInset>
146 );
148​
149function SettingsRouteLayout() {
150 const rawSearch = Route.useSearch();
151 const navigate = Route.useNavigate();
152 const pathname = useLocation({ select: (location) => location.pathname });
153 return (
154 <SettingsScopeProvider
155 search={rawSearch}
156 singleEnvironment={pathname === "/settings/providers"}
157 onChange={(next) => {
158 // Send every axis so the retain middleware sees an explicit target
159 // even when the choice is "all", which is the absence of a key.
160 void navigate({
161 to: pathname,
162 search: () => ({
163 project: next.project,
164 machine: next.machine,
165 checkout: next.checkout,
166 }),
167 hash: "",
168 resetScroll: false,
169 });
170 }}
171 >
172 <SettingsContentLayout />
173 </SettingsScopeProvider>
174 );
176​
177export const Route = createFileRoute("/settings")({
178 validateSearch: validateSettingsRouteSearch,
179 search: { middlewares: [retainSettingsScope] },
180 beforeLoad: async ({ context, location }) => {
181 if (
182 context.authGateState.status !== "authenticated" &&
183 context.authGateState.status !== "hosted-static"
184 ) {
185 throw redirect({ to: "/pair", replace: true });
186 }
187​
188 if (location.pathname === "/settings") {
189 throw redirect({ to: "/settings/general", replace: true });
190 }
191 },
192 component: SettingsRouteLayout,
193});