-
Notifications
You must be signed in to change notification settings - Fork 31
Prevent query graph flickering during relayout #170
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
Open
vogelsgesang
wants to merge
1
commit into
main
Choose a base branch
from
fix-flicker-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,22 +1,27 @@ | ||||||||||||
| import type {NodeChange} from "@xyflow/react"; | ||||||||||||
| import {ReactFlow, MiniMap, Controls, ReactFlowProvider} from "@xyflow/react"; | ||||||||||||
| import "@xyflow/react/dist/base.css"; | ||||||||||||
|
|
||||||||||||
| import {layoutTree} from "./tree-layout"; | ||||||||||||
| import type {TreeDescription, TreeNode} from "../tree-description"; | ||||||||||||
| import {allChildren, visitTreeNodes} from "../tree-description"; | ||||||||||||
| import type {ReactNode} from "react"; | ||||||||||||
| import {useMemo, useEffect, useRef} from "react"; | ||||||||||||
| import {useCallback, useMemo} from "react"; | ||||||||||||
| import {QueryNode} from "./QueryNode"; | ||||||||||||
| import type {QueryGraphNode} from "./QueryNode"; | ||||||||||||
| import {ColoredEdge} from "./ColoredEdge"; | ||||||||||||
| import {useGraphRenderingStore} from "./store"; | ||||||||||||
| import {createGraphRenderingStore, GraphRenderingStoreContext, useGraphRenderingStore} from "./store"; | ||||||||||||
| import "./QueryGraph.css"; | ||||||||||||
|
|
||||||||||||
| interface QueryGraphProps { | ||||||||||||
| treeDescription: TreeDescription; | ||||||||||||
| children: ReactNode | ReactNode[]; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| interface QueryGraphInternalProps extends QueryGraphProps { | ||||||||||||
| nodeIdMapping: Map<TreeNode, string>; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| function minimapNodeColor(n: QueryGraphNode): string { | ||||||||||||
| if (n.data.nodeColor) return n.data.nodeColor; | ||||||||||||
| if (n.data.iconColor) return n.data.iconColor; | ||||||||||||
|
|
@@ -31,59 +36,27 @@ const edgeTypes = { | |||||||||||
| colored: ColoredEdge, | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| function QueryGraphInternal({treeDescription, children}: QueryGraphProps) { | ||||||||||||
| // Assign ids to all nodes | ||||||||||||
| const nodeIdMapping = useMemo(() => { | ||||||||||||
| let nextId = 0; | ||||||||||||
| const nodeIds = new Map<TreeNode, string>(); | ||||||||||||
| visitTreeNodes( | ||||||||||||
| treeDescription.root, | ||||||||||||
| (d) => { | ||||||||||||
| nodeIds.set(d, "" + nextId++); | ||||||||||||
| }, | ||||||||||||
| allChildren, | ||||||||||||
| ); | ||||||||||||
| return nodeIds; | ||||||||||||
| }, [treeDescription]); | ||||||||||||
|
|
||||||||||||
| // Initialize our state using the correct "expandedByDefault" state | ||||||||||||
| const initGraphStore = useGraphRenderingStore((s) => s.init); | ||||||||||||
| useMemo(() => { | ||||||||||||
| const expandedSubtrees = {}; | ||||||||||||
| visitTreeNodes( | ||||||||||||
| treeDescription.root, | ||||||||||||
| (n) => { | ||||||||||||
| if (n.expandedByDefault) { | ||||||||||||
| expandedSubtrees[nodeIdMapping.get(n)!] = true; | ||||||||||||
| } | ||||||||||||
| }, | ||||||||||||
| allChildren, | ||||||||||||
| ); | ||||||||||||
| initGraphStore(expandedSubtrees); | ||||||||||||
| }, [treeDescription, initGraphStore, nodeIdMapping]); | ||||||||||||
|
|
||||||||||||
| // Create a ResizeObserver to keep track of the sizes of the nodes | ||||||||||||
| const resizeObserverRef = useRef<ResizeObserver | undefined>(undefined); | ||||||||||||
| function QueryGraphInternal({treeDescription, children, nodeIdMapping}: QueryGraphInternalProps) { | ||||||||||||
| // Keep React Flow's measurements in the controlled node objects. Dropping them when | ||||||||||||
| // recomputing the layout makes React Flow repeatedly hide and re-initialize the nodes. | ||||||||||||
|
Comment on lines
+40
to
+41
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| const nodeDimensions = useGraphRenderingStore((s) => s.nodeDimensions); | ||||||||||||
| const updateNodeDimensions = useGraphRenderingStore((s) => s.updateNodeDimensions); | ||||||||||||
| const resizeObserver = useMemo(() => { | ||||||||||||
| resizeObserverRef.current?.disconnect(); | ||||||||||||
| const observer = new ResizeObserver(updateNodeDimensions); | ||||||||||||
| resizeObserverRef.current = observer; | ||||||||||||
| return observer; | ||||||||||||
| }, [updateNodeDimensions]); | ||||||||||||
| useEffect(() => { | ||||||||||||
| return () => { | ||||||||||||
| resizeObserverRef.current?.disconnect(); | ||||||||||||
| }; | ||||||||||||
| }, []); | ||||||||||||
| const onNodesChange = useCallback( | ||||||||||||
| (changes: NodeChange<QueryGraphNode>[]) => { | ||||||||||||
| const updates = changes.flatMap((change) => { | ||||||||||||
| if (change.type !== "dimensions" || change.dimensions === undefined) return []; | ||||||||||||
| return [[change.id, change.dimensions] as const]; | ||||||||||||
| }); | ||||||||||||
| updateNodeDimensions(updates); | ||||||||||||
| }, | ||||||||||||
| [updateNodeDimensions], | ||||||||||||
| ); | ||||||||||||
|
|
||||||||||||
| // Layout the tree, using the actual measured sizes of the DOM nodes | ||||||||||||
| const nodeDimensions = useGraphRenderingStore((s) => s.nodeDimensions); | ||||||||||||
| const expandedNodes = useGraphRenderingStore((s) => s.expandedNodes); | ||||||||||||
| // Layout the tree using the dimensions measured by React Flow itself. | ||||||||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| const expandedSubtrees = useGraphRenderingStore((s) => s.expandedSubtrees); | ||||||||||||
| const layout = useMemo( | ||||||||||||
| () => layoutTree(treeDescription, nodeIdMapping, nodeDimensions, expandedNodes, expandedSubtrees, resizeObserver), | ||||||||||||
| [treeDescription, nodeIdMapping, nodeDimensions, expandedNodes, expandedSubtrees, resizeObserver], | ||||||||||||
| () => layoutTree(treeDescription, nodeIdMapping, nodeDimensions, expandedSubtrees), | ||||||||||||
| [treeDescription, nodeIdMapping, nodeDimensions, expandedSubtrees], | ||||||||||||
| ); | ||||||||||||
|
|
||||||||||||
| return ( | ||||||||||||
|
|
@@ -93,6 +66,7 @@ function QueryGraphInternal({treeDescription, children}: QueryGraphProps) { | |||||||||||
| nodeOrigin={[0.5, 0]} | ||||||||||||
| nodeTypes={nodeTypes} | ||||||||||||
| edgeTypes={edgeTypes} | ||||||||||||
| onNodesChange={onNodesChange} | ||||||||||||
| fitView | ||||||||||||
| minZoom={0.2} | ||||||||||||
| maxZoom={1.5} | ||||||||||||
|
|
@@ -109,10 +83,33 @@ function QueryGraphInternal({treeDescription, children}: QueryGraphProps) { | |||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| function createGraphState(treeDescription: TreeDescription) { | ||||||||||||
| let nextId = 0; | ||||||||||||
| const nodeIdMapping = new Map<TreeNode, string>(); | ||||||||||||
| const expandedSubtrees: Record<string, boolean> = {}; | ||||||||||||
| visitTreeNodes( | ||||||||||||
| treeDescription.root, | ||||||||||||
| (node) => { | ||||||||||||
| const id = "" + nextId++; | ||||||||||||
| nodeIdMapping.set(node, id); | ||||||||||||
| if (node.expandedByDefault) expandedSubtrees[id] = true; | ||||||||||||
| }, | ||||||||||||
| allChildren, | ||||||||||||
| ); | ||||||||||||
| return { | ||||||||||||
| nodeIdMapping, | ||||||||||||
| graphStore: createGraphRenderingStore(expandedSubtrees), | ||||||||||||
| }; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| export function QueryGraph(props: QueryGraphProps) { | ||||||||||||
| const {nodeIdMapping, graphStore} = useMemo(() => createGraphState(props.treeDescription), [props.treeDescription]); | ||||||||||||
|
|
||||||||||||
| return ( | ||||||||||||
| <ReactFlowProvider> | ||||||||||||
| <QueryGraphInternal {...props} /> | ||||||||||||
| <GraphRenderingStoreContext.Provider value={graphStore}> | ||||||||||||
| <QueryGraphInternal {...props} nodeIdMapping={nodeIdMapping} /> | ||||||||||||
| </GraphRenderingStoreContext.Provider> | ||||||||||||
| </ReactFlowProvider> | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.