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

SettingsSidebarNav.tsx

130 lines4.2 KB

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