({ open = false, onClose })
| 27 | }; |
| 28 | |
| 29 | export const LoadModal: FC<LoadModalProps> = ({ open = false, onClose }) => { |
| 30 | const [value, setValue] = useState<string>(); |
| 31 | const [selected, setSelected] = useState(""); |
| 32 | |
| 33 | const instance = useReactFlow(); |
| 34 | |
| 35 | const handleLoad = () => { |
| 36 | let graph; |
| 37 | if (value !== undefined) { |
| 38 | graph = JSON.parse(value) as GraphJSON; |
| 39 | } else if (selected !== "") { |
| 40 | graph = examples[selected]; |
| 41 | } |
| 42 | |
| 43 | if (graph === undefined) return; |
| 44 | |
| 45 | const [nodes, edges] = behaveToFlow(graph); |
| 46 | |
| 47 | if (hasPositionMetaData(graph) === false) { |
| 48 | autoLayout(nodes, edges); |
| 49 | } |
| 50 | |
| 51 | instance.setNodes(nodes); |
| 52 | instance.setEdges(edges); |
| 53 | |
| 54 | // TODO better way to call fit vew after edges render |
| 55 | setTimeout(() => { |
| 56 | instance.fitView(); |
| 57 | }, 100); |
| 58 | |
| 59 | handleClose(); |
| 60 | }; |
| 61 | |
| 62 | const handleClose = () => { |
| 63 | setValue(undefined); |
| 64 | setSelected(""); |
| 65 | onClose(); |
| 66 | }; |
| 67 | |
| 68 | return ( |
| 69 | <Modal |
| 70 | title="Load Graph" |
| 71 | actions={[ |
| 72 | { label: "Cancel", onClick: handleClose }, |
| 73 | { label: "Load", onClick: handleLoad }, |
| 74 | ]} |
| 75 | open={open} |
| 76 | onClose={onClose} |
| 77 | > |
| 78 | <textarea |
| 79 | autoFocus |
| 80 | className="border border-gray-300 w-full p-2 h-32 align-top" |
| 81 | placeholder="Paste JSON here" |
| 82 | value={value} |
| 83 | onChange={(e) => setValue(e.currentTarget.value)} |
| 84 | ></textarea> |
| 85 | <div className="p-4 text-center text-gray-800">or</div> |
| 86 | <select |
nothing calls this directly
no outgoing calls
no test coverage detected