(props)
| 405 | } |
| 406 | |
| 407 | export function DecoderGraph(props) { |
| 408 | const graphElementRef = React.useRef(); |
| 409 | |
| 410 | const derivedNodeFeatures = props.derivedNodeFeatures || (() => {}) |
| 411 | |
| 412 | const [cyData, setCyData] = useState(null); |
| 413 | const [rawGraphData, setRawGraphData] = useState(null); |
| 414 | const cyRef = useRef(null) |
| 415 | |
| 416 | props.fitTrigger && props.fitTrigger.addTriggerListener(() => cyRef.current.fit()) |
| 417 | |
| 418 | const reset = () => { |
| 419 | if (cyRef.current) { |
| 420 | cyRef.current.nodes().remove() |
| 421 | cyRef.current.edges().remove() |
| 422 | } else { |
| 423 | console.log("no cy to reset", cyRef.current, "is cy") |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // listen for changes to persisted graph |
| 428 | const onPersistedGraphChange = React.useCallback((s) => { |
| 429 | if (rawGraphData == s) { |
| 430 | return |
| 431 | } |
| 432 | reset() |
| 433 | setCyData(JSON.parse(s)) |
| 434 | setTimeout(() => cyRef.current.fit(), 0) |
| 435 | }, []) |
| 436 | |
| 437 | React.useEffect(() => { |
| 438 | // restore previous graph data |
| 439 | let graphData = persistedState.getItem("decoder-graph") || null |
| 440 | if (graphData) { |
| 441 | let graph = JSON.parse(graphData); |
| 442 | setCyData(graph) |
| 443 | } |
| 444 | |
| 445 | persistedState.on("decoder-graph", onPersistedGraphChange) |
| 446 | |
| 447 | return () => { |
| 448 | // remove listener |
| 449 | persistedState.remove("decoder-graph", onPersistedGraphChange) |
| 450 | }; |
| 451 | }, []); |
| 452 | |
| 453 | React.useEffect(() => { |
| 454 | const cy = initDecoderGraphCy(graphElementRef.current) |
| 455 | cyRef.current = cy |
| 456 | |
| 457 | // when clicking a node, highlight all paths to root |
| 458 | function onSelectNode(evt) { |
| 459 | let node = evt.target |
| 460 | let nodes = [node] |
| 461 | |
| 462 | // for compound nodes, select all children |
| 463 | if (node.isParent()) { |
| 464 | nodes = node.descendants() |
nothing calls this directly
no test coverage detected