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

sidebar.tsx

179 lines5.9 KB

1import { Link } from "@tanstack/react-router";
2import { ChartColumn, ChevronRight, Folder, Pencil, PanelLeft, Trash } from "lucide-react";
3import { useState } from "react";
4import type { ReactNode } from "react";
5​
6import {
7 ContextMenu,
8 ContextMenuContent,
9 ContextMenuItem,
10 ContextMenuTrigger,
11} from "@/components/ui/context-menu";
12import { useAccount, useFolders, useNoteCounts } from "@/db/queries";
13import type { Folder as FolderRecord } from "@/db/schema";
14import { reportAction } from "@/lib/app-actions";
15import { isSmartFolderId } from "@/lib/notes/folder-views";
16​
17import { cn } from "@/lib/utils";
18​
19import { notesMenuClassName } from "../menu-style";
20import { AccountAvatar, QuickNotesIcon, SharedIcon } from "../icons";
21import { useNotesLayout } from "../notes-layout";
22import { canRenameFolder, RenameFolderDialog } from "../rename-folder-dialog";
23import { MacCircleButton, MacColumnHeader } from "./controls";
24​
25export function SidebarToggle() {
26 const { toggleSidebar, sidebarVisible } = useNotesLayout();
27 return (
28 <MacCircleButton
29 aria-label={sidebarVisible ? "Hide sidebar" : "Show sidebar"}
30 aria-pressed={sidebarVisible}
31 onClick={toggleSidebar}
32 >
33 <PanelLeft />
34 </MacCircleButton>
35 );
37​
38export function MacSidebar() {
39 const folders = useFolders();
40 const counts = useNoteCounts();
41 const account = useAccount();
42 const [renaming, setRenaming] = useState<FolderRecord | null>(null);
43​
44 const quickNotes = folders.find((folder) => folder.role === "quick-notes");
45 const iCloudFolders = folders.filter((folder) => folder.role !== "quick-notes");
46​
47 return (
48 <nav
49 aria-label="Folders"
50 className="flex w-[208px] shrink-0 flex-col border-r border-mac-column-border bg-mac-sidebar"
51 >
52 <MacColumnHeader>
53 <SidebarToggle />
54 </MacColumnHeader>
55​
56 <div
57 data-spotlight="folder-counts"
58 className="notes-scroll min-h-0 flex-1 overflow-y-auto px-2.5 pb-3"
59 >
60 {quickNotes && (
61 <SidebarLink
62 folderId={quickNotes.id}
63 icon={<QuickNotesIcon />}
64 label={quickNotes.name}
65 count={counts[quickNotes.id]}
66 />
67 )}
68 <SidebarLink folderId="shared" icon={<SharedIcon />} label="Shared" count={counts.shared} />
69​
70 <SidebarGroupLabel>iCloud</SidebarGroupLabel>
71 <SidebarLink folderId="all" icon={<FolderIcon />} label="All iCloud" count={counts.all} />
72 {iCloudFolders.map((folder) => (
73 <SidebarLink
74 key={folder.id}
75 folderId={folder.id}
76 icon={<FolderIcon />}
77 label={folder.name}
78 count={counts[folder.id]}
79 onRename={canRenameFolder(folder) ? () => setRenaming(folder) : undefined}
80 />
81 ))}
82 <SidebarLink
83 folderId="recently-deleted"
84 icon={<Trash className="size-[17px] stroke-[1.5] text-mac-icon" />}
85 label="Recently Deleted"
86 count={counts["recently-deleted"]}
87 />
88​
89 <SidebarGroupLabel collapsed>Tags</SidebarGroupLabel>
90 </div>
91​
92 <RenameFolderDialog folder={renaming} onOpenChange={(open) => !open && setRenaming(null)} />
93​
94 <div className="space-y-0.5 border-t border-mac-column-border px-2.5 py-2">
95 <Link to="/analytics" className={rowClassName}>
96 <ChartColumn className="size-[17px] stroke-[1.5] text-mac-icon" />
97 <span className="flex-1 truncate">Analytics</span>
98 </Link>
99 {account && (
100 <Link to="/settings" className={`${rowClassName} h-auto py-1.5`}>
101 <AccountAvatar name={account.name} />
102 <span className="min-w-0 flex-1 leading-tight">
103 <span className="block truncate font-medium">{account.name}</span>
104 <span className="block truncate text-[11px] text-mac-text-secondary">
105 {account.email}
106 </span>
107 </span>
108 </Link>
109 )}
110 </div>
111 </nav>
112 );
114​
115const rowClassName =
116 "flex h-[30px] items-center gap-2 rounded-[7px] px-2 text-[13px] text-mac-text outline-none focus-visible:ring-2 focus-visible:ring-notes-accent/60 data-[status=active]:bg-mac-folder-selected";
117​
118function SidebarLink({
119 folderId,
120 icon,
121 label,
122 count,
123 onRename,
124}: {
125 folderId: string;
126 icon: ReactNode;
127 label: string;
128 count: number | undefined;
129 /** Renamable folders: double-click or right-click → Rename Folder. */
130 onRename?: () => void;
131}) {
132 const link = (
133 <Link
134 to="/folders/$folderId"
135 params={{ folderId }}
136 onClick={() =>
137 reportAction({ type: "folder.opened", folderId, smart: isSmartFolderId(folderId) })
138 }
139 onDoubleClick={onRename}
140 className={`${rowClassName} data-[state=open]:ring-2 data-[state=open]:ring-notes-accent/60`}
141 >
142 {icon}
143 <span className="flex-1 truncate">{label}</span>
144 <span className="text-[12px] text-mac-text-secondary tabular-nums">{count ?? 0}</span>
145 </Link>
146 );
147 if (!onRename) return link;
148​
149 return (
150 <ContextMenu>
151 <ContextMenuTrigger asChild>{link}</ContextMenuTrigger>
152 {/* Don't hand focus back to the row: the rename dialog takes it. */}
153 <ContextMenuContent
154 className={cn("min-w-44", notesMenuClassName)}
155 onCloseAutoFocus={(event) => event.preventDefault()}
156 >
157 <ContextMenuItem onSelect={onRename}>
158 <Pencil />
159 Rename Folder…
160 </ContextMenuItem>
161 </ContextMenuContent>
162 </ContextMenu>
163 );
165​
166function SidebarGroupLabel({ children, collapsed }: { children: ReactNode; collapsed?: boolean }) {
167 return (
168 <div className="group flex items-center px-2 pt-4 pb-1 text-[11px] font-semibold text-mac-text-secondary">
169 <span className="flex-1">{children}</span>
170 {collapsed && (
171 <ChevronRight className="size-3 opacity-0 transition-opacity group-hover:opacity-100" />
172 )}
173 </div>
174 );
176​
177function FolderIcon() {
178 return <Folder className="size-[17px] shrink-0 stroke-[1.5] text-mac-icon" aria-hidden />;