|
1 | 1 | import { |
| 2 | + Box, |
2 | 3 | Button, |
3 | 4 | Dialog, |
4 | 5 | DialogActions, |
5 | 6 | DialogContent, |
6 | 7 | DialogTitle, |
| 8 | + FormLabel, |
| 9 | + Stack, |
7 | 10 | TextField, |
8 | 11 | } from "@mui/material"; |
| 12 | +import nullthrows from "nullthrows"; |
9 | 13 | import { useState } from "react"; |
| 14 | +import { z } from "zod"; |
| 15 | +import type CCStore from "../store"; |
| 16 | +import type { CCComponentId } from "../store/component"; |
| 17 | +import type { CCComponentPinId } from "../store/componentPin"; |
| 18 | +import { useStore } from "../store/react"; |
10 | 19 |
|
11 | 20 | export type ComponentPropertyDialogProps = { |
| 21 | + componentId: CCComponentId; |
12 | 22 | defaultName: string; |
13 | | - onAccept(newName: string): void; |
| 23 | + onClose(): void; |
14 | 24 | onCancel(): void; |
15 | 25 | }; |
16 | 26 |
|
| 27 | +const stateSchema = z.object({ |
| 28 | + name: z.string().nonempty(), |
| 29 | + pinNameById: z.map(z.custom<CCComponentPinId>(), z.string().nonempty()), |
| 30 | +}); |
| 31 | +function extractStateFromStore( |
| 32 | + store: CCStore, |
| 33 | + componentId: CCComponentId, |
| 34 | +): z.input<typeof stateSchema> { |
| 35 | + const component = nullthrows(store.components.get(componentId)); |
| 36 | + return { |
| 37 | + name: component.name, |
| 38 | + pinNameById: new Map( |
| 39 | + store.componentPins |
| 40 | + .getManyByComponentId(componentId) |
| 41 | + .map((pin) => [pin.id, pin.name]), |
| 42 | + ), |
| 43 | + }; |
| 44 | +} |
| 45 | +function applyStateToStore( |
| 46 | + store: CCStore, |
| 47 | + componentId: CCComponentId, |
| 48 | + state: z.output<typeof stateSchema>, |
| 49 | +) { |
| 50 | + store.components.update(componentId, { name: state.name }); |
| 51 | + for (const [pinId, pinName] of state.pinNameById) { |
| 52 | + store.componentPins.update(pinId, { name: pinName }); |
| 53 | + } |
| 54 | +} |
| 55 | + |
17 | 56 | export function ComponentPropertyDialog({ |
18 | | - defaultName, |
19 | | - onAccept, |
20 | | - onCancel, |
| 57 | + componentId, |
| 58 | + onClose, |
21 | 59 | }: ComponentPropertyDialogProps) { |
22 | | - const [newName, setNewName] = useState(defaultName); |
| 60 | + const { store } = useStore(); |
| 61 | + const component = nullthrows(store.components.get(componentId)); |
| 62 | + const [state, setState] = useState(() => |
| 63 | + extractStateFromStore(store, componentId), |
| 64 | + ); |
| 65 | + const result = stateSchema.safeParse(state); |
23 | 66 |
|
24 | 67 | return ( |
25 | | - <Dialog maxWidth="xs" fullWidth open onClose={onCancel}> |
| 68 | + <Dialog maxWidth="sm" fullWidth open onClose={onClose}> |
26 | 69 | <form |
27 | 70 | onSubmit={(e) => { |
28 | 71 | e.preventDefault(); |
29 | | - if (!newName) return; |
30 | | - onAccept(newName); |
| 72 | + if (!result.success) return; |
| 73 | + applyStateToStore(store, componentId, result.data); |
| 74 | + onClose(); |
31 | 75 | }} |
32 | 76 | > |
33 | | - <DialogTitle>Component property</DialogTitle> |
| 77 | + <DialogTitle>{component.name} Properties</DialogTitle> |
34 | 78 | <DialogContent> |
| 79 | + <FormLabel component="div">Name</FormLabel> |
35 | 80 | <TextField |
36 | | - label="Name" |
37 | | - value={newName} |
| 81 | + size="small" |
| 82 | + value={state.name} |
38 | 83 | fullWidth |
39 | | - onChange={(e) => setNewName(e.target.value)} |
| 84 | + onChange={(e) => setState({ ...state, name: e.target.value })} |
40 | 85 | placeholder="Name" |
| 86 | + sx={{ mt: 0.5 }} |
41 | 87 | /> |
| 88 | + <Box |
| 89 | + sx={{ |
| 90 | + display: "grid", |
| 91 | + gridTemplateColumns: "1fr 1fr", |
| 92 | + gap: 2, |
| 93 | + mt: 2, |
| 94 | + }} |
| 95 | + > |
| 96 | + <Stack sx={{ gap: 0.5 }}> |
| 97 | + <FormLabel component="div">Input Pins</FormLabel> |
| 98 | + {store.componentPins |
| 99 | + .getManyByComponentId(componentId) |
| 100 | + .filter((pin) => pin.type === "input") |
| 101 | + .map((pin) => ( |
| 102 | + <TextField |
| 103 | + key={pin.id} |
| 104 | + size="small" |
| 105 | + value={state.pinNameById.get(pin.id)} |
| 106 | + fullWidth |
| 107 | + onChange={(e) => |
| 108 | + setState({ |
| 109 | + ...state, |
| 110 | + pinNameById: new Map(state.pinNameById).set( |
| 111 | + pin.id, |
| 112 | + e.target.value, |
| 113 | + ), |
| 114 | + }) |
| 115 | + } |
| 116 | + /> |
| 117 | + ))} |
| 118 | + </Stack> |
| 119 | + <Stack sx={{ gap: 0.5 }}> |
| 120 | + <FormLabel component="div">Output Pins</FormLabel> |
| 121 | + {store.componentPins |
| 122 | + .getManyByComponentId(componentId) |
| 123 | + .filter((pin) => pin.type === "output") |
| 124 | + .map((pin) => ( |
| 125 | + <TextField |
| 126 | + key={pin.id} |
| 127 | + size="small" |
| 128 | + value={state.pinNameById.get(pin.id)} |
| 129 | + fullWidth |
| 130 | + onChange={(e) => |
| 131 | + setState({ |
| 132 | + ...state, |
| 133 | + pinNameById: new Map(state.pinNameById).set( |
| 134 | + pin.id, |
| 135 | + e.target.value, |
| 136 | + ), |
| 137 | + }) |
| 138 | + } |
| 139 | + /> |
| 140 | + ))} |
| 141 | + </Stack> |
| 142 | + </Box> |
42 | 143 | </DialogContent> |
43 | 144 | <DialogActions> |
44 | | - <Button onClick={onCancel} color="inherit"> |
| 145 | + <Button onClick={onClose} color="inherit"> |
45 | 146 | Cancel |
46 | 147 | </Button> |
47 | 148 | <Button |
48 | 149 | variant="outlined" |
49 | | - color="inherit" |
| 150 | + color="primary" |
50 | 151 | type="submit" |
51 | | - disabled={!newName} |
| 152 | + disabled={!result.success} |
52 | 153 | > |
53 | | - Create |
| 154 | + Apply |
54 | 155 | </Button> |
55 | 156 | </DialogActions> |
56 | 157 | </form> |
|
0 commit comments