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

sidebar.tsx

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