Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix react flow environment renders #146

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import type { Edge, Node } from "reactflow";
import dagre from "dagre";

const dagreGraph = new dagre.graphlib.Graph().setDefaultEdgeLabel(() => ({}));

const generateLevels = (nodes: Node[], edges: Edge[]) => {
const levels: Record<string, number> = {};
const edgeMap: Record<string, string[]> = {};
Expand Down Expand Up @@ -39,6 +37,8 @@ export const getLayoutedElementsDagre = (
direction = "TB",
extraEdgeLength = 0,
) => {
const dagreGraph = new dagre.graphlib.Graph();
dagreGraph.setDefaultEdgeLabel(() => ({}));
dagreGraph.setGraph({ rankdir: direction });

nodes.forEach((node) => dagreGraph.setNode(node.id, node));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,12 @@ const useOnConnect = () => {
return onConnect;
};

const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
export const EnvFlowBuilder: React.FC<{
systemId: string;
envs: Array<Environment>;
policies: Array<EnvironmentPolicy>;
policyDeployments: Array<EnvironmentPolicyDeployment>;
}> = ({ systemId, envs, policies, policyDeployments }) => {
const { fitView } = useReactFlow();
const [nodes, setNodes, onNodesChange] = useNodesState([
triggerNode,
...envs.map((env) => ({
Expand Down Expand Up @@ -223,14 +221,11 @@ export const EnvFlowBuilder: React.FC<{

setNodes([...layouted.nodes]);
setEdges([...layouted.edges]);
}, [nodes, edges, setNodes, setEdges]);

window.requestAnimationFrame(() => {
// hack to get it to center - we should figure out when the layout is done
// and then call fitView. We are betting that everything should be
// rendered correctly in 100ms before fitting the view.
sleep(100).then(() => fitView({ padding: 0.12, maxZoom: 1 }));
});
}, [nodes, edges, setNodes, setEdges, fitView]);
useEffect(() => {
if (reactFlowInstance && nodes.length) reactFlowInstance.fitView();
}, [reactFlowInstance, nodes]);

useEffect(() => {
if (reactFlowInstance != null) onLayout();
Expand Down
Loading