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

note-list.tsx

197 lines6.3 KB

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