forge
Explore
Log in

Don't you dare go Hollow

📎 shared file-type metadata across notes and homework

Browse files at this commit

c866b7e8797a542ca6a2f3e925992df8c0c3b023Burak BoduroğluParent: 678299e
  • refactor
    • 📎 shared file-type metadata across notes and homework
Show full message
refactor: 📎 shared file-type metadata across notes and homework
- file-type.tsx: extension + icon/badge mapping extracted from
  note-files-panel's local copy, reused by homework panels
- homework answer moves to the rich text editor with rich display on
  the submissions side; icon-only download/delete actions
- submission file lists become the compact thumbnail grid with a count
  badge, collapsed to the header row until a file exists
src/components/homework/homework-submission-panel.tsxmodified+39 -31
@@ -12,11 +12,12 @@ import { getSettings } from "@/api/settings";
12 import { Alert } from "@/components/ui/alert";12 import { Alert } from "@/components/ui/alert";
13 import { Badge } from "@/components/ui/badge";13 import { Badge } from "@/components/ui/badge";
14 import { Button } from "@/components/ui/button";14 import { Button } from "@/components/ui/button";
15-import { EmptyState } from "@/components/ui/empty-state";
16 import { IconDownload, IconTrash, IconUploadCloud } from "@/components/ui/icons";15 import { IconDownload, IconTrash, IconUploadCloud } from "@/components/ui/icons";
17 import { PageSpinner } from "@/components/ui/page-spinner";16 import { PageSpinner } from "@/components/ui/page-spinner";
18-import { Textarea } from "@/components/ui/textarea";17+import { RichTextEditor } from "@/components/ui/rich-text-editor";
18+import { cn } from "@/lib/cn";
19 import { createFlash } from "@/lib/flash";19 import { createFlash } from "@/lib/flash";
20+import { fileTypeMeta } from "@/lib/file-type";
20 import { formatDateTime } from "@/lib/format";21 import { formatDateTime } from "@/lib/format";
21 import { formatBytes, maxUploadBytes } from "@/lib/upload-limits";22 import { formatBytes, maxUploadBytes } from "@/lib/upload-limits";
22 import { usePreferences, useT } from "@/stores/preferences-context";23 import { usePreferences, useT } from "@/stores/preferences-context";
@@ -54,6 +55,7 @@ export function HomeworkSubmissionPanel(props: { homeworkId: string }) {
54 },55 },
55 );56 );
56 const maxFileBytes = () => maxUploadBytes(settings());57 const maxFileBytes = () => maxUploadBytes(settings());
58+ const hasFiles = () => (submission()?.files.length ?? 0) > 0;
57 59
58 createEffect(() => setText(submission()?.text ?? ""));60 createEffect(() => setText(submission()?.text ?? ""));
59 61
@@ -139,51 +141,57 @@ export function HomeworkSubmissionPanel(props: { homeworkId: string }) {
139 )}141 )}
140 </Show>142 </Show>
141 <form class="space-y-3" onSubmit={(event) => void save(event)}>143 <form class="space-y-3" onSubmit={(event) => void save(event)}>
142- <Textarea rows={6} maxlength={10000} value={text()} onInput={(event) => setText(event.currentTarget.value)} placeholder={t("homework.answerPlaceholder")} />144+ <RichTextEditor value={text()} onChange={setText} placeholder={t("homework.answerPlaceholder")} minHeight="min-h-32" />
143 <Button type="submit" class="rounded-lg" disabled={pending()}>{t("common.save")}</Button>145 <Button type="submit" class="rounded-lg" disabled={pending()}>{t("common.save")}</Button>
144 </form>146 </form>
145- <div class="space-y-3 rounded-lg border bg-background/70 p-3">147+ <div class={cn("rounded-lg border bg-background/70 p-3", hasFiles() && "space-y-3")}>
146 <div class="flex flex-wrap items-center justify-between gap-2">148 <div class="flex flex-wrap items-center justify-between gap-2">
147- <p class="text-sm font-medium">{t("notes.files")}</p>149+ <div class="flex items-center gap-2">
150+ <p class="text-sm font-medium">{t("notes.files")}</p>
151+ <Show when={hasFiles()}>
152+ <Badge variant="secondary" class="rounded-full">{submission()!.files.length}</Badge>
153+ </Show>
154+ </div>
148 <input ref={(el) => { input = el; }} type="file" class="hidden" disabled={pending()} onChange={(event) => void upload(event.currentTarget.files?.[0])} />155 <input ref={(el) => { input = el; }} type="file" class="hidden" disabled={pending()} onChange={(event) => void upload(event.currentTarget.files?.[0])} />
149 <Button type="button" size="sm" variant="outline" class="rounded-lg" disabled={pending()} onClick={() => input?.click()}>156 <Button type="button" size="sm" variant="outline" class="rounded-lg" disabled={pending()} onClick={() => input?.click()}>
150 <IconUploadCloud class="h-4 w-4" />157 <IconUploadCloud class="h-4 w-4" />
151 {t("notes.addFile")}158 {t("notes.addFile")}
152 </Button>159 </Button>
153 </div>160 </div>
154- <Show when={(submission()?.files ?? []).length > 0} fallback={<EmptyState title={t("notes.noFiles")} />}>161+ {/* Collapsed to just the header row above until a file exists — the list opens the moment one is added. */}
155- <ul class="space-y-2">162+ <Show when={hasFiles()}>
163+ <ul class="grid grid-cols-1 gap-2 sm:grid-cols-2">
156 <For each={submission()?.files ?? []}>164 <For each={submission()?.files ?? []}>
157- {(file) => (165+ {(file) => {
158- <li class="flex flex-wrap items-center justify-between gap-2 rounded-md border bg-card px-3 py-2 text-sm">166+ const meta = fileTypeMeta(file);
159- <div class="min-w-0">167+ return (
160- <p class="truncate font-medium">{file.name}</p>168+ <li class="flex items-center gap-2 rounded-lg border bg-card px-2.5 py-2 text-sm">
161- <p class="text-xs text-muted-foreground">{formatBytes(file.size)}</p>169+ <span class={`flex h-8 w-8 shrink-0 items-center justify-center rounded-md border ${meta.class}`}>{meta.icon}</span>
162- </div>170+ <div class="min-w-0 flex-1">
163- <div class="flex items-center gap-1">171+ <p class="truncate font-medium">{file.name}</p>
172+ <p class="text-xs text-muted-foreground">{formatBytes(file.size)}</p>
173+ </div>
164 <a href={getHomeworkSubmissionFileUrl(props.homeworkId, file.id)} download={file.name}>174 <a href={getHomeworkSubmissionFileUrl(props.homeworkId, file.id)} download={file.name}>
165- <Button type="button" size="sm" variant="ghost" class="rounded-lg">175+ <Button type="button" size="icon" variant="ghost" class="h-7 w-7 rounded-lg" title={t("notes.downloadFile")}>
166- <IconDownload class="h-4 w-4" />176+ <IconDownload class="h-3.5 w-3.5" />
167- {t("notes.downloadFile")}
168 </Button>177 </Button>
169 </a>178 </a>
170- <Button type="button" size="sm" variant="ghost" class="rounded-lg text-destructive hover:text-destructive" disabled={pending()} onClick={() => void removeFile(file.id)}>179+ <Button type="button" size="icon" variant="ghost" class="h-7 w-7 rounded-lg text-destructive hover:text-destructive" disabled={pending()} title={t("common.delete")} onClick={() => void removeFile(file.id)}>
171- <IconTrash class="h-4 w-4" />180+ <IconTrash class="h-3.5 w-3.5" />
172- {t("common.delete")}
173 </Button>181 </Button>
174- </div>182+ </li>
175- </li>183+ );
176- )}184+ }}
177 </For>185 </For>
178 </ul>186 </ul>
179- </Show>187+ <Show when={submission()}>
180- <Show when={submission()}>188+ {(row) => (
181- {(row) => (189+ <p class="text-xs text-muted-foreground">
182- <p class="text-xs text-muted-foreground">190+ {t("homework.submittedAt")}: {formatDateTime(row().updated_at, locale())}
183- {t("homework.submittedAt")}: {formatDateTime(row().updated_at, locale())}191+ <Show when={row().late}> · {t("homework.late")}</Show>
184- <Show when={row().late}> · {t("homework.late")}</Show>192+ </p>
185- </p>193+ )}
186- )}194+ </Show>
187 </Show>195 </Show>
188 </div>196 </div>
189 </Suspense>197 </Suspense>
src/components/homework/homework-submissions-panel.tsxmodified+34 -21
@@ -16,6 +16,7 @@ import { Select } from "@/components/ui/select";
16 import { SidePanel } from "@/components/ui/side-panel";16 import { SidePanel } from "@/components/ui/side-panel";
17 import { TableRowActions } from "@/components/ui/table-row-actions";17 import { TableRowActions } from "@/components/ui/table-row-actions";
18 import { createFlash } from "@/lib/flash";18 import { createFlash } from "@/lib/flash";
19+import { fileTypeMeta } from "@/lib/file-type";
19 import { formatDateTime } from "@/lib/format";20 import { formatDateTime } from "@/lib/format";
20 import { formatBytes } from "@/lib/upload-limits";21 import { formatBytes } from "@/lib/upload-limits";
21 import { usePreferences, useT } from "@/stores/preferences-context";22 import { usePreferences, useT } from "@/stores/preferences-context";
@@ -179,31 +180,43 @@ export function HomeworkSubmissionsPanel(props: { homeworkId: string; courseId:
179 <div class="space-y-4">180 <div class="space-y-4">
180 <div class="rounded-lg border bg-muted/20 p-3">181 <div class="rounded-lg border bg-muted/20 p-3">
181 <p class="text-xs text-muted-foreground">{t("exams.textAnswer")}</p>182 <p class="text-xs text-muted-foreground">{t("exams.textAnswer")}</p>
182- <p class="mt-2 whitespace-pre-wrap text-sm">{submission().text || t("homework.noAnswer")}</p>183+ <Show
184+ when={submission().text}
185+ fallback={<p class="mt-2 text-sm text-muted-foreground">{t("homework.noAnswer")}</p>}
186+ >
187+ {(text) => <div class="mt-2 text-sm leading-relaxed" innerHTML={text()} />}
188+ </Show>
183 </div>189 </div>
184- <div class="space-y-2 rounded-lg border bg-background/70 p-3">190+ {/* Skipped entirely (not just an empty placeholder) when there are no files — this is a read-only view, so a "no files" row would just be dead weight. */}
185- <p class="text-sm font-medium">{t("notes.files")}</p>191+ <Show when={submission().files.length > 0}>
186- <Show when={submission().files.length > 0} fallback={<p class="text-sm text-muted-foreground">{t("notes.noFiles")}</p>}>192+ <div class="space-y-2 rounded-lg border bg-background/70 p-3">
187- <ul class="space-y-2">193+ <div class="flex items-center gap-2">
194+ <p class="text-sm font-medium">{t("notes.files")}</p>
195+ <Badge variant="secondary" class="rounded-full">{submission().files.length}</Badge>
196+ </div>
197+ <ul class="grid grid-cols-1 gap-2 sm:grid-cols-2">
188 <For each={submission().files}>198 <For each={submission().files}>
189- {(file) => (199+ {(file) => {
190- <li class="flex flex-wrap items-center justify-between gap-2 rounded-md border bg-card px-3 py-2 text-sm">200+ const meta = fileTypeMeta(file);
191- <div class="min-w-0">201+ return (
192- <p class="truncate font-medium">{file.name}</p>202+ <li class="flex items-center gap-2 rounded-lg border bg-card px-2.5 py-2 text-sm">
193- <p class="text-xs text-muted-foreground">{formatBytes(file.size)}</p>203+ <span class={`flex h-8 w-8 shrink-0 items-center justify-center rounded-md border ${meta.class}`}>{meta.icon}</span>
194- </div>204+ <div class="min-w-0 flex-1">
195- <a href={getHomeworkRosterSubmissionFileUrl(props.homeworkId, viewTarget()!.user, file.id)} download={file.name}>205+ <p class="truncate font-medium">{file.name}</p>
196- <Button type="button" size="sm" variant="ghost" class="rounded-lg">206+ <p class="text-xs text-muted-foreground">{formatBytes(file.size)}</p>
197- <IconDownload class="h-4 w-4" />207+ </div>
198- {t("notes.downloadFile")}208+ <a href={getHomeworkRosterSubmissionFileUrl(props.homeworkId, viewTarget()!.user, file.id)} download={file.name}>
199- </Button>209+ <Button type="button" size="icon" variant="ghost" class="h-7 w-7 rounded-lg" title={t("notes.downloadFile")}>
200- </a>210+ <IconDownload class="h-3.5 w-3.5" />
201- </li>211+ </Button>
202- )}212+ </a>
213+ </li>
214+ );
215+ }}
203 </For>216 </For>
204 </ul>217 </ul>
205- </Show>218+ </div>
206- </div>219+ </Show>
207 </div>220 </div>
208 )}221 )}
209 </Show>222 </Show>
src/components/notes/note-files-panel.tsxmodified+4 -32
@@ -1,4 +1,4 @@
1-import { For, Show, Suspense, createEffect, createMemo, createResource, createSignal, lazy, type JSX } from "solid-js";1+import { For, Show, Suspense, createEffect, createMemo, createResource, createSignal, lazy } from "solid-js";
2 import { deleteNoteFileById } from "@/api/notes";2 import { deleteNoteFileById } from "@/api/notes";
3 import { getNoteFileBlob } from "@/api/notes";3 import { getNoteFileBlob } from "@/api/notes";
4 import { getNoteFileUrl } from "@/api/notes";4 import { getNoteFileUrl } from "@/api/notes";
@@ -14,23 +14,12 @@ import { Button } from "@/components/ui/button";
14 import { ConfirmDialog } from "@/components/ui/confirm-dialog";14 import { ConfirmDialog } from "@/components/ui/confirm-dialog";
15 import { FormDialog } from "@/components/ui/form-dialog";15 import { FormDialog } from "@/components/ui/form-dialog";
16 import { createFlash } from "@/lib/flash";16 import { createFlash } from "@/lib/flash";
17-import {17+import { IconDownload, IconEdit, IconEye, IconPlus, IconTrash } from "@/components/ui/icons";
18- IconDownload,
19- IconEdit,
20- IconEye,
21- IconFileAudio,
22- IconFileImage,
23- IconFileSpreadsheet,
24- IconFileText,
25- IconFileVideo,
26- IconNote,
27- IconPlus,
28- IconTrash,
29-} from "@/components/ui/icons";
30 import { PageSpinner } from "@/components/ui/page-spinner";18 import { PageSpinner } from "@/components/ui/page-spinner";
31 import { PaginationControls } from "@/components/ui/pagination-controls";19 import { PaginationControls } from "@/components/ui/pagination-controls";
32 import { TableRowActions } from "@/components/ui/table-row-actions";20 import { TableRowActions } from "@/components/ui/table-row-actions";
33 import { cn } from "@/lib/cn";21 import { cn } from "@/lib/cn";
22+import { fileTypeMeta } from "@/lib/file-type";
34 import { formatBytes, maxUploadBytes } from "@/lib/upload-limits";23 import { formatBytes, maxUploadBytes } from "@/lib/upload-limits";
35 import { useT } from "@/stores/preferences-context";24 import { useT } from "@/stores/preferences-context";
36 25
@@ -44,23 +33,6 @@ const FILE_PAGE_SIZE = 4;
44 const DRAWING_SUFFIX = ".hzdraw.png";33 const DRAWING_SUFFIX = ".hzdraw.png";
45 const isDrawing = (file: NoteFile) => file.name.toLowerCase().endsWith(DRAWING_SUFFIX);34 const isDrawing = (file: NoteFile) => file.name.toLowerCase().endsWith(DRAWING_SUFFIX);
46 35
47-function extension(name: string): string {
48- const ext = name.split(".").pop()?.toLowerCase();
49- return ext && ext !== name.toLowerCase() ? ext : "file";
50-}
51-
52-function fileMeta(file: NoteFile): { label: string; class: string; icon: JSX.Element } {
53- const type = file.content_type.toLowerCase();
54- const ext = extension(file.name);
55- if (type.startsWith("image/")) return { label: ext, class: "border-sky-500/25 bg-sky-500/10 text-sky-700 dark:text-sky-300", icon: <IconFileImage class="h-8 w-8" /> };
56- if (type.startsWith("video/")) return { label: ext, class: "border-violet-500/25 bg-violet-500/10 text-violet-700 dark:text-violet-300", icon: <IconFileVideo class="h-8 w-8" /> };
57- if (type.startsWith("audio/")) return { label: ext, class: "border-fuchsia-500/25 bg-fuchsia-500/10 text-fuchsia-700 dark:text-fuchsia-300", icon: <IconFileAudio class="h-8 w-8" /> };
58- if (type === "application/pdf" || ext === "pdf") return { label: "pdf", class: "border-rose-500/25 bg-rose-500/10 text-rose-700 dark:text-rose-300", icon: <IconFileText class="h-8 w-8" /> };
59- if (["xls", "xlsx", "csv"].includes(ext) || type.includes("spreadsheet")) return { label: ext, class: "border-emerald-500/25 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300", icon: <IconFileSpreadsheet class="h-8 w-8" /> };
60- if (["doc", "docx", "txt", "md", "rtf"].includes(ext) || type.startsWith("text/") || type.includes("word")) return { label: ext, class: "border-blue-500/25 bg-blue-500/10 text-blue-700 dark:text-blue-300", icon: <IconFileText class="h-8 w-8" /> };
61- return { label: ext, class: "border-border bg-background text-muted-foreground", icon: <IconNote class="h-8 w-8" /> };
62-}
63-
64 export function NoteFilesPanel(props: { noteId: string; active: boolean }) {36 export function NoteFilesPanel(props: { noteId: string; active: boolean }) {
65 let input: HTMLInputElement | undefined;37 let input: HTMLInputElement | undefined;
66 const t = useT();38 const t = useT();
@@ -214,7 +186,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) {
214 <ul class="grid grid-cols-1 gap-3 rounded-md border border-border/70 bg-background/70 p-2 sm:grid-cols-2">186 <ul class="grid grid-cols-1 gap-3 rounded-md border border-border/70 bg-background/70 p-2 sm:grid-cols-2">
215 <For each={pageFiles()}>187 <For each={pageFiles()}>
216 {(file) => {188 {(file) => {
217- const meta = fileMeta(file);189+ const meta = fileTypeMeta(file, "h-8 w-8");
218 return (190 return (
219 <li class="group overflow-hidden rounded-xl border border-border/70 bg-card text-sm shadow-xs transition-colors hover:border-amber-500/40">191 <li class="group overflow-hidden rounded-xl border border-border/70 bg-card text-sm shadow-xs transition-colors hover:border-amber-500/40">
220 <div class="relative h-28 bg-muted/25">192 <div class="relative h-28 bg-muted/25">
src/lib/file-type.tsxadded+29 -0
@@ -0,0 +1,29 @@
1+import type { JSX } from "solid-js";
2+import {
3+ IconFileAudio,
4+ IconFileImage,
5+ IconFileSpreadsheet,
6+ IconFileText,
7+ IconFileVideo,
8+ IconNote,
9+} from "@/components/ui/icons";
10+
11+export function fileExtension(name: string): string {
12+ const ext = name.split(".").pop()?.toLowerCase();
13+ return ext && ext !== name.toLowerCase() ? ext : "file";
14+}
15+
16+export type FileTypeMeta = { label: string; class: string; icon: JSX.Element };
17+
18+/** Icon + badge color for a file, by content type / extension — shared across notes and homework file lists. */
19+export function fileTypeMeta(file: { name: string; content_type: string }, iconClass = "h-5 w-5"): FileTypeMeta {
20+ const type = file.content_type.toLowerCase();
21+ const ext = fileExtension(file.name);
22+ if (type.startsWith("image/")) return { label: ext, class: "border-sky-500/25 bg-sky-500/10 text-sky-700 dark:text-sky-300", icon: <IconFileImage class={iconClass} /> };
23+ if (type.startsWith("video/")) return { label: ext, class: "border-violet-500/25 bg-violet-500/10 text-violet-700 dark:text-violet-300", icon: <IconFileVideo class={iconClass} /> };
24+ if (type.startsWith("audio/")) return { label: ext, class: "border-fuchsia-500/25 bg-fuchsia-500/10 text-fuchsia-700 dark:text-fuchsia-300", icon: <IconFileAudio class={iconClass} /> };
25+ if (type === "application/pdf" || ext === "pdf") return { label: "pdf", class: "border-rose-500/25 bg-rose-500/10 text-rose-700 dark:text-rose-300", icon: <IconFileText class={iconClass} /> };
26+ if (["xls", "xlsx", "csv"].includes(ext) || type.includes("spreadsheet")) return { label: ext, class: "border-emerald-500/25 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300", icon: <IconFileSpreadsheet class={iconClass} /> };
27+ if (["doc", "docx", "txt", "md", "rtf"].includes(ext) || type.startsWith("text/") || type.includes("word")) return { label: ext, class: "border-blue-500/25 bg-blue-500/10 text-blue-700 dark:text-blue-300", icon: <IconFileText class={iconClass} /> };
28+ return { label: ext, class: "border-border bg-background text-muted-foreground", icon: <IconNote class={iconClass} /> };
29+}