Skip to content

Commit 22eb64b

Browse files
committed
ENH: Close theory selector dialog upon selection.
Also, refactor the dialog to not depend on the whole Automerge document.
1 parent ab852b2 commit 22eb64b

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

packages/frontend/src/document/model_document_editor.tsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import type { ChangeFn, DocHandle } from "@automerge/automerge-repo";
22
import Dialog from "@corvu/dialog";
33
import { MultiProvider } from "@solid-primitives/context";
44
import { useNavigate, useParams } from "@solidjs/router";
5-
import { type Accessor, Match, Switch, createMemo, createResource, useContext } from "solid-js";
5+
import {
6+
type Accessor,
7+
Match,
8+
Switch,
9+
createMemo,
10+
createResource,
11+
createSignal,
12+
useContext,
13+
} from "solid-js";
614
import invariant from "tiny-invariant";
715

816
import type { JsonValue, Permissions } from "catcolab-api";
@@ -36,7 +44,7 @@ import { type TheoryLibrary, TheoryLibraryContext } from "../stdlib";
3644
import type { Theory } from "../theory";
3745
import { PermissionsButton } from "../user";
3846
import { type IndexedMap, indexMap } from "../util/indexing";
39-
import TheorySelector from "./theory_selector";
47+
import { TheorySelector } from "./theory_selector";
4048
import { type ModelDocument, newAnalysisDocument } from "./types";
4149

4250
import "./model_document_editor.css";
@@ -215,6 +223,7 @@ export function ModelPane(props: {
215223
}) {
216224
const theories = useContext(TheoryLibraryContext);
217225
invariant(theories, "Library of theories should be provided as context");
226+
const [theorySelectorOpen, setTheorySelectorOpen] = createSignal(false);
218227

219228
const liveDoc = () => props.liveDoc;
220229
const doc = () => props.liveDoc.doc;
@@ -233,24 +242,22 @@ export function ModelPane(props: {
233242
placeholder="Untitled"
234243
/>
235244
</div>
236-
<Dialog
237-
initialOpen={false}
238-
modal={true}
239-
closeOnEscapeKeyDown={true}
240-
trapFocus={true}
241-
>
242-
<div>
243-
<Dialog.Trigger class="theory-selector-button">
244-
{liveDoc().theory()?.name || "Theory"}
245-
</Dialog.Trigger>
246-
</div>
245+
<Dialog open={theorySelectorOpen()} onOpenChange={setTheorySelectorOpen}>
246+
<Dialog.Trigger class="theory-selector-button">
247+
{liveDoc().theory()?.name || "Theory"}
248+
</Dialog.Trigger>
247249
<Dialog.Portal>
248250
<Dialog.Overlay class="overlay" />
249251
<Dialog.Content class="popup">
250252
<TheorySelector
251-
docHandle={props.liveDoc.docHandle}
253+
theory={props.liveDoc.theory()?.id}
254+
setTheory={(id) => {
255+
changeDoc((model) => {
256+
model.theory = id;
257+
});
258+
setTheorySelectorOpen(false);
259+
}}
252260
theories={theories}
253-
doc={doc()}
254261
/>
255262
</Dialog.Content>
256263
</Dialog.Portal>
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
import type { DocHandle } from "@automerge/automerge-repo";
21
import { For, createMemo } from "solid-js";
32

43
import type { TheoryLibrary, TheoryMeta } from "../stdlib";
5-
import type { ModelDocument } from "./types";
4+
import type { TheoryId } from "../theory";
65

76
import "./theory_selector.css";
87

9-
interface TheorySelectorProps {
10-
docHandle: DocHandle<ModelDocument>;
8+
type TheorySelectorProps = {
9+
theory: TheoryId | undefined;
10+
setTheory: (theory: TheoryId | undefined) => void;
1111
theories: TheoryLibrary;
12-
doc: ModelDocument;
13-
}
12+
};
1413

15-
const TheorySelector = (props: TheorySelectorProps) => {
14+
export const TheorySelector = (props: TheorySelectorProps) => {
1615
const groupedTheories = createMemo(() => {
17-
const theories = Array.from(props.theories.metadata()).filter(
18-
(meta) => meta.divisionCategory,
19-
);
2016
const grouped = new Map<string, TheoryMeta[]>();
2117

22-
for (const theory of theories) {
18+
for (const theory of props.theories.metadata()) {
2319
const category = theory.divisionCategory ?? "Other";
2420
const group = grouped.get(category) || [];
2521
group.push(theory);
@@ -37,23 +33,22 @@ const TheorySelector = (props: TheorySelectorProps) => {
3733
<h4 class="division">{category}</h4>
3834
<For each={theories}>
3935
{(meta) => (
40-
<label>
36+
<div class="theory">
4137
<input
4238
type="radio"
4339
name="theory"
40+
id={meta.id}
4441
value={meta.id}
4542
onchange={(evt) => {
46-
const id = (evt.target as HTMLInputElement).value;
47-
props.docHandle.change((model) => {
48-
model.theory = id ? id : undefined;
49-
});
43+
const id = evt.target.value as TheoryId;
44+
props.setTheory(id ? id : undefined);
5045
}}
5146
/>
52-
<div class="theory">
53-
{meta.name}
47+
<label for={meta.id}>
48+
<div class="name">{meta.name}</div>
5449
<div class="description">{meta.description}</div>
55-
</div>
56-
</label>
50+
</label>
51+
</div>
5752
)}
5853
</For>
5954
</>
@@ -62,4 +57,3 @@ const TheorySelector = (props: TheorySelectorProps) => {
6257
</div>
6358
);
6459
};
65-
export default TheorySelector;

0 commit comments

Comments
 (0)