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

AppSidebar.tsx

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