Open History to see the last commit
1import { RotateCcwIcon } from "lucide-react";
2import {
3 Outlet,
4 createFileRoute,
5 redirect,
6 useCanGoBack,
7 useLocation,
8 useNavigate,
9} from "@tanstack/react-router";
10import { useCallback, useEffect, useState } from "react";
11
12import { useSettingsRestore } from "../components/settings/SettingsPanels";
13import { Button } from "../components/ui/button";
14import { SidebarInset } from "../components/ui/sidebar";
15import { isElectron } from "../env";
16import { cn } from "~/lib/utils";
17import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
18
19function RestoreDefaultsButton({ onRestored }: { onRestored: () => void }) {
20 const { changedSettingLabels, restoreDefaults } = useSettingsRestore(onRestored);
21
22 return (
23 <Button
24 size="xs"
25 variant="outline"
26 disabled={changedSettingLabels.length === 0}
27 onClick={() => void restoreDefaults()}
28 >
29 <RotateCcwIcon className="mx-1 size-3.5" />
30 Restore defaults
31 </Button>
32 );
33}
34
35function SettingsContentLayout() {
36 const location = useLocation();
37 const navigate = useNavigate();
38 const canGoBack = useCanGoBack();
39 const [restoreSignal, setRestoreSignal] = useState(0);
40 const showRestoreDefaults = location.pathname === "/settings/general";
41 const handleRestored = () => setRestoreSignal((value) => value + 1);
42 const navigateBackWithinApp = useCallback(() => {
43 if (canGoBack) {
44 window.history.back();
45 return;
46 }
47 void navigate({ to: "/" });
48 }, [canGoBack, navigate]);
49
50 useEffect(() => {
51 const onKeyDown = (event: KeyboardEvent) => {
52 if (event.defaultPrevented) return;
53 if (event.key === "Escape") {
54 event.preventDefault();
55 navigateBackWithinApp();
56 }
57 };
58
59 window.addEventListener("keydown", onKeyDown);
60 return () => {
61 window.removeEventListener("keydown", onKeyDown);
62 };
63 }, [navigateBackWithinApp]);
64
65 return (
66 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground isolate">
67 <div className="flex min-h-0 min-w-0 flex-1 flex-col bg-background text-foreground">
68 {!isElectron && (
69 <header
70 className={cn(
71 "border-b border-border px-3 py-2 transition-[padding-left] duration-200 ease-linear motion-reduce:transition-none sm:px-5",
72 COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS,
73 )}
74 >
75 <div className="flex min-h-7 items-center gap-2 sm:min-h-6">
76 <span className="text-sm font-medium text-foreground">Settings</span>
77 {showRestoreDefaults ? (
78 <div className="ms-auto flex items-center gap-2">
79 <RestoreDefaultsButton onRestored={handleRestored} />
80 </div>
81 ) : null}
82 </div>
83 </header>
84 )}
85
86 {isElectron && (
87 <div
88 className={cn(
89 "drag-region flex h-[52px] shrink-0 items-center border-b border-border px-5 transition-[padding-left] duration-200 ease-linear motion-reduce:transition-none wco:h-[env(titlebar-area-height)] wco:pr-[calc(100vw-env(titlebar-area-width)-env(titlebar-area-x)+1em)]",
90 COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS,
91 )}
92 >
93 <span className="text-xs font-medium tracking-wide text-muted-foreground/70">
94 Settings
95 </span>
96 {showRestoreDefaults ? (
97 <div className="ms-auto flex items-center gap-2">
98 <RestoreDefaultsButton onRestored={handleRestored} />
99 </div>
100 ) : null}
101 </div>
102 )}
103
104 <div key={restoreSignal} className="min-h-0 flex flex-1 flex-col">
105 <Outlet />
106 </div>
107 </div>
108 </SidebarInset>
109 );
110}
111
112function SettingsRouteLayout() {
113 return <SettingsContentLayout />;
114}
115
116export const Route = createFileRoute("/settings")({
117 beforeLoad: async ({ context, location }) => {
118 if (
119 context.authGateState.status !== "authenticated" &&
120 context.authGateState.status !== "hosted-static"
121 ) {
122 throw redirect({ to: "/pair", replace: true });
123 }
124
125 if (location.pathname === "/settings") {
126 throw redirect({ to: "/settings/general", replace: true });
127 }
128 },
129 component: SettingsRouteLayout,
130});