import { For, Show, createMemo, createSignal } from "solid-js"; import { formatApiError } from "@/api/client"; import type { Subject } from "@/api/client"; import { Alert } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { TOOLBAR_CONTROL } from "@/components/ui/data-toolbar"; import { IconPlus, IconX } from "@/components/ui/icons"; import { SearchableSelect } from "@/components/ui/searchable-select"; import { cn } from "@/lib/cn"; import { useT } from "@/stores/preferences-context"; /** * A selected-topic set (a grade template's, or a section's own) drawn as * chips; editors add from the catalog course's subjects and remove chips. */ export function SubjectSetEditor(props: { selected: Subject[]; /** The catalog course's subjects — the only ones a set may hold. */ available: Subject[]; canEdit: boolean; /** Either list is still loading: draw nothing rather than "no topics" / "all taken". */ loading?: boolean; onAdd: (subjectId: string) => Promise; onRemove: (subject: Subject) => Promise; }) { const t = useT(); const [pick, setPick] = createSignal(""); const [pending, setPending] = createSignal(false); const [error, setError] = createSignal(""); const addable = createMemo(() => { const taken = new Set(props.selected.map((subject) => subject.id)); return props.available.filter((subject) => !taken.has(subject.id)); }); const run = async (action: () => Promise) => { if (pending()) return; setError(""); setPending(true); try { await action(); } catch (err) { setError(formatApiError(err)); } finally { setPending(false); } }; return (
}> 0} fallback={

{t("instances.noSubjects")}

}>
    {(subject) => (
  • {subject.name}
  • )}
0} fallback={

{t("instances.allSubjectsSelected")}

}> {/* Picker and button are one control row: both take the toolbar pill so the button is not a 26px chip beside a 36px field. */}
({ value: subject.id, label: subject.name }))} />
{error()}
); }