hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { For, Show } from "solid-js"; |
| 2 | import { Link } from "@tanstack/solid-router"; |
| 3 | import { createResource } from "@/lib/create-resource"; |
| 4 | import { getAcademicYears } from "@/api/academic-years"; |
| 5 | import { getClasses } from "@/api/classes"; |
| 6 | import { getCourses } from "@/api/courses"; |
| 7 | import { getUserSearch } from "@/api/users"; |
| 8 | import { IconCheck, IconChevronRight } from "@/components/ui/icons"; |
| 9 | import { cn } from "@/lib/cn"; |
| 10 | import type { MessageKey } from "@/i18n/messages"; |
| 11 | import { useT } from "@/stores/preferences-context"; |
| 12 | |
| 13 | type Step = { |
| 14 | id: string; |
| 15 | labelKey: MessageKey; |
| 16 | to: string; |
| 17 | module?: string; |
| 18 | /** How many exist now; the step is done at one or more. */ |
| 19 | count: () => Promise<number>; |
| 20 | }; |
| 21 | |
| 22 | const STEPS: Step[] = [ |
| 23 | { id: "year", labelKey: "setup.academicYear", to: "/management/academic-years", count: async () => (await getAcademicYears({ limit: 1 })).total }, |
| 24 | { id: "teachers", labelKey: "setup.teachers", to: "/management/teachers", count: async () => (await getUserSearch("", undefined, "teacher", { limit: 1 })).total }, |
| 25 | { id: "students", labelKey: "setup.students", to: "/management/students", count: async () => (await getUserSearch("", undefined, "student", { limit: 1 })).total }, |
| 26 | { id: "classes", labelKey: "setup.classes", to: "/management/classes", module: "classes", count: async () => (await getClasses({ limit: 1 })).total }, |
| 27 | { id: "courses", labelKey: "setup.courses", to: "/courses", module: "courses", count: async () => (await getCourses({ limit: 1 })).total }, |
| 28 | ]; |
| 29 | |
| 30 | /** |
| 31 | * ADM "school setup": the five things a new school needs before the rest of |
| 32 | * the app has anything to show, each ticked by a real count (at least one |
| 33 | * exists) and linking to where it is added. A read that fails leaves its step |
| 34 | * unknown rather than claiming it done or missing. Once every step is done |
| 35 | * the panel steps aside. |
| 36 | */ |
| 37 | export function SetupChecklistPanel(props: { moduleOn: (module: string) => boolean }) { |
| 38 | const t = useT(); |
| 39 | const [status] = createResource(async () => { |
| 40 | const steps = STEPS.filter((step) => !step.module || props.moduleOn(step.module)); |
| 41 | return Promise.all( |
| 42 | steps.map(async (step) => ({ step, done: await step.count().then((total) => total > 0).catch(() => null) })), |
| 43 | ); |
| 44 | }); |
| 45 | const doneCount = () => (status() ?? []).filter((row) => row.done === true).length; |
| 46 | const allDone = () => (status() ?? []).length > 0 && doneCount() === status()!.length; |
| 47 | |
| 48 | return ( |
| 49 | <Show when={status() && !allDone()}> |
| 50 | <section class="flex flex-col gap-3 rounded-xl border border-border-line bg-surface-base p-4" aria-labelledby="setup-heading"> |
| 51 | <div class="flex items-baseline justify-between gap-2"> |
| 52 | <h2 id="setup-heading" class="text-base font-semibold tracking-tight text-text-strong">{t("setup.title")}</h2> |
| 53 | <span class="text-xs font-medium text-muted-foreground">{t("setup.progress", { done: doneCount(), total: status()!.length })}</span> |
| 54 | </div> |
| 55 | <ol class="grid gap-2 sm:grid-cols-2 lg:grid-cols-5"> |
| 56 | <For each={status()}> |
| 57 | {(row) => ( |
| 58 | <li> |
| 59 | <Link |
| 60 | to={row.step.to as never} |
| 61 | class={cn( |
| 62 | "flex h-full items-center gap-2.5 rounded-lg border px-3 py-2.5 text-sm outline-hidden transition-colors hover:bg-muted/40 focus-visible:ring-2 focus-visible:ring-ring", |
| 63 | row.done ? "border-success/30 text-muted-foreground" : "border-border-line text-text-default", |
| 64 | )} |
| 65 | > |
| 66 | <span |
| 67 | class={cn( |
| 68 | "flex h-5 w-5 shrink-0 items-center justify-center rounded-full border", |
| 69 | row.done ? "border-success bg-success text-white" : "border-border-line", |
| 70 | )} |
| 71 | aria-hidden="true" |
| 72 | > |
| 73 | <Show when={row.done}> |
| 74 | <IconCheck class="h-3 w-3" /> |
| 75 | </Show> |
| 76 | </span> |
| 77 | <span class="min-w-0 flex-1"> |
| 78 | <span class="block truncate font-medium">{t(row.step.labelKey)}</span> |
| 79 | <span class="block text-xs text-muted-foreground"> |
| 80 | {row.done === true ? t("setup.done") : row.done === false ? t("setup.todo") : t("setup.unknown")} |
| 81 | </span> |
| 82 | </span> |
| 83 | <IconChevronRight class="h-4 w-4 shrink-0 text-muted-foreground/60" /> |
| 84 | </Link> |
| 85 | </li> |
| 86 | )} |
| 87 | </For> |
| 88 | </ol> |
| 89 | </section> |
| 90 | </Show> |
| 91 | ); |
| 92 | } |