Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

AppSidebar.tsx

132 lines5.0 KB

1import { useLiveQuery } from "@tanstack/react-db";
2import { Link, useParams } from "@tanstack/react-router";
3import { ChevronDownIcon, FolderClosedIcon, SidebarIcon } from "lucide-react";
4import { projectsCollection } from "@/collections/projects";
5import {
6 Sidebar,
7 SidebarContent,
8 SidebarGroup,
9 SidebarGroupContent,
10 SidebarGroupLabel,
11 SidebarMenu,
12 SidebarMenuButton,
13 SidebarMenuItem,
14 SidebarMenuSub,
15 SidebarMenuSubButton,
16 SidebarMenuSubItem,
17 useSidebar,
18} from "@/components/ui/sidebar";
19import { cn } from "@/lib/utils";
20import { HighlightWrapper } from "@/utils/highlight-collection-related-info";
21import {
22 Collapsible,
23 CollapsibleContent,
24 CollapsibleTrigger,
25} from "./ui/collapsible";
26import { Skeleton } from "./ui/skeleton";
27​
28export function AppSidebar() {
29 const { toggleSidebar } = useSidebar();
30​
31 const { data: projects, isLoading: isLoadingProjects } = useLiveQuery((q) =>
32 q.from({
33 project: projectsCollection,
34 }),
35 );
36​
37 const params = useParams({ strict: false });
38​
39 return (
40 <Sidebar className="w-64" collapsible="icon">
41 <SidebarContent>
42 <SidebarGroup>
43 <SidebarGroupContent>
44 <SidebarMenu>
45 <SidebarMenuItem>
46 <SidebarMenuButton
47 onClick={() => {
48 toggleSidebar();
49 }}
50 >
51 <SidebarIcon />
52 {/* <SidebarTrigger /> */}
53 <span>Collapse</span>
54 </SidebarMenuButton>
55 </SidebarMenuItem>
56 </SidebarMenu>
57 </SidebarGroupContent>
58 </SidebarGroup>
59 <SidebarGroup>
60 <SidebarGroupLabel>Navigation</SidebarGroupLabel>
61 <SidebarGroupContent>
62 <SidebarMenu>
63 <Collapsible defaultOpen className="group/collapsible">
64 <SidebarMenuItem>
65 <HighlightWrapper highlightId="project_sidebar">
66 <CollapsibleTrigger asChild className="flex items-center">
67 <SidebarMenuButton className={cn("grow")}>
68 <FolderClosedIcon />
69 <span>
70 Projects{" "}
71 {!isLoadingProjects && (
72 <span>({projects.length})</span>
73 )}
74 </span>
75 {/* <SidebarMenuBadge>{boards.length}</SidebarMenuBadge> */}
76 <ChevronDownIcon className="ml-auto transition-transform duration-200 ease-in-out group-data-[state=open]/collapsible:rotate-180" />
77 </SidebarMenuButton>
78 </CollapsibleTrigger>
79 <CollapsibleContent>
80 <SidebarMenuSub>
81 {isLoadingProjects &&
82 Array.from({ length: 3 }).map((_, idx) => (
83 <SidebarMenuSubItem key={idx}>
84 <SidebarMenuSubButton>
85 <Skeleton className="h-(--text-base) w-40" />
86 </SidebarMenuSubButton>
87 </SidebarMenuSubItem>
88 ))}
89 {projects.map((project) => (
90 <SidebarMenuSubItem key={project.id}>
91 <SidebarMenuSubButton
92 asChild
93 isActive={params.projectId === project.id}
94 >
95 <Link
96 preload={
97 /*
98 NOTE: People will probably check the network
99 requests to see how TanStack DB works, so it's
100 the best if don't confuse them by preloading
101 data on link hovers etc.
102 */
103 false
104 }
105 to="/projects/$projectId"
106 params={{ projectId: project.id }}
107 style={
108 {
109 // fontWeight:
110 // selectedProject === project ? "bold" : "normal",
111 // color:
112 // selectedProject === project ? "#2563eb" : "inherit",
113 }
114 }
115 >
116 {project.name}
117 </Link>
118 </SidebarMenuSubButton>
119 </SidebarMenuSubItem>
120 ))}
121 </SidebarMenuSub>
122 </CollapsibleContent>
123 </HighlightWrapper>
124 </SidebarMenuItem>
125 </Collapsible>
126 </SidebarMenu>
127 </SidebarGroupContent>
128 </SidebarGroup>
129 </SidebarContent>
130 </Sidebar>
131 );