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

_chat.settings.tsx

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