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