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

settings.tsx

137 lines4.4 KB

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 { SettingsBreadcrumb } from "../components/settings/SettingsBreadcrumb";
14import { Button } from "../components/ui/button";
15import { SidebarInset } from "../components/ui/sidebar";
16import { isElectron } from "../env";
17import { cn } from "~/lib/utils";
18import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
19​
20function RestoreDefaultsButton({ onRestored }: { onRestored: () => void }) {
21 const { changedSettingLabels, restoreDefaults } = useSettingsRestore(onRestored);
22​
23 return (
24 <Button
25 size="xs"
26 variant="ghost"
27 disabled={changedSettingLabels.length === 0}
28 onClick={() => void restoreDefaults()}
29 >
30 <RotateCcwIcon className="mx-1 size-3.5" />
31 Restore defaults
32 </Button>
33 );
35​
36function SettingsContentLayout() {
37 const location = useLocation();
38 const navigate = useNavigate();
39 const canGoBack = useCanGoBack();
40 const [restoreSignal, setRestoreSignal] = useState(0);
41 const showRestoreDefaults = location.pathname === "/settings/general";
42 const handleRestored = () => setRestoreSignal((value) => value + 1);
43 const navigateBackWithinApp = useCallback(() => {
44 if (canGoBack) {
45 window.history.back();
46 return;
47 }
48 void navigate({ to: "/" });
49 }, [canGoBack, navigate]);
50​
51 useEffect(() => {
52 const onKeyDown = (event: KeyboardEvent) => {
53 if (event.defaultPrevented) return;
54 if (event.key === "Escape") {
55 event.preventDefault();
56​
57 const activeElement = document.activeElement;
58 if (activeElement instanceof HTMLElement) {
59 activeElement.blur();
60 }
61​
62 navigateBackWithinApp();
63 }
64 };
65​
66 window.addEventListener("keydown", onKeyDown);
67 return () => {
68 window.removeEventListener("keydown", onKeyDown);
69 };
70 }, [navigateBackWithinApp]);
71​
72 return (
73 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground isolate">
74 <div className="flex min-h-0 min-w-0 flex-1 flex-col bg-background text-foreground">
75 {!isElectron && (
76 <header
77 className={cn(
78 "workspace-topbar px-3 transition-[padding-left] duration-200 ease-linear motion-reduce:transition-none sm:px-5",
79 COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS,
80 )}
81 >
82 <div className="flex w-full items-center gap-2">
83 <SettingsBreadcrumb pathname={location.pathname} />
84 {showRestoreDefaults ? (
85 <div className="ms-auto flex items-center gap-2">
86 <RestoreDefaultsButton onRestored={handleRestored} />
87 </div>
88 ) : null}
89 </div>
90 </header>
91 )}
92​
93 {isElectron && (
94 <div
95 className={cn(
96 "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)]",
97 COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS,
98 )}
99 >
100 <div className="flex w-full items-center gap-2">
101 <SettingsBreadcrumb pathname={location.pathname} />
102 {showRestoreDefaults ? (
103 <div className="ms-auto flex items-center gap-2">
104 <RestoreDefaultsButton onRestored={handleRestored} />
105 </div>
106 ) : null}
107 </div>
108 </div>
109 )}
110​
111 <div key={restoreSignal} className="min-h-0 flex flex-1 flex-col">
112 <Outlet />
113 </div>
114 </div>
115 </SidebarInset>
116 );
118​
119function SettingsRouteLayout() {
120 return <SettingsContentLayout />;
122​
123export const Route = createFileRoute("/settings")({
124 beforeLoad: async ({ context, location }) => {
125 if (
126 context.authGateState.status !== "authenticated" &&
127 context.authGateState.status !== "hosted-static"
128 ) {
129 throw redirect({ to: "/pair", replace: true });
130 }
131​
132 if (location.pathname === "/settings") {
133 throw redirect({ to: "/settings/general", replace: true });
134 }
135 },
136 component: SettingsRouteLayout,
137});