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

folders-screen.tsx

211 lines6.8 KB

1import { Link } from "@tanstack/react-router";
2import { ChartColumn, Folder, FolderPlus, Pencil, Trash } from "lucide-react";
3import { useState } from "react";
4import type { ReactNode } from "react";
5​
6import { useAccount, useFolders, useNoteCounts } from "@/db/queries";
7import type { Folder as FolderRecord } from "@/db/schema";
8import { reportAction } from "@/lib/app-actions";
9import { isSmartFolderId } from "@/lib/notes/folder-views";
10import { cn } from "@/lib/utils";
11​
12import { AccountAvatar, QuickNotesIcon, SharedIcon } from "../icons";
13import { canRenameFolder, RenameFolderDialog } from "../rename-folder-dialog";
14import { useComposeNote } from "../use-compose-note";
15import {
16 GlassCircle,
17 GlassPill,
18 InsetCard,
19 InsetRow,
20 IosScreen,
21 LargeTitle,
22 RowChevron,
23 SearchAndCompose,
24 SectionHeader,
25} from "./ui";
26​
27export function IosFoldersScreen() {
28 const folders = useFolders();
29 const counts = useNoteCounts();
30 const account = useAccount();
31 const [iCloudOpen, setICloudOpen] = useState(true);
32 const [editing, setEditing] = useState(false);
33 const [renaming, setRenaming] = useState<FolderRecord | null>(null);
34 const composeNote = useComposeNote();
35​
36 const quickNotes = folders.find((folder) => folder.role === "quick-notes");
37 const iCloudFolders = folders.filter((folder) => folder.role !== "quick-notes");
38​
39 return (
40 <IosScreen
41 trailing={
42 <>
43 <GlassCircle aria-label="New folder">
44 <FolderPlus />
45 </GlassCircle>
46 <GlassPill className={cn("p-0", editing && "bg-notes-accent text-black")}>
47 <button
48 type="button"
49 aria-pressed={editing}
50 onClick={() => setEditing((on) => !on)}
51 className="h-full rounded-full px-4 text-[17px] font-medium"
52 >
53 {editing ? "Done" : "Edit"}
54 </button>
55 </GlassPill>
56 </>
57 }
58 bottom={<SearchAndCompose onCompose={composeNote} />}
59 >
60 <LargeTitle title="Folders" />
61​
62 <InsetCard>
63 {quickNotes && (
64 <InsetRow first separatorInset={58}>
65 <FolderRow
66 folderId={quickNotes.id}
67 icon={<QuickNotesIcon className="size-[30px] rounded-[8px]" />}
68 name={quickNotes.name}
69 count={counts[quickNotes.id]}
70 />
71 </InsetRow>
72 )}
73 <InsetRow first={!quickNotes} separatorInset={58}>
74 <FolderRow
75 folderId="shared"
76 icon={<SharedIcon className="size-[30px] rounded-[8px]" />}
77 name="Shared"
78 count={counts.shared}
79 />
80 </InsetRow>
81 </InsetCard>
82​
83 <SectionHeader chevron open={iCloudOpen} onToggle={() => setICloudOpen((open) => !open)}>
84 iCloud
85 </SectionHeader>
86 {iCloudOpen && (
87 <InsetCard>
88 <InsetRow first separatorInset={58}>
89 <FolderRow folderId="all" icon={<FolderGlyph />} name="All iCloud" count={counts.all} />
90 </InsetRow>
91 {iCloudFolders.map((folder) => (
92 <InsetRow key={folder.id} separatorInset={58}>
93 {editing && canRenameFolder(folder) ? (
94 <RenameRow
95 folder={folder}
96 count={counts[folder.id]}
97 onRename={() => setRenaming(folder)}
98 />
99 ) : (
100 <FolderRow
101 folderId={folder.id}
102 icon={<FolderGlyph />}
103 name={folder.name}
104 count={counts[folder.id]}
105 />
106 )}
107 </InsetRow>
108 ))}
109 <InsetRow separatorInset={58}>
110 <FolderRow
111 folderId="recently-deleted"
112 icon={<Trash className="size-[26px] text-notes-accent" strokeWidth={1.6} />}
113 name="Recently Deleted"
114 count={counts["recently-deleted"]}
115 />
116 </InsetRow>
117 </InsetCard>
118 )}
119​
120 <SectionHeader chevron open={false}>
121 Tags
122 </SectionHeader>
123​
124 <SectionHeader>Account</SectionHeader>
125 <InsetCard>
126 <InsetRow first separatorInset={58}>
127 <Link to="/settings" className="flex min-h-[60px] items-center gap-3 px-4 py-2">
128 <AccountAvatar name={account?.name ?? ""} className="size-[30px]" />
129 <span className="min-w-0 flex-1 leading-tight">
130 <span className="block truncate text-[17px]">{account?.name}</span>
131 <span className="block truncate text-[14px] text-ios-secondary">
132 {account?.email}
133 </span>
134 </span>
135 <RowChevron />
136 </Link>
137 </InsetRow>
138 <InsetRow separatorInset={58}>
139 <Link to="/analytics" className="flex h-[52px] items-center gap-3 px-4">
140 <span className="grid w-[30px] place-items-center">
141 <ChartColumn className="size-[24px] text-notes-accent" strokeWidth={1.8} />
142 </span>
143 <span className="flex-1 text-[17px]">Analytics</span>
144 <RowChevron />
145 </Link>
146 </InsetRow>
147 </InsetCard>
148​
149 <RenameFolderDialog folder={renaming} onOpenChange={(open) => !open && setRenaming(null)} />
150 </IosScreen>
151 );
153​
154/** In Edit mode, tapping a folder renames it instead of opening it. */
155function RenameRow({
156 folder,
157 count,
158 onRename,
159}: {
160 folder: FolderRecord;
161 count: number | undefined;
162 onRename: () => void;
163}) {
164 return (
165 <button
166 type="button"
167 onClick={onRename}
168 aria-label={`Rename ${folder.name}`}
169 className="flex h-[52px] w-full items-center gap-3 px-4 text-left active:bg-black/5 dark:active:bg-white/5"
170 >
171 <span className="grid w-[30px] place-items-center">
172 <FolderGlyph />
173 </span>
174 <span className="min-w-0 flex-1 truncate text-[17px]">{folder.name}</span>
175 <span className="text-[17px] text-ios-secondary tabular-nums">{count ?? 0}</span>
176 <Pencil className="size-[18px] shrink-0 text-notes-accent" strokeWidth={2.2} />
177 </button>
178 );
180​
181function FolderRow({
182 folderId,
183 icon,
184 name,
185 count,
186}: {
187 folderId: string;
188 icon: ReactNode;
189 name: string;
190 count: number | undefined;
191}) {
192 return (
193 <Link
194 to="/folders/$folderId"
195 params={{ folderId }}
196 onClick={() =>
197 reportAction({ type: "folder.opened", folderId, smart: isSmartFolderId(folderId) })
198 }
199 className="flex h-[52px] items-center gap-3 px-4 active:bg-black/5 dark:active:bg-white/5"
200 >
201 <span className="grid w-[30px] place-items-center">{icon}</span>
202 <span className="min-w-0 flex-1 truncate text-[17px]">{name}</span>
203 <span className="text-[17px] text-ios-secondary tabular-nums">{count ?? 0}</span>
204 <RowChevron />
205 </Link>
206 );
208​
209function FolderGlyph() {
210 return <Folder className="size-[26px] text-notes-accent" strokeWidth={1.6} />;