forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

course-notes-panel.tsx164 lines, 5.8 KiBHistoryRawDownload
1import { Show, Suspense, createEffect, createMemo, createSignal } from "solid-js";
2import { createResource } from "@/lib/create-resource";
3import { deleteCourseNoteById, getCourseNotes, patchCourseNoteById, postCourseNote, postCourseNoteFile } from "@/api/course-notes";
4import { formatApiError } from "@/api/client";
5import { NoteForm } from "@/components/notes/note-form";
6import { NoteList } from "@/components/notes/note-list";
7import { Alert } from "@/components/ui/alert";
8import { Button } from "@/components/ui/button";
9import { DataToolbar } from "@/components/ui/data-toolbar";
10import { IconPlus } from "@/components/ui/icons";
11import { PageSpinner } from "@/components/ui/page-spinner";
12import { PaginationControls } from "@/components/ui/pagination-controls";
13import { SidePanel } from "@/components/ui/side-panel";
14import { createFlash } from "@/lib/flash";
15import { courseNoteFiles } from "@/lib/note-source";
16import { matchesSearch } from "@/lib/search-text";
17import { useT } from "@/stores/preferences-context";
18
19const NOTE_PAGE_SIZE = 6;
20
21export function CourseNotesPanel(props: {
22 courseId: string;
23 canManage: boolean;
24 createOpen: boolean;
25 onCreateOpenChange: (open: boolean) => void;
26 onCountChange: (count: number) => void;
27}) {
28 const t = useT();
29 const [error, setError] = createSignal("");
30 const [flash, setFlash] = createFlash();
31 const [page, setPage] = createSignal(0);
32 const [search, setSearch] = createSignal("");
33
34 const [list, { refetch }] = createResource(
35 () => props.courseId,
36 (courseId) => getCourseNotes(courseId, { limit: 100 }),
37 );
38
39 const allNotes = () => list()?.items ?? [];
40 const filteredNotes = createMemo(() => {
41 const query = search().trim();
42 return query ? allNotes().filter((note) => matchesSearch(query, note.title, note.content)) : allNotes();
43 });
44 const total = () => filteredNotes().length;
45 const pageItems = createMemo(() => filteredNotes().slice(page() * NOTE_PAGE_SIZE, (page() + 1) * NOTE_PAGE_SIZE));
46 const totalPages = createMemo(() => Math.max(1, Math.ceil(total() / NOTE_PAGE_SIZE)));
47 const safePage = createMemo(() => Math.min(page(), totalPages() - 1));
48
49 createEffect(() => {
50 search();
51 setPage(0);
52 });
53
54 createEffect(() => {
55 const result = list();
56 if (result) props.onCountChange(result.total);
57 });
58
59 createEffect(() => {
60 if (page() > totalPages() - 1) setPage(totalPages() - 1);
61 });
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 <section class="min-w-0 space-y-4">
76 <Show when={flash()}>
77 <Alert variant="success">{flash()}</Alert>
78 </Show>
79 <Show when={error() && !props.createOpen}>
80 <Alert variant="destructive">{error()}</Alert>
81 </Show>
82
83 <div class="space-y-3 rounded-lg border border-border-line bg-surface-base p-3">
84 <DataToolbar
85 inline
86 searchValue={search()}
87 searchPlaceholder={t("common.searchPlaceholder")}
88 searchHint={t("search.hint.courseNotes")}
89 onSearchInput={setSearch}
90 actions={
91 <Show when={props.canManage}>
92 <Button type="button" size="sm" class="shrink-0 rounded-lg" onClick={() => props.onCreateOpenChange(true)}>
93 <IconPlus class="h-4 w-4" />{t("courseNotes.add")}
94 </Button>
95 </Show>
96 }
97 />
98 </div>
99
100 <Show when={props.canManage}>
101 <SidePanel guardUnsaved open={props.createOpen} onOpenChange={props.onCreateOpenChange} title={t("courseNotes.new")}>
102 <NoteForm
103 enableFiles
104 submitLabel={t("common.create")}
105 onCancel={() => props.onCreateOpenChange(false)}
106 onSubmit={async (values) => {
107 setError("");
108 const note = await postCourseNote({
109 course: props.courseId,
110 title: values.title,
111 content: values.content || undefined,
112 });
113 let failedUploads = 0;
114 for (const file of values.files) {
115 try {
116 await postCourseNoteFile(note.id, file);
117 } catch {
118 failedUploads += 1;
119 }
120 }
121 await refetch();
122 props.onCreateOpenChange(false);
123 if (failedUploads > 0) {
124 setError(t("notes.fileUploadPartial", { count: failedUploads }));
125 return;
126 }
127 setFlash(t("common.created"));
128 }}
129 />
130 </SidePanel>
131 </Show>
132
133 <div class="rounded-xl border border-border-line bg-surface-base p-3 shadow-xs sm:p-4">
134 <Suspense fallback={<PageSpinner />}>
135 <Show when={list.error}>
136 <Alert variant="destructive">{formatApiError(list.error)}</Alert>
137 </Show>
138 <Show when={list()}>
139 <NoteList
140 notes={pageItems()}
141 source={courseNoteFiles}
142 canManage={props.canManage}
143 emptyTitle={t("courseNotes.empty")}
144 emptyDescription={props.canManage ? t("courseNotes.emptyManageHint") : undefined}
145 onUpdate={(id, values) =>
146 wrap(async () => {
147 await patchCourseNoteById(id, values);
148 }, t("common.saved"))
149 }
150 onDelete={(id) =>
151 wrap(async () => {
152 await deleteCourseNoteById(id);
153 }, t("common.deleted"))
154 }
155 />
156 <Show when={total() > NOTE_PAGE_SIZE}>
157 <PaginationControls page={safePage()} totalPages={totalPages()} onPageChange={setPage} />
158 </Show>
159 </Show>
160 </Suspense>
161 </div>
162 </section>
163 );
164}