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

SettingsSidebarNav.tsx

114 lines3.4 KB

1import { useCallback, type ComponentType } from "react";
2import {
3 ArchiveIcon,
4 ArrowLeftIcon,
5 BotIcon,
6 FlaskConicalIcon,
7 GitBranchIcon,
8 KeyboardIcon,
9 Link2Icon,
10 PaletteIcon,
11 Settings2Icon,
12} from "lucide-react";
13import { useCanGoBack, useNavigate } from "@tanstack/react-router";
14​
15import {
16 SidebarContent,
17 SidebarFooter,
18 SidebarGroup,
19 SidebarMenu,
20 SidebarMenuButton,
21 SidebarMenuItem,
22 useSidebar,
23} from "../ui/sidebar";
24import { T3ConnectSidebarAvatar, T3ConnectSidebarSignIn } from "../clerk/T3ConnectSidebarSignIn";
25​
26export type SettingsSectionPath =
27 | "/settings/general"
28 | "/settings/appearance"
29 | "/settings/keybindings"
30 | "/settings/providers"
31 | "/settings/source-control"
32 | "/settings/connections"
33 | "/settings/beta"
34 | "/settings/archived";
35​
36export const SETTINGS_NAV_ITEMS: ReadonlyArray<{
37 label: string;
38 to: SettingsSectionPath;
39 icon: ComponentType<{ className?: string }>;
40}> = [
41 { label: "General", to: "/settings/general", icon: Settings2Icon },
42 { label: "Appearance", to: "/settings/appearance", icon: PaletteIcon },
43 { label: "Keybindings", to: "/settings/keybindings", icon: KeyboardIcon },
44 { label: "Providers", to: "/settings/providers", icon: BotIcon },
45 { label: "Source Control", to: "/settings/source-control", icon: GitBranchIcon },
46 { label: "Connections", to: "/settings/connections", icon: Link2Icon },
47 { label: "Beta", to: "/settings/beta", icon: FlaskConicalIcon },
48 { label: "Archive", to: "/settings/archived", icon: ArchiveIcon },
49];
50​
51export function SettingsSidebarNav({ pathname }: { pathname: string }) {
52 const navigate = useNavigate();
53 const canGoBack = useCanGoBack();
54 const { isMobile, setOpenMobile } = useSidebar();
55 const handleSectionClick = useCallback(
56 (to: SettingsSectionPath) => {
57 if (isMobile) {
58 setOpenMobile(false);
59 }
60 void navigate({ to, replace: true });
61 },
62 [isMobile, navigate, setOpenMobile],
63 );
64 const handleBackClick = useCallback(() => {
65 if (isMobile) {
66 setOpenMobile(false);
67 }
68 if (canGoBack) {
69 window.history.back();
70 return;
71 }
72 void navigate({ to: "/" });
73 }, [canGoBack, isMobile, navigate, setOpenMobile]);
74​
75 return (
76 <>
77 <SidebarContent className="overflow-x-hidden">
78 <SidebarGroup className="p-2">
79 <SidebarMenu>
80 {SETTINGS_NAV_ITEMS.map((item) => {
81 const Icon = item.icon;
82 const isActive = pathname === item.to;
83 return (
84 <SidebarMenuItem key={item.to}>
85 <SidebarMenuButton
86 isActive={isActive}
87 onClick={() => handleSectionClick(item.to)}
88 >
89 <Icon />
90 <span className="truncate">{item.label}</span>
91 </SidebarMenuButton>
92 </SidebarMenuItem>
93 );
94 })}
95 </SidebarMenu>
96 </SidebarGroup>
97 </SidebarContent>
98 <SidebarFooter className="p-2">
99 <T3ConnectSidebarSignIn />
100 <div className="flex items-center gap-1">
101 <SidebarMenu className="min-w-0 flex-1">
102 <SidebarMenuItem>
103 <SidebarMenuButton onClick={handleBackClick}>
104 <ArrowLeftIcon />
105 <span>Back</span>
106 </SidebarMenuButton>
107 </SidebarMenuItem>
108 </SidebarMenu>
109 <T3ConnectSidebarAvatar />
110 </div>
111 </SidebarFooter>
112 </>
113 );