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

note-list.tsx

165 lines5.4 KB

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