question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Restore doesn't work well

See original GitHub issue

Describe the Bug

Hi,

I Save my flow before to update the nodes/edges (remove/add some edges and nodes) and on pane Click I restore the original flow.

after the restore the positions of nodes and edges is not well restored. when the page is resized, the edges and nodes don’t seems to fit the view. The smooth edge looks curved a little bit to connect to the fixed node’s position.

Your Example Website or App

here below the code note that I’m using dagree for the layouting part

const nodeTypes: NodeTypes = {
  customNode: CustomNode,
};

const edgeTypes: EdgeTypes = {
  customEdge: CustomEdge
};

const flowKey = 'example-flow';

const SaveRestoreComponent = (props) => {
  const [reactflowInstance, setReactflowInstance] = useState<ReactFlowInstance>(null);
  const [nodes, setNodes, onNodesChange] = useNodesState<>([]);
  const [edges, setEdges, onEdgesChange] = useEdgesState<>([]);
  const { setViewport } = useReactFlow();

  const handleResize = useCallback(() => {
    reactflowInstance && reactflowInstance.fitView();
  }, [reactflowInstance]);

  const onSave = useCallback(() => {
    if (reactflowInstance) {
      const flow = reactflowInstance.toObject();
      localStorage.setItem(flowKey, JSON.stringify(flow));
    }
  }, [reactflowInstance]);

  const onInit = useCallback(
    (flowInstance: ReactFlowInstance) => {

      setNodes([...props.nodes]);
      setEdges([...props.edges]);

      if (!reactflowInstance) {
        setReactflowInstance(flowInstance);
      }
    },
    [props.nodes, props.edges, setNodes, setEdges, reactflowInstance]
  );

  const onRestore = useCallback(() => {
    const restoreFlow = async () => {
 
      const flow = JSON.parse(localStorage.getItem(flowKey));

      if (flow) {
        const { x = 0, y = 0, zoom = 1 } = flow.viewport;
        setNodes(flow.nodes || []);
        setEdges(flow.edges || []);
        setViewport({ x, y, zoom });

        updateElements(flow.nodes, flow.edges);
      }
    };

    restoreFlow();
  }, [setEdges, setNodes, setViewport]);

  const onNodeClick = useCallback((event: React.MouseEvent, node) => {

     onSave();
     const {nodes, edges} = updateFlow(node);
     setNodes([...nodes]);
     setEdges([...edges]);
    
    },[onSave, setEdges, setNodes]
  );

  const onPaneClick = useCallback(
    (event: React.MouseEvent) => {
        onRestore();
    },
    [onRestore]
  );

  useEffect(() => {
    window.addEventListener('resize', handleResize);
    reactflowInstance && reactflowInstance.fitView();
    return () => window.removeEventListener('resize', handleResize);
  }, [handleResize, reactflowInstance]);

  return (
    <ReactFlow
      nodes={nodes}
      edges={edges}
      nodeTypes={nodeTypes}
      edgeTypes={edgeTypes}
      onNodeClick={onNodeClick}
      onPaneClick={onPaneClick}
      onInit={onInit}
      onNodesChange={onNodesChange}
      onEdgesChange={onEdgesChange}
      minZoom={1}
      fitView
    ></ReactFlow>
  );
});

export const Flow = (props) => {
  return (
    <ReactFlowProvider>
      <SaveRestoreComponent {...props} />
    </ReactFlowProvider>
  );
};

Steps to Reproduce the Bug or Issue

  1. Save the flow
  2. update nodes/edges ( by adding and removing some nodes and edges)
  3. restore on pane click
  4. edges and pane position doesnt fit the view while resizing.

Expected behavior

I expected the restore to add back the element at the right position and fit the view while resizing my window.

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [Chrome, Safari, Firefox]
  • Version: [10.0.6]

Additional context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Jasminoucommented, Jul 15, 2022

@moklick yes it’s fixed thank you

1reaction
Jasminoucommented, Jul 14, 2022

thank you I will check later and close the bug 😃 @moklick

Read more comments on GitHub >

github_iconTop Results From Across the Web

System Restore Not Working on Windows? 5 Tips and Fixes to ...
First, try another System Restore point. Something may have corrupted the default restore point during the store process and, as such, won't boot....
Read more >
Windows System Restore Not Working? We Have the Solution
Most modern Windows applications work fine with Windows System Restore and will not interfere with the creation or recovery of restore points.
Read more >
System Restore Not Working in Windows 10 | 9 Fixes Are Here!
Fix 9: Check if services running properly. System Restore relies on specific services to run properly, thus if these services is not runing, ......
Read more >
12 Ways to Fix Restore Point not Working in Windows 11
If Restore point is not working in Windows 10, check if System Restore is enabled or use the solutions mentioned in this guide....
Read more >
Easily Fix: Windows 10 System Restore Stuck or Hang Up
If your system is stuck on the initializing screen or file restoring, the first thing to do is still to wait for a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found