|
| 1 | +import Dialog from "@corvu/dialog"; |
| 2 | +import { For, Show, createMemo, createSignal } from "solid-js"; |
| 3 | + |
| 4 | +import type { TheoryLibrary, TheoryMeta } from "../stdlib"; |
| 5 | +import type { TheoryId } from "../theory"; |
| 6 | + |
| 7 | +import "./theory_selector.css"; |
| 8 | + |
| 9 | +type TheorySelectorProps = { |
| 10 | + theory: TheoryMeta | undefined; |
| 11 | + setTheory: (theory: TheoryId | undefined) => void; |
| 12 | + theories: TheoryLibrary; |
| 13 | +}; |
| 14 | + |
| 15 | +export function TheorySelectorDialog( |
| 16 | + props: { |
| 17 | + disabled?: boolean; |
| 18 | + } & TheorySelectorProps, |
| 19 | +) { |
| 20 | + const [theorySelectorOpen, setTheorySelectorOpen] = createSignal(false); |
| 21 | + |
| 22 | + return ( |
| 23 | + <Dialog open={theorySelectorOpen()} onOpenChange={setTheorySelectorOpen}> |
| 24 | + <Dialog.Trigger class="theory-selector-button" disabled={props.disabled}> |
| 25 | + <Show |
| 26 | + when={props.theory} |
| 27 | + fallback={<span class="placeholder">Choose a logic</span>} |
| 28 | + > |
| 29 | + {props.theory?.name} |
| 30 | + </Show> |
| 31 | + </Dialog.Trigger> |
| 32 | + <Dialog.Portal> |
| 33 | + <Dialog.Overlay class="overlay" /> |
| 34 | + <Dialog.Content class="popup"> |
| 35 | + <TheorySelector |
| 36 | + theory={props.theory} |
| 37 | + setTheory={(id) => { |
| 38 | + props.setTheory(id); |
| 39 | + setTheorySelectorOpen(false); |
| 40 | + }} |
| 41 | + theories={props.theories} |
| 42 | + /> |
| 43 | + </Dialog.Content> |
| 44 | + </Dialog.Portal> |
| 45 | + </Dialog> |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +export function TheorySelector(props: TheorySelectorProps) { |
| 50 | + const groupedTheories = createMemo(() => { |
| 51 | + const grouped = new Map<string, TheoryMeta[]>(); |
| 52 | + |
| 53 | + for (const theory of props.theories.metadata()) { |
| 54 | + const category = theory.divisionCategory ?? "Other"; |
| 55 | + const group = grouped.get(category) || []; |
| 56 | + group.push(theory); |
| 57 | + grouped.set(category, group); |
| 58 | + } |
| 59 | + |
| 60 | + return Array.from(grouped.entries()).sort((a, b) => a[0].localeCompare(b[0])); |
| 61 | + }); |
| 62 | + |
| 63 | + return ( |
| 64 | + <div class="theory-selector"> |
| 65 | + <For each={groupedTheories()}> |
| 66 | + {([category, theories]) => ( |
| 67 | + <div class="division"> |
| 68 | + <h4 class="division-name">{category}</h4> |
| 69 | + <For each={theories}> |
| 70 | + {(meta) => ( |
| 71 | + <div class="theory"> |
| 72 | + <input |
| 73 | + type="radio" |
| 74 | + name="theory" |
| 75 | + id={meta.id} |
| 76 | + value={meta.id} |
| 77 | + onchange={(evt) => { |
| 78 | + const id = evt.target.value as TheoryId; |
| 79 | + props.setTheory(id ? id : undefined); |
| 80 | + }} |
| 81 | + /> |
| 82 | + <label for={meta.id}> |
| 83 | + <div class="name">{meta.name}</div> |
| 84 | + <div class="description">{meta.description}</div> |
| 85 | + </label> |
| 86 | + </div> |
| 87 | + )} |
| 88 | + </For> |
| 89 | + </div> |
| 90 | + )} |
| 91 | + </For> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +} |
0 commit comments