The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { workspaceAreaProps } from "../lib/workspaceFocus"; |
| 2 | import { Outlet, createFileRoute, redirect, useLocation } from "@tanstack/react-router"; |
| 3 | import { useState, type ReactNode } from "react"; |
| 4 | import { T, useGT, useMessages, Var } from "gt-react"; |
| 5 | import { RotateCcwIcon } from "lucide-react"; |
| 6 | import { Button } from "../components/ui/button"; |
| 7 | import { useSettingsRestore } from "../components/settings/SettingsPanels"; |
| 8 | |
| 9 | import { AppRouteModal, AppRouteModalClose } from "../components/AppRouteModal"; |
| 10 | import { SettingsBreadcrumb } from "../components/settings/SettingsBreadcrumb"; |
| 11 | import { SettingsSidebarNav } from "../components/settings/SettingsSidebarNav"; |
| 12 | import { |
| 13 | SettingsScopeProvider, |
| 14 | useSettingsScope, |
| 15 | } from "../components/settings/SettingsScopeContext"; |
| 16 | import { useEnvironments } from "../state/environments"; |
| 17 | import { SettingsScopeNotice } from "../components/settings/SettingsScopeNotice"; |
| 18 | import { SETTINGS_DEVICE_ONLY_PATHS } from "../components/settings/SettingsScopeSentence"; |
| 19 | import { SettingsPageContainer } from "../components/settings/settingsLayout"; |
| 20 | import { |
| 21 | retainSettingsScope, |
| 22 | validateSettingsRouteSearch, |
| 23 | } from "../components/settings/settingsScopeNavigation"; |
| 24 | import { |
| 25 | getSettingsSearchTargetScope, |
| 26 | getThreadAutoSettlementSearchAvailability, |
| 27 | isSettingsSearchScopeAvailable, |
| 28 | movedSettingsPath, |
| 29 | } from "../components/settings/settingsSearch"; |
| 30 | |
| 31 | function 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 | <span className="max-sm:sr-only"> |
| 42 | <T>Restore device defaults</T> |
| 43 | </span> |
| 44 | </Button> |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | function SettingsScopeBoundary({ pathname, children }: { pathname: string; children: ReactNode }) { |
| 49 | const { scope, connectedEnvironments } = useSettingsScope(); |
| 50 | const { environments } = useEnvironments(); |
| 51 | const gt = useGT(); |
| 52 | const m = useMessages(); |
| 53 | const hash = useLocation({ select: (location) => location.hash }); |
| 54 | const searchTarget = getSettingsSearchTargetScope(hash); |
| 55 | const autoSettlementAvailability = searchTarget?.requiresThreadAutoSettlement |
| 56 | ? getThreadAutoSettlementSearchAvailability(environments, scope) |
| 57 | : null; |
| 58 | if ( |
| 59 | scope.kind !== "unavailable" && |
| 60 | searchTarget && |
| 61 | autoSettlementAvailability && |
| 62 | !autoSettlementAvailability.isTargetAvailable |
| 63 | ) { |
| 64 | return ( |
| 65 | <SettingsScopeNotice |
| 66 | target="environment" |
| 67 | targetId={hash} |
| 68 | eligibleEnvironmentIds={autoSettlementAvailability.eligibleEnvironmentIds} |
| 69 | > |
| 70 | {autoSettlementAvailability.eligibleEnvironmentIds.length > 0 |
| 71 | ? gt("{title} requires a supporting environment. Choose one to continue.", { |
| 72 | title: m(searchTarget.title), |
| 73 | }) |
| 74 | : gt( |
| 75 | "{title} requires a supporting environment. Connect or update an environment to continue.", |
| 76 | { title: m(searchTarget.title) }, |
| 77 | )} |
| 78 | </SettingsScopeNotice> |
| 79 | ); |
| 80 | } |
| 81 | if ( |
| 82 | scope.kind !== "unavailable" && |
| 83 | searchTarget && |
| 84 | !isSettingsSearchScopeAvailable(searchTarget.scope, scope.kind) |
| 85 | ) { |
| 86 | const target = |
| 87 | searchTarget.scope === "environment" || |
| 88 | searchTarget.scope === "project" || |
| 89 | searchTarget.scope === "checkout" |
| 90 | ? searchTarget.scope |
| 91 | : "all"; |
| 92 | return ( |
| 93 | <SettingsScopeNotice target={target} targetId={hash}> |
| 94 | {gt( |
| 95 | "{title} is not available for the selected target. Choose its owning scope to continue.", |
| 96 | { title: m(searchTarget.title) }, |
| 97 | )} |
| 98 | </SettingsScopeNotice> |
| 99 | ); |
| 100 | } |
| 101 | if (SETTINGS_DEVICE_ONLY_PATHS.has(pathname) || pathname === "/settings/projects") { |
| 102 | return children; |
| 103 | } |
| 104 | if (scope.kind === "unavailable") |
| 105 | return ( |
| 106 | <SettingsPageContainer> |
| 107 | <p className="text-sm text-muted-foreground">{scope.message}</p> |
| 108 | </SettingsPageContainer> |
| 109 | ); |
| 110 | if (scope.kind === "environment" && connectedEnvironments.length === 0) { |
| 111 | return ( |
| 112 | <SettingsPageContainer> |
| 113 | <p className="text-sm text-muted-foreground"> |
| 114 | <T> |
| 115 | Reconnect <Var>{scope.label}</Var> to change its settings. |
| 116 | </T> |
| 117 | </p> |
| 118 | </SettingsPageContainer> |
| 119 | ); |
| 120 | } |
| 121 | return children; |
| 122 | } |
| 123 | |
| 124 | function SettingsContentLayout() { |
| 125 | const gt = useGT(); |
| 126 | const contentArea = workspaceAreaProps("settings", 1, gt("Settings content")); |
| 127 | const location = useLocation(); |
| 128 | const { search, environment } = useSettingsScope(); |
| 129 | const contentScopeKey = |
| 130 | location.pathname === "/settings/diagnostics" || location.pathname === "/settings/providers" |
| 131 | ? environment?.environmentId |
| 132 | : JSON.stringify(search); |
| 133 | const [restoreSignal, setRestoreSignal] = useState(0); |
| 134 | |
| 135 | return ( |
| 136 | <AppRouteModal label={gt("Settings")}> |
| 137 | <div className="relative flex min-h-0 flex-1"> |
| 138 | <SettingsSidebarNav pathname={location.pathname} /> |
| 139 | <div {...contentArea} className="flex min-h-0 min-w-0 flex-1 flex-col"> |
| 140 | <header className="flex h-12 shrink-0 items-center gap-3 border-b ps-5 pe-2"> |
| 141 | <SettingsBreadcrumb pathname={location.pathname} /> |
| 142 | <div className="ms-auto flex shrink-0 items-center gap-1"> |
| 143 | {location.pathname === "/settings/general" ? ( |
| 144 | <RestoreDeviceDefaultsButton |
| 145 | onRestored={() => setRestoreSignal((value) => value + 1)} |
| 146 | /> |
| 147 | ) : null} |
| 148 | <AppRouteModalClose /> |
| 149 | </div> |
| 150 | </header> |
| 151 | <div key={`${contentScopeKey}:${restoreSignal}`} className="min-h-0 flex flex-1 flex-col"> |
| 152 | <SettingsScopeBoundary pathname={location.pathname}> |
| 153 | <Outlet /> |
| 154 | </SettingsScopeBoundary> |
| 155 | </div> |
| 156 | </div> |
| 157 | </div> |
| 158 | </AppRouteModal> |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | function SettingsRouteLayout() { |
| 163 | const rawSearch = Route.useSearch(); |
| 164 | const navigate = Route.useNavigate(); |
| 165 | const pathname = useLocation({ select: (location) => location.pathname }); |
| 166 | return ( |
| 167 | <SettingsScopeProvider |
| 168 | search={rawSearch} |
| 169 | singleEnvironment={pathname === "/settings/providers" || pathname === "/settings/secrets"} |
| 170 | deviceOnly={SETTINGS_DEVICE_ONLY_PATHS.has(pathname)} |
| 171 | onChange={(next) => { |
| 172 | // Send every axis so retain middleware recognizes "all" as an explicit target. |
| 173 | void navigate({ |
| 174 | to: pathname, |
| 175 | search: () => ({ |
| 176 | project: next.project, |
| 177 | machine: next.machine, |
| 178 | checkout: next.checkout, |
| 179 | }), |
| 180 | hash: "", |
| 181 | resetScroll: false, |
| 182 | }); |
| 183 | }} |
| 184 | > |
| 185 | <SettingsContentLayout /> |
| 186 | </SettingsScopeProvider> |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | export const Route = createFileRoute("/_chat/settings")({ |
| 191 | validateSearch: validateSettingsRouteSearch, |
| 192 | search: { middlewares: [retainSettingsScope] }, |
| 193 | beforeLoad: async ({ location }) => { |
| 194 | if (location.pathname === "/settings") { |
| 195 | throw redirect({ to: "/settings/general", replace: true }); |
| 196 | } |
| 197 | const moved = movedSettingsPath(location.pathname, location.hash); |
| 198 | if (moved) throw redirect({ to: moved, search: true, hash: true, replace: true }); |
| 199 | }, |
| 200 | component: SettingsRouteLayout, |
| 201 | }); |