forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

notes-page.tsx141 lines, 5.2 KiBHistoryRawDownload
1import { Show, Suspense, createEffect, createMemo, createSignal } from "solid-js";
2import { createResource } from "@/lib/create-resource";
3import { useLocation, useNavigate } from "@tanstack/solid-router";
4import { deleteNoteById } from "@/api/notes";
5import { getNotes } from "@/api/notes";
6import { patchNoteById } from "@/api/notes";
7import { postNote } from "@/api/notes";
8import { formatApiError } from "@/api/client";
9import { Alert } from "@/components/ui/alert";
10import { RouteGuard } from "@/components/layout/route-guard";
11import { NoteImportPanel } from "@/components/notes/note-import-panel";
12import { NoteList } from "@/components/notes/note-list";
13import { Button } from "@/components/ui/button";
14import { IconPlus, IconUploadCloud } from "@/components/ui/icons";
15import { PageSpinner } from "@/components/ui/page-spinner";
16import { PaginationControls } from "@/components/ui/pagination-controls";
17import { SidePanel } from "@/components/ui/side-panel";
18import { createFlash } from "@/lib/flash";
19import { loadListPage, totalPages as pagesOf } from "@/lib/list-page";
20import { personalNoteFiles } from "@/lib/note-source";
21import { useT } from "@/stores/preferences-context";
22
23const NOTE_PAGE_SIZE = 10;
24
25export default function NotesPage() {
26 return (
27 <RouteGuard>
28 <NotesContent />
29 </RouteGuard>
30 );
31}
32
33function NotesContent() {
34 const t = useT();
35 const location = useLocation();
36 const navigate = useNavigate();
37 const [error, setError] = createSignal("");
38 const [flash, setFlash] = createFlash();
39 const [importOpen, setImportOpen] = createSignal(location().searchStr.includes("action=import"));
40 createEffect(() => {
41 // Old deep link: the new-note side panel is now its own page.
42 if (location().searchStr.includes("action=new")) void navigate({ to: "/notes/new", replace: true });
43 if (location().searchStr.includes("action=import")) setImportOpen(true);
44 });
45 const [page, setPage] = createSignal(0);
46
47 const [list, { refetch }] = createResource(
48 () => page(),
49 async (currentPage) =>
50 loadListPage({
51 page: currentPage,
52 pageSize: NOTE_PAGE_SIZE,
53 clientMode: false,
54 fetch: getNotes,
55 }),
56 );
57
58 const total = () => list()?.total ?? 0;
59 const pageItems = () => list()?.items ?? [];
60 const totalPages = createMemo(() => pagesOf(total(), NOTE_PAGE_SIZE));
61 const safePage = createMemo(() => Math.min(page(), totalPages() - 1));
62
63 const wrap = async (fn: () => Promise<void>, okMessage: string) => {
64 setError("");
65 try {
66 await fn();
67 await refetch();
68 setFlash(okMessage);
69 } catch (err) {
70 setError(formatApiError(err));
71 }
72 };
73
74 return (
75 <div class="space-y-6">
76 <SidePanel open={importOpen()} onOpenChange={setImportOpen} title={t("notes.import")}>
77 <NoteImportPanel
78 onCancel={() => setImportOpen(false)}
79 onImport={async (importedTitle, importedMarkdown) => {
80 setError("");
81 await postNote({
82 title: importedTitle,
83 content: importedMarkdown || undefined,
84 });
85 await refetch();
86 setImportOpen(false);
87 setFlash(t("common.created"));
88 }}
89 />
90 </SidePanel>
91
92 <div class="space-y-4">
93 <div class="flex flex-wrap items-center justify-end gap-2 rounded-xl border border-border-line bg-surface-base p-3 shadow-xs">
94 <Button type="button" size="sm" class="rounded-lg" onClick={() => void navigate({ to: "/notes/new" })}>
95 <IconPlus class="h-4 w-4" />
96 {t("notes.new")}
97 </Button>
98 <Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setImportOpen(true)}>
99 <IconUploadCloud class="h-4 w-4" />
100 {t("notes.import")}
101 </Button>
102 </div>
103 <div class="space-y-4">
104 <Show when={flash()}>
105 <Alert variant="success">{flash()}</Alert>
106 </Show>
107 <Show when={error() && !importOpen()}>
108 <Alert variant="destructive">{error()}</Alert>
109 </Show>
110 <Suspense fallback={<PageSpinner />}>
111 <Show when={list.error}>
112 <Alert variant="destructive">{formatApiError(list.error)}</Alert>
113 </Show>
114 <Show when={list()}>
115 <NoteList
116 notes={pageItems()}
117 source={personalNoteFiles}
118 onOpen={(note) => void navigate({ to: "/notes/$id", params: { id: note.id } })}
119 emptyTitle={t("dashboard.emptyNotesTitle")}
120 emptyDescription={t("notes.empty")}
121 onUpdate={(id, values) =>
122 wrap(async () => {
123 await patchNoteById(id, values);
124 }, t("common.saved"))
125 }
126 onDelete={(id) =>
127 wrap(async () => {
128 await deleteNoteById(id);
129 }, t("common.deleted"))
130 }
131 />
132 <Show when={total() > NOTE_PAGE_SIZE}>
133 <PaginationControls page={safePage()} totalPages={totalPages()} onPageChange={setPage} />
134 </Show>
135 </Show>
136 </Suspense>
137 </div>
138 </div>
139 </div>
140 );
141}