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

note-list-screen.tsx

146 lines4.5 KB

1import { Link } from "@tanstack/react-router";
2import { Ellipsis, Folder, Share } 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";
12​
13import { NoteActionItems } from "../note-actions";
14import { useComposeNote } from "../use-compose-note";
15​
16import {
17 BackLink,
18 GlassPill,
19 GlassPillButton,
20 InsetCard,
21 InsetRow,
22 IosScreen,
23 LargeTitle,
24 SearchAndCompose,
25 SectionHeader,
26} from "./ui";
27​
28export function IosNoteListScreen({ folderId }: { folderId: string }) {
29 const { view } = useFolderView(folderId);
30 const { notes, listOptions } = useNoteList(folderId);
31 const [pinnedOpen, setPinnedOpen] = useState(true);
32 const composeNote = useComposeNote();
33​
34 const now = new Date();
35 const { pinned, groups } = noteListSections(notes, now, listOptions);
36 const showFolder = view?.showsFolderName ?? false;
37​
38 return (
39 <IosScreen
40 leading={<BackLink to="/" aria-label="Folders" />}
41 trailing={
42 <GlassPill>
43 <GlassPillButton aria-label="Share">
44 <Share />
45 </GlassPillButton>
46 <GlassPillButton aria-label="More">
47 <Ellipsis />
48 </GlassPillButton>
49 </GlassPill>
50 }
51 bottom={<SearchAndCompose onCompose={composeNote} />}
52 >
53 <LargeTitle
54 title={view?.name ?? "Notes"}
55 subtitle={`${notes.length} ${notes.length === 1 ? "Note" : "Notes"}`}
56 />
57​
58 {notes.length === 0 && (
59 <p className="pt-16 text-center text-[17px] text-ios-secondary">No Notes</p>
60 )}
61​
62 {pinned.length > 0 && (
63 <>
64 <SectionHeader chevron open={pinnedOpen} onToggle={() => setPinnedOpen((open) => !open)}>
65 Pinned
66 </SectionHeader>
67 {pinnedOpen && (
68 <NoteCard notes={pinned} folderId={folderId} showFolder={showFolder} now={now} />
69 )}
70 </>
71 )}
72​
73 {groups.map((group) => (
74 <div key={group.label}>
75 {group.label ? <SectionHeader>{group.label}</SectionHeader> : <div className="h-5" />}
76 <NoteCard notes={group.items} folderId={folderId} showFolder={showFolder} now={now} />
77 </div>
78 ))}
79 </IosScreen>
80 );
82​
83function NoteCard({
84 notes,
85 folderId,
86 showFolder,
87 now,
88}: {
89 notes: NoteListItem[];
90 folderId: string;
91 showFolder: boolean;
92 now: Date;
93}) {
94 return (
95 <InsetCard>
96 {notes.map((note, index) => (
97 <InsetRow key={note.id} first={index === 0} separatorInset={26}>
98 {/* Long-press for the note's actions, like in Notes. */}
99 <ContextMenu>
100 <ContextMenuTrigger asChild>
101 <Link
102 to="/folders/$folderId/notes/$noteId"
103 params={{ folderId, noteId: note.id }}
104 onClick={() => reportAction({ type: "note.opened", noteId: note.id })}
105 className="block px-[26px] py-[11px] select-none [-webkit-touch-callout:none] active:bg-black/5 data-[state=open]:bg-black/5 dark:active:bg-white/5 dark:data-[state=open]:bg-white/5"
106 >
107 <NoteRowText note={note} showFolder={showFolder} now={now} />
108 </Link>
109 </ContextMenuTrigger>
110 <ContextMenuContent className="min-w-48">
111 <NoteActionItems note={note} menu="context" />
112 </ContextMenuContent>
113 </ContextMenu>
114 </InsetRow>
115 ))}
116 </InsetCard>
117 );
119​
120function NoteRowText({
121 note,
122 showFolder,
123 now,
124}: {
125 note: NoteListItem;
126 showFolder: boolean;
127 now: Date;
128}) {
129 return (
130 <>
131 <p className="truncate text-[17px] leading-snug font-semibold">
132 {noteDisplayTitle(note.title)}
133 </p>
134 <p className="flex gap-2 text-[15px] leading-snug text-ios-secondary">
135 <span className="shrink-0">{formatListDate(new Date(note.updatedAt), now)}</span>
136 <span className="truncate">{noteSnippet(note.body)}</span>
137 </p>
138 {showFolder && note.folderName && (
139 <p className="mt-0.5 flex items-center gap-1.5 text-[15px] leading-snug text-ios-secondary">
140 <Folder className="size-4 shrink-0" />
141 <span className="truncate">{note.folderName}</span>
142 </p>
143 )}
144 </>
145 );