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

SettingsSidebarNav.tsx

127 lines4.0 KB

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