hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { For, Show } from "solid-js"; |
| 2 | import type { ModuleCatalog } from "@/api/client"; |
| 3 | import { Badge } from "@/components/ui/badge"; |
| 4 | import { moduleLabel, packageLabel } from "@/lib/module-labels"; |
| 5 | import { useT } from "@/stores/preferences-context"; |
| 6 | |
| 7 | /** |
| 8 | * The deployment catalog grouped by package, each module marked on/off for one |
| 9 | * school. Read-only without `onToggle` (a school admin's view); with it, every |
| 10 | * module row becomes a switch (the builder's switchboard). |
| 11 | */ |
| 12 | export function ModuleCatalogGrid(props: { |
| 13 | catalog: ModuleCatalog; |
| 14 | enabled: readonly string[]; |
| 15 | onToggle?: (module: string, next: boolean) => void; |
| 16 | onTogglePackage?: (pkg: string, next: boolean) => void; |
| 17 | disabled?: boolean; |
| 18 | /** One column regardless of viewport — for narrow hosts like a SidePanel. */ |
| 19 | stacked?: boolean; |
| 20 | }) { |
| 21 | const t = useT(); |
| 22 | const isOn = (module: string) => props.enabled.includes(module); |
| 23 | const requiresOf = (module: string) => props.catalog.modules.find((entry) => entry.module === module)?.requires ?? []; |
| 24 | |
| 25 | return ( |
| 26 | <div class={props.stacked ? "grid gap-4" : "grid gap-4 md:grid-cols-2"}> |
| 27 | <For each={props.catalog.packages}> |
| 28 | {(pkg) => { |
| 29 | const onCount = () => pkg.modules.filter(isOn).length; |
| 30 | return ( |
| 31 | <section class="data-shell space-y-3 p-4"> |
| 32 | <div class="flex items-center justify-between gap-3"> |
| 33 | <div class="min-w-0"> |
| 34 | <h2 class="truncate font-semibold text-text-strong">{packageLabel(pkg.package, t)}</h2> |
| 35 | <p class="text-xs text-text-subtle">{t("modules.enabledCount", { on: String(onCount()), total: String(pkg.modules.length) })}</p> |
| 36 | </div> |
| 37 | <Show when={props.onTogglePackage}> |
| 38 | {(toggle) => ( |
| 39 | <div class="flex shrink-0 gap-2"> |
| 40 | <button |
| 41 | type="button" |
| 42 | class="rounded-md px-2 py-1 text-xs font-medium text-primary-text hover:bg-muted/60 disabled:opacity-50" |
| 43 | disabled={props.disabled || onCount() === pkg.modules.length} |
| 44 | onClick={() => toggle()(pkg.package, true)} |
| 45 | > |
| 46 | {t("modules.enableAll")} |
| 47 | </button> |
| 48 | <button |
| 49 | type="button" |
| 50 | class="rounded-md px-2 py-1 text-xs font-medium text-muted-foreground hover:bg-muted/60 disabled:opacity-50" |
| 51 | disabled={props.disabled || onCount() === 0} |
| 52 | onClick={() => toggle()(pkg.package, false)} |
| 53 | > |
| 54 | {t("modules.disableAll")} |
| 55 | </button> |
| 56 | </div> |
| 57 | )} |
| 58 | </Show> |
| 59 | </div> |
| 60 | <ul class="divide-y divide-border-hairline"> |
| 61 | <For each={pkg.modules}> |
| 62 | {(module) => ( |
| 63 | <li class="flex items-center justify-between gap-3 py-2"> |
| 64 | <div class="min-w-0"> |
| 65 | <p class="truncate text-sm font-medium text-foreground">{moduleLabel(module, t)}</p> |
| 66 | <Show when={requiresOf(module).length > 0}> |
| 67 | <p class="truncate text-xs text-text-subtle"> |
| 68 | {t("modules.requires")}: {requiresOf(module).map((name) => moduleLabel(name, t)).join(", ")} |
| 69 | </p> |
| 70 | </Show> |
| 71 | </div> |
| 72 | <Show |
| 73 | when={props.onToggle} |
| 74 | fallback={ |
| 75 | <Badge variant={isOn(module) ? "success" : "secondary"} class="shrink-0 rounded-full"> |
| 76 | {isOn(module) ? t("modules.on") : t("modules.off")} |
| 77 | </Badge> |
| 78 | } |
| 79 | > |
| 80 | {(toggle) => ( |
| 81 | <label class="relative shrink-0 cursor-pointer" title={moduleLabel(module, t)}> |
| 82 | <input |
| 83 | type="checkbox" |
| 84 | class="peer sr-only" |
| 85 | aria-label={moduleLabel(module, t)} |
| 86 | checked={isOn(module)} |
| 87 | disabled={props.disabled} |
| 88 | onChange={(event) => { |
| 89 | const next = event.currentTarget.checked; |
| 90 | // The switch shows `enabled`, not the click: snap back |
| 91 | // now and let a successful write move it, so a refused |
| 92 | // (409) toggle never leaves it in the wrong position. |
| 93 | event.currentTarget.checked = isOn(module); |
| 94 | toggle()(module, next); |
| 95 | }} |
| 96 | /> |
| 97 | <span class="block h-6 w-10 rounded-full bg-input ring-1 ring-inset ring-black/5 transition-colors peer-checked:bg-primary peer-disabled:opacity-60 peer-focus-visible:ring-2 peer-focus-visible:ring-ring dark:ring-white/10" /> |
| 98 | <span class="pointer-events-none absolute left-1 top-1 h-4 w-4 rounded-full bg-white shadow-sm transition-transform peer-checked:translate-x-4" /> |
| 99 | </label> |
| 100 | )} |
| 101 | </Show> |
| 102 | </li> |
| 103 | )} |
| 104 | </For> |
| 105 | </ul> |
| 106 | </section> |
| 107 | ); |
| 108 | }} |
| 109 | </For> |
| 110 | </div> |
| 111 | ); |
| 112 | } |