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

settings.tsx

211 lines7.3 KB

1import { Outlet, createFileRoute, redirect, useLocation } from "@tanstack/react-router";
2import { useEffect, 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 {
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} from "../components/settings/settingsSearch";
29​
30function RestoreDeviceDefaultsButton({ onRestored }: { onRestored: () => void }) {
31 const { changedSettingLabels, restoreDefaults } = useSettingsRestore(onRestored);
32 return (
33 <Button
34 size="xs"
35 variant="ghost"
36 disabled={changedSettingLabels.length === 0}
37 onClick={() => void restoreDefaults()}
38 >
39 <RotateCcwIcon className="mx-1 size-3.5" />
40 Restore device defaults
41 </Button>
42 );
44​
45function SettingsScopeBoundary({ pathname, children }: { pathname: string; children: ReactNode }) {
46 const { scope, connectedEnvironments } = useSettingsScope();
47 const { environments } = useEnvironments();
48 const hash = useLocation({ select: (location) => location.hash });
49 const searchTarget = getSettingsSearchTargetScope(hash);
50 const autoSettlementAvailability = searchTarget?.requiresThreadAutoSettlement
51 ? getThreadAutoSettlementSearchAvailability(environments, scope)
52 : null;
53 if (
54 scope.kind !== "unavailable" &&
55 searchTarget &&
56 autoSettlementAvailability &&
57 !autoSettlementAvailability.isTargetAvailable
58 ) {
59 return (
60 <SettingsScopeNotice
61 target="environment"
62 targetId={hash}
63 eligibleEnvironmentIds={autoSettlementAvailability.eligibleEnvironmentIds}
64 >
65 {autoSettlementAvailability.eligibleEnvironmentIds.length > 0
66 ? `${searchTarget.title} requires a supporting environment. Choose one to continue.`
67 : `${searchTarget.title} requires a supporting environment. Connect or update an environment to continue.`}
68 </SettingsScopeNotice>
69 );
70 }
71 if (
72 scope.kind !== "unavailable" &&
73 searchTarget &&
74 !isSettingsSearchScopeAvailable(searchTarget.scope, scope.kind)
75 ) {
76 const target =
77 searchTarget.scope === "environment" ||
78 searchTarget.scope === "project" ||
79 searchTarget.scope === "checkout"
80 ? searchTarget.scope
81 : "all";
82 return (
83 <SettingsScopeNotice target={target} targetId={hash}>
84 {`${searchTarget.title} is not available for the selected target. Choose its owning scope to continue.`}
85 </SettingsScopeNotice>
86 );
87 }
88 // Device-local pages ignore the scope entirely; the project page follows
89 // remembered members while a grouping change replaces its URL key.
90 if (SETTINGS_DEVICE_ONLY_PATHS.has(pathname) || pathname === "/settings/projects") {
91 return children;
92 }
93 // Keep the scope sentence on screen so the selection can be changed back.
94 if (scope.kind === "unavailable")
95 return (
96 <SettingsPageContainer>
97 <p className="text-sm text-muted-foreground">{scope.message}</p>
98 </SettingsPageContainer>
99 );
100 if (scope.kind === "environment" && connectedEnvironments.length === 0) {
101 return (
102 <SettingsPageContainer>
103 <p className="text-sm text-muted-foreground">
104 Reconnect {scope.label} to change its settings.
105 </p>
106 </SettingsPageContainer>
107 );
108 }
109 return children;
111​
112function SettingsContentLayout() {
113 const location = useLocation();
114 const navigateToMainApp = useNavigateToMainApp();
115 const { search } = useSettingsScope();
116 const [restoreSignal, setRestoreSignal] = useState(0);
117​
118 useEffect(() => {
119 const onKeyDown = (event: KeyboardEvent) => {
120 if (event.defaultPrevented) return;
121 if (event.key === "Escape") {
122 event.preventDefault();
123​
124 const activeElement = document.activeElement;
125 if (activeElement instanceof HTMLElement) {
126 activeElement.blur();
127 }
128​
129 void navigateToMainApp();
130 }
131 };
132​
133 window.addEventListener("keydown", onKeyDown);
134 return () => {
135 window.removeEventListener("keydown", onKeyDown);
136 };
137 }, [navigateToMainApp]);
138​
139 return (
140 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none isolate">
141 <div className="flex min-h-0 min-w-0 flex-1 flex-col bg-background text-foreground">
142 <WorkspacePageHeader electron={isElectron}>
143 <div className="flex w-full items-center gap-3">
144 <SettingsBreadcrumb pathname={location.pathname} />
145 {location.pathname === "/settings/general" ? (
146 <div className="ms-auto flex shrink-0 items-center">
147 <RestoreDeviceDefaultsButton
148 onRestored={() => setRestoreSignal((value) => value + 1)}
149 />
150 </div>
151 ) : null}
152 </div>
153 </WorkspacePageHeader>
154​
155 <div
156 key={`${JSON.stringify(search)}:${restoreSignal}`}
157 className="min-h-0 flex flex-1 flex-col"
158 >
159 <SettingsScopeBoundary pathname={location.pathname}>
160 <Outlet />
161 </SettingsScopeBoundary>
162 </div>
163 </div>
164 </SidebarInset>
165 );
167​
168function SettingsRouteLayout() {
169 const rawSearch = Route.useSearch();
170 const navigate = Route.useNavigate();
171 const pathname = useLocation({ select: (location) => location.pathname });
172 return (
173 <SettingsScopeProvider
174 search={rawSearch}
175 onChange={(next) => {
176 // Send every axis so the retain middleware sees an explicit target
177 // even when the choice is "all", which is the absence of a key.
178 void navigate({
179 to: pathname,
180 search: () => ({
181 project: next.project,
182 machine: next.machine,
183 checkout: next.checkout,
184 }),
185 hash: "",
186 resetScroll: false,
187 });
188 }}
189 >
190 <SettingsContentLayout />
191 </SettingsScopeProvider>
192 );
194​
195export const Route = createFileRoute("/settings")({
196 validateSearch: validateSettingsRouteSearch,
197 search: { middlewares: [retainSettingsScope] },
198 beforeLoad: async ({ context, location }) => {
199 if (
200 context.authGateState.status !== "authenticated" &&
201 context.authGateState.status !== "hosted-static"
202 ) {
203 throw redirect({ to: "/pair", replace: true });
204 }
205​
206 if (location.pathname === "/settings") {
207 throw redirect({ to: "/settings/general", replace: true });
208 }
209 },
210 component: SettingsRouteLayout,
211});