forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

homework-queue-panel.tsx80 lines, 3.7 KiBHistoryRawDownload
1import { For, Show, createMemo } from "solid-js";
2import { Link } from "@tanstack/solid-router";
3import { createResource } from "@/lib/create-resource";
4import type { Homework } from "@/api/client";
5import { getHomeworkSubmissions } from "@/api/homework";
6import { EmptyInline } from "@/components/ui/empty-inline";
7import { IconChevronRight } from "@/components/ui/icons";
8import { formatDateTime } from "@/lib/format";
9import { queueCandidates, queueRow, type QueueRow } from "@/lib/homework-queue";
10import { usePreferences, useT } from "@/stores/preferences-context";
11
12/**
13 * TCH "what is waiting on me": for the teacher's own homework due around
14 * now, how many students have not handed in and how many hand-ins still need
15 * a grade — as sentences, each linking to that homework. Counts come from
16 * each homework's roster (`/homework/{id}/submissions`), read for a capped
17 * handful since there is no bulk endpoint; a failed read drops its row.
18 */
19export function HomeworkQueuePanel(props: { homework: Homework[]; teacherId: string; now: number }) {
20 const t = useT();
21 const { locale } = usePreferences();
22 const candidates = createMemo(() => queueCandidates(props.homework, props.teacherId, props.now));
23 const [rows] = createResource(
24 () => candidates().map((item) => item.id).join(",") || null,
25 async () => {
26 const settled = await Promise.all(
27 candidates().map(async (item) => {
28 try {
29 return queueRow(item, (await getHomeworkSubmissions(item.id)).items);
30 } catch {
31 return null;
32 }
33 }),
34 );
35 return settled.filter((row): row is QueueRow => row != null && row.missing + row.toGrade > 0);
36 },
37 );
38 const toGrade = () => (rows() ?? []).reduce((sum, row) => sum + row.toGrade, 0);
39
40 return (
41 <section class="flex flex-col gap-3 rounded-xl border border-border-line bg-surface-base p-4" aria-labelledby="homework-queue-heading">
42 <div class="flex flex-wrap items-baseline justify-between gap-2">
43 <h2 id="homework-queue-heading" class="text-base font-semibold tracking-tight text-text-strong">{t("homeworkQueue.title")}</h2>
44 <Show when={toGrade() > 0}>
45 <span class="text-xs font-medium text-warning-text">{t("homeworkQueue.toGradeTotal", { count: toGrade() })}</span>
46 </Show>
47 </div>
48 <Show
49 when={(rows() ?? []).length > 0}
50 fallback={<EmptyInline illustration="calendar-empty" title={t("homeworkQueue.empty")} />}
51 >
52 <ul class="divide-y divide-border-hairline">
53 <For each={rows()}>
54 {(row) => (
55 <li>
56 <Link
57 to="/homework/$id"
58 params={{ id: row.homework.id }}
59 class="flex items-center gap-3 py-2.5 outline-hidden hover:bg-muted/40 focus-visible:ring-2 focus-visible:ring-ring"
60 >
61 <span class="min-w-0 flex-1">
62 <span class="block truncate text-sm font-medium text-text-default">{row.homework.title}</span>
63 <span class="block text-xs text-muted-foreground">
64 {[
65 row.missing > 0 ? t("homeworkQueue.missing", { count: row.missing }) : null,
66 row.toGrade > 0 ? t("homeworkQueue.toGrade", { count: row.toGrade }) : null,
67 ].filter(Boolean).join(" · ")}
68 </span>
69 </span>
70 <span class="shrink-0 text-xs tabular-nums text-muted-foreground">{formatDateTime(row.homework.due_at, locale())}</span>
71 <IconChevronRight class="h-4 w-4 shrink-0 text-muted-foreground/60" />
72 </Link>
73 </li>
74 )}
75 </For>
76 </ul>
77 </Show>
78 </section>
79 );
80}