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

SettingsSidebarNav.tsx

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