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="ghost"
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
56 const activeElement = document.activeElement;
57 if (activeElement instanceof HTMLElement) {
58 activeElement.blur();
59 }
60
61 navigateBackWithinApp();
62 }
63 };
64
65 window.addEventListener("keydown", onKeyDown);
66 return () => {
67 window.removeEventListener("keydown", onKeyDown);
68 };
69 }, [navigateBackWithinApp]);
70
71 return (
72 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground isolate">
73 <div className="flex min-h-0 min-w-0 flex-1 flex-col bg-background text-foreground">
74 {!isElectron && (
75 <header
76 className={cn(
77 "workspace-topbar px-3 transition-[padding-left] duration-200 ease-linear motion-reduce:transition-none sm:px-5",
78 COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS,
79 )}
80 >
81 <div className="flex w-full items-center gap-2">
82 <span className="text-sm font-medium text-foreground">Settings</span>
83 {showRestoreDefaults ? (
84 <div className="ms-auto flex items-center gap-2">
85 <RestoreDefaultsButton onRestored={handleRestored} />
86 </div>
87 ) : null}
88 </div>
89 </header>
90 )}
91
92 {isElectron && (
93 <div
94 className={cn(
95 "drag-region flex h-[52px] shrink-0 items-center 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)]",
96 COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS,
97 )}
98 >
99 <span className="text-xs font-medium tracking-wide text-muted-foreground/70">
100 Settings
101 </span>
102 {showRestoreDefaults ? (
103 <div className="ms-auto flex items-center gap-2">
104 <RestoreDefaultsButton onRestored={handleRestored} />
105 </div>
106 ) : null}
107 </div>
108 )}
109
110 <div key={restoreSignal} className="min-h-0 flex flex-1 flex-col">
111 <Outlet />
112 </div>
113 </div>
114 </SidebarInset>
115 );
116}
117
118function SettingsRouteLayout() {
119 return <SettingsContentLayout />;
120}
121
122export const Route = createFileRoute("/settings")({
123 beforeLoad: async ({ context, location }) => {
124 if (
125 context.authGateState.status !== "authenticated" &&
126 context.authGateState.status !== "hosted-static"
127 ) {
128 throw redirect({ to: "/pair", replace: true });
129 }
130
131 if (location.pathname === "/settings") {
132 throw redirect({ to: "/settings/general", replace: true });
133 }
134 },
135 component: SettingsRouteLayout,
136});