Open History to see the last commit
1import { Link, useParams } from "@tanstack/react-router";
2import { ChevronDown, Ellipsis, Folder } from "lucide-react";
3import { useState } from "react";
4
5import { ContextMenu, ContextMenuContent, ContextMenuTrigger } from "@/components/ui/context-menu";
6import { useFolderView, useNoteList } from "@/db/queries";
7import type { NoteListItem } from "@/db/queries";
8import { reportAction } from "@/lib/app-actions";
9import { formatListDate } from "@/lib/notes/dates";
10import { noteDisplayTitle, noteSnippet } from "@/lib/notes/note-body";
11import { noteListSections } from "@/lib/notes/note-list";
12import { cn } from "@/lib/utils";
13
14import { notesMenuClassName } from "../menu-style";
15
16import { NoteActionItems } from "../note-actions";
17import { useNotesLayout } from "../notes-layout";
18import { MacCircleButton, MacColumnHeader } from "./controls";
19import { SidebarToggle } from "./sidebar";
20
21export function MacNoteList({ folderId }: { folderId: string }) {
22 const { view } = useFolderView(folderId);
23 const { notes, listOptions } = useNoteList(folderId);
24 const { sidebarVisible } = useNotesLayout();
25 const { noteId: selectedNoteId } = useParams({ strict: false });
26 const [pinnedOpen, setPinnedOpen] = useState(true);
27
28 const now = new Date();
29 const { pinned, groups } = noteListSections(notes, now, listOptions);
30 const showFolder = view?.showsFolderName ?? false;
31
32 return (
33 <section
34 aria-label="Notes"
35 data-spotlight="note-list"
36 className="flex w-[216px] shrink-0 flex-col border-r border-mac-column-border bg-mac-content"
37 >
38 <MacColumnHeader>
39 {!sidebarVisible && <SidebarToggle />}
40 <div className="min-w-0 flex-1 leading-tight">
41 <h2 className="truncate text-[13px] font-semibold text-mac-text">{view?.name}</h2>
42 <p className="text-[11px] text-mac-text-secondary">
43 {notes.length} {notes.length === 1 ? "note" : "notes"}
44 </p>
45 </div>
46 <MacCircleButton aria-label="View options" notImplemented>
47 <Ellipsis />
48 </MacCircleButton>
49 </MacColumnHeader>
50
51 <div className="notes-scroll min-h-0 flex-1 overflow-y-auto pb-4">
52 {notes.length === 0 && (
53 <p className="pt-24 text-center text-[13px] text-mac-text-secondary">No Notes</p>
54 )}
55
56 {pinned.length > 0 && (
57 <div className="pb-1">
58 <button
59 type="button"
60 onClick={() => setPinnedOpen((open) => !open)}
61 aria-expanded={pinnedOpen}
62 className="mb-2 flex w-full items-center border-b border-mac-separator px-[14px] pt-2.5 pb-2 text-left text-[15px] font-bold text-mac-text"
63 >
64 <span className="flex-1">Pinned</span>
65 <ChevronDown
66 className={cn(
67 "size-3.5 text-mac-text-secondary transition-transform",
68 !pinnedOpen && "-rotate-90",
69 )}
70 />
71 </button>
72 {pinnedOpen && (
73 <NoteRows
74 notes={pinned}
75 selectedNoteId={selectedNoteId}
76 showFolder={showFolder}
77 now={now}
78 />
79 )}
80 </div>
81 )}
82
83 {groups.map((group) => (
84 <div key={group.label}>
85 {group.label ? (
86 <h3 className="px-[14px] pt-5 pb-1.5 text-[15px] font-bold text-mac-text">
87 {group.label}
88 </h3>
89 ) : (
90 pinned.length > 0 && <div className="h-3" />
91 )}
92 <NoteRows
93 notes={group.items}
94 selectedNoteId={selectedNoteId}
95 showFolder={showFolder}
96 now={now}
97 />
98 </div>
99 ))}
100 </div>
101 </section>
102 );
103}
104
105function NoteRows({
106 notes,
107 selectedNoteId,
108 showFolder,
109 now,
110}: {
111 notes: NoteListItem[];
112 selectedNoteId: string | undefined;
113 showFolder: boolean;
114 now: Date;
115}) {
116 return (
117 <ul className="px-2">
118 {notes.map((note, index) => {
119 const selected = note.id === selectedNoteId;
120 const previousSelected = notes[index - 1]?.id === selectedNoteId;
121 return (
122 <li key={note.id} className="relative">
123 {index > 0 && !selected && !previousSelected && (
124 <span
125 aria-hidden
126 className="absolute top-0 right-1.5 left-[22px] h-px bg-mac-separator"
127 />
128 )}
129 <NoteRow note={note} selected={selected} showFolder={showFolder} now={now} />
130 </li>
131 );
132 })}
133 </ul>
134 );
135}
136
137function NoteRow({
138 note,
139 selected,
140 showFolder,
141 now,
142}: {
143 note: NoteListItem;
144 selected: boolean;
145 showFolder: boolean;
146 now: Date;
147}) {
148 const { folderId } = useParams({ strict: false });
149
150 return (
151 <ContextMenu>
152 <ContextMenuTrigger asChild>
153 <Link
154 to="/folders/$folderId/notes/$noteId"
155 params={{ folderId: folderId ?? note.folderId, noteId: note.id }}
156 onClick={() => reportAction({ type: "note.opened", noteId: note.id })}
157 aria-current={selected ? "page" : undefined}
158 className={cn(
159 "block rounded-[8px] px-[14px] py-[9px] text-mac-text outline-none focus-visible:ring-2 focus-visible:ring-notes-accent/60 data-[state=open]:ring-2 data-[state=open]:ring-notes-accent/60",
160 selected && "bg-mac-row-selected",
161 )}
162 >
163 <NoteRowText note={note} showFolder={showFolder} now={now} />
164 </Link>
165 </ContextMenuTrigger>
166 <ContextMenuContent className={cn("min-w-44", notesMenuClassName)}>
167 <NoteActionItems note={note} menu="context" />
168 </ContextMenuContent>
169 </ContextMenu>
170 );
171}
172
173function NoteRowText({
174 note,
175 showFolder,
176 now,
177}: {
178 note: NoteListItem;
179 showFolder: boolean;
180 now: Date;
181}) {
182 return (
183 <>
184 <p className="truncate text-[13px] leading-[17px] font-bold">
185 {noteDisplayTitle(note.title)}
186 </p>
187 <p className="flex gap-1.5 text-[12px] leading-[16px]">
188 <span className="shrink-0">{formatListDate(new Date(note.updatedAt), now)}</span>
189 <span className="truncate text-mac-text-secondary">{noteSnippet(note.body)}</span>
190 </p>
191 {showFolder && note.folderName && (
192 <p className="mt-0.5 flex items-center gap-1 text-[12px] leading-[16px] text-mac-text-secondary">
193 <Folder className="size-3 shrink-0" />
194 <span className="truncate">{note.folderName}</span>
195 </p>
196 )}
197 </>
198 );
199}