Open History to see the last commit
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 );
33}
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
54 data-spotlight="folder-counts"
55 className="notes-scroll min-h-0 flex-1 overflow-y-auto px-2.5 pb-3"
56 >
57 {quickNotes && (
58 <SidebarLink
59 folderId={quickNotes.id}
60 icon={<QuickNotesIcon />}
61 label={quickNotes.name}
62 count={counts[quickNotes.id]}
63 />
64 )}
65 <SidebarLink folderId="shared" icon={<SharedIcon />} label="Shared" count={counts.shared} />
66
67 <SidebarGroupLabel>iCloud</SidebarGroupLabel>
68 <SidebarLink folderId="all" icon={<FolderIcon />} label="All iCloud" count={counts.all} />
69 {iCloudFolders.map((folder) => (
70 <SidebarLink
71 key={folder.id}
72 folderId={folder.id}
73 icon={<FolderIcon />}
74 label={folder.name}
75 count={counts[folder.id]}
76 onRename={canRenameFolder(folder) ? () => setRenaming(folder) : undefined}
77 />
78 ))}
79 <SidebarLink
80 folderId="recently-deleted"
81 icon={<Trash className="size-[17px] stroke-[1.5] text-mac-icon" />}
82 label="Recently Deleted"
83 count={counts["recently-deleted"]}
84 />
85
86 <SidebarGroupLabel collapsed>Tags</SidebarGroupLabel>
87 </div>
88
89 <RenameFolderDialog folder={renaming} onOpenChange={(open) => !open && setRenaming(null)} />
90
91 <div className="space-y-0.5 border-t border-mac-column-border px-2.5 py-2">
92 <Link to="/analytics" className={rowClassName}>
93 <ChartColumn className="size-[17px] stroke-[1.5] text-mac-icon" />
94 <span className="flex-1 truncate">Analytics</span>
95 </Link>
96 {account && (
97 <Link to="/settings" className={`${rowClassName} h-auto py-1.5`}>
98 <AccountAvatar name={account.name} />
99 <span className="min-w-0 flex-1 leading-tight">
100 <span className="block truncate font-medium">{account.name}</span>
101 <span className="block truncate text-[11px] text-mac-text-secondary">
102 {account.email}
103 </span>
104 </span>
105 </Link>
106 )}
107 </div>
108 </nav>
109 );
110}
111
112const rowClassName =
113 "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";
114
115function SidebarLink({
116 folderId,
117 icon,
118 label,
119 count,
120 onRename,
121}: {
122 folderId: string;
123 icon: ReactNode;
124 label: string;
125 count: number | undefined;
126 /** Renamable folders: double-click or right-click → Rename Folder. */
127 onRename?: () => void;
128}) {
129 const link = (
130 <Link
131 to="/folders/$folderId"
132 params={{ folderId }}
133 onClick={() =>
134 reportAction({ type: "folder.opened", folderId, smart: isSmartFolderId(folderId) })
135 }
136 onDoubleClick={onRename}
137 className={`${rowClassName} data-[state=open]:ring-2 data-[state=open]:ring-notes-accent/60`}
138 >
139 {icon}
140 <span className="flex-1 truncate">{label}</span>
141 <span className="text-[12px] text-mac-text-secondary tabular-nums">{count ?? 0}</span>
142 </Link>
143 );
144 if (!onRename) return link;
145
146 return (
147 <ContextMenu>
148 <ContextMenuTrigger asChild>{link}</ContextMenuTrigger>
149 {/* Don't hand focus back to the row: the rename dialog takes it. */}
150 <ContextMenuContent className="min-w-44" onCloseAutoFocus={(event) => event.preventDefault()}>
151 <ContextMenuItem onSelect={onRename}>
152 <Pencil />
153 Rename Folder…
154 </ContextMenuItem>
155 </ContextMenuContent>
156 </ContextMenu>
157 );
158}
159
160function SidebarGroupLabel({ children, collapsed }: { children: ReactNode; collapsed?: boolean }) {
161 return (
162 <div className="group flex items-center px-2 pt-4 pb-1 text-[11px] font-semibold text-mac-text-secondary">
163 <span className="flex-1">{children}</span>
164 {collapsed && (
165 <ChevronRight className="size-3 opacity-0 transition-opacity group-hover:opacity-100" />
166 )}
167 </div>
168 );
169}
170
171function FolderIcon() {
172 return <Folder className="size-[17px] shrink-0 stroke-[1.5] text-mac-icon" aria-hidden />;
173}