Skip to content

Commit 56cfc8d

Browse files
committed
refactor(easy-peasy): return single values from useStoreState
1 parent 17614bd commit 56cfc8d

File tree

10 files changed

+93
-130
lines changed

10 files changed

+93
-130
lines changed

src/additional-components/Background/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ const defaultColors = {
2525

2626
const Background = memo(
2727
({ variant = BackgroundVariant.Dots, gap = 24, size = 0.5, color, style = {}, className = '' }: BackgroundProps) => {
28-
const {
29-
width,
30-
height,
31-
transform: [x, y, scale],
32-
} = useStoreState((s) => s);
28+
const width = useStoreState((s) => s.width);
29+
const height = useStoreState((s) => s.height);
30+
const [x, y, scale] = useStoreState((s) => s.transform);
3331

3432
const bgClasses = classnames('react-flow__background', className);
3533
const bgColor = color ? color : defaultColors[variant];

src/additional-components/Controls/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface ControlProps extends React.HTMLAttributes<HTMLDivElement> {
2525

2626
const Controls = ({ style, showZoom = true, showFitView = true, showInteractive = true, className }: ControlProps) => {
2727
const setInteractive = useStoreActions((actions) => actions.setInteractive);
28-
const { isInteractive } = useStoreState(({ isInteractive }) => ({ isInteractive }));
28+
const isInteractive = useStoreState((s) => s.isInteractive);
2929
const mapClasses: string = classnames('react-flow__controls', className);
3030

3131
return (

src/additional-components/MiniMap/index.tsx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,23 @@ const MiniMap = ({
3030
nodeBorderRadius = 5,
3131
maskColor = 'rgba(10, 10, 10, .25)',
3232
}: MiniMapProps) => {
33-
const state = useStoreState(({ width, height, nodes, transform: [tX, tY, tScale] }) => ({
34-
width,
35-
height,
36-
nodes,
37-
tX,
38-
tY,
39-
tScale,
40-
}));
33+
const containerWidth = useStoreState((s) => s.width);
34+
const containerHeight = useStoreState((s) => s.height);
35+
const [tX, tY, tScale] = useStoreState((s) => s.transform);
36+
const nodes = useStoreState((s) => s.nodes);
4137

4238
const mapClasses = classnames('react-flow__minimap', className);
4339
const elementWidth = (style.width || baseStyle.width)! as number;
4440
const elementHeight = (style.height || baseStyle.height)! as number;
4541
const nodeColorFunc = (nodeColor instanceof Function ? nodeColor : () => nodeColor) as StringFunc;
46-
const hasNodes = state.nodes && state.nodes.length;
42+
const hasNodes = nodes && nodes.length;
4743

48-
const bb = getRectOfNodes(state.nodes);
44+
const bb = getRectOfNodes(nodes);
4945
const viewBB: Rect = {
50-
x: -state.tX / state.tScale,
51-
y: -state.tY / state.tScale,
52-
width: state.width / state.tScale,
53-
height: state.height / state.tScale,
46+
x: -tX / tScale,
47+
y: -tY / tScale,
48+
width: containerWidth / tScale,
49+
height: containerHeight / tScale,
5450
};
5551

5652
const boundingRect = hasNodes ? getBoundsofRects(bb, viewBB) : viewBB;
@@ -79,7 +75,7 @@ const MiniMap = ({
7975
}}
8076
className={mapClasses}
8177
>
82-
{state.nodes.map((node) => (
78+
{nodes.map((node) => (
8379
<MiniMapNode key={node.id} node={node} color={nodeColorFunc(node)} borderRadius={nodeBorderRadius} />
8480
))}
8581
<path

src/components/NodesSelection/index.tsx

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,28 @@ function getStartPositions(nodes: Node[]): StartPositions {
3030
export default memo(() => {
3131
const [offset, setOffset] = useState<XYPosition>({ x: 0, y: 0 });
3232
const [startPositions, setStartPositions] = useState<StartPositions>({});
33-
const state = useStoreState((s) => ({
34-
transform: s.transform,
35-
selectedNodesBbox: s.selectedNodesBbox,
36-
selectedElements: s.selectedElements,
37-
snapToGrid: s.snapToGrid,
38-
snapGrid: s.snapGrid,
39-
nodes: s.nodes,
40-
}));
33+
const [tX, tY, tScale] = useStoreState((s) => s.transform);
34+
const selectedNodesBbox = useStoreState((s) => s.selectedNodesBbox);
35+
const selectedElements = useStoreState((s) => s.selectedElements);
36+
const snapToGrid = useStoreState((s) => s.snapToGrid);
37+
const snapGrid = useStoreState((s) => s.snapGrid);
38+
const nodes = useStoreState((s) => s.nodes);
39+
4140
const updateNodePos = useStoreActions((a) => a.updateNodePos);
42-
const [tx, ty, tScale] = state.transform;
43-
const position = state.selectedNodesBbox;
44-
const grid = (state.snapToGrid ? state.snapGrid : [1, 1])! as [number, number];
41+
const position = selectedNodesBbox;
42+
const grid = (snapToGrid ? snapGrid : [1, 1])! as [number, number];
4543

4644
const onStart = (evt: MouseEvent) => {
4745
const scaledClient: XYPosition = {
4846
x: evt.clientX / tScale,
4947
y: evt.clientY / tScale,
5048
};
51-
const offsetX: number = scaledClient.x - position.x - tx;
52-
const offsetY: number = scaledClient.y - position.y - ty;
53-
const selectedNodes = state.selectedElements
54-
? (state.selectedElements.filter(isNode) as Node[]).map(
55-
(selectedNode) => state.nodes.find((node) => node.id === selectedNode.id)! as Node
56-
)
49+
const offsetX: number = scaledClient.x - position.x - tX;
50+
const offsetY: number = scaledClient.y - position.y - tY;
51+
const selectedNodes = selectedElements
52+
? selectedElements
53+
.filter(isNode)
54+
.map((selectedNode) => nodes.find((node) => node.id === selectedNode.id)! as Node)
5755
: [];
5856

5957
const nextStartPositions = getStartPositions(selectedNodes);
@@ -70,11 +68,11 @@ export default memo(() => {
7068
y: evt.clientY / tScale,
7169
};
7270

73-
if (state.selectedElements) {
74-
(state.selectedElements.filter(isNode) as Node[]).forEach((node) => {
71+
if (selectedElements) {
72+
selectedElements.filter(isNode).forEach((node) => {
7573
const pos: XYPosition = {
76-
x: startPositions[node.id].x + scaledClient.x - position.x - offset.x - tx,
77-
y: startPositions[node.id].y + scaledClient.y - position.y - offset.y - ty,
74+
x: startPositions[node.id].x + scaledClient.x - position.x - offset.x - tX,
75+
y: startPositions[node.id].y + scaledClient.y - position.y - offset.y - tY,
7876
};
7977

8078
updateNodePos({ id: node.id, pos });
@@ -86,7 +84,7 @@ export default memo(() => {
8684
<div
8785
className="react-flow__nodesselection"
8886
style={{
89-
transform: `translate(${tx}px,${ty}px) scale(${tScale})`,
87+
transform: `translate(${tX}px,${tY}px) scale(${tScale})`,
9088
}}
9189
>
9290
<ReactDraggable
@@ -98,10 +96,10 @@ export default memo(() => {
9896
<div
9997
className="react-flow__nodesselection-rect"
10098
style={{
101-
width: state.selectedNodesBbox.width,
102-
height: state.selectedNodesBbox.height,
103-
top: state.selectedNodesBbox.y,
104-
left: state.selectedNodesBbox.x,
99+
width: selectedNodesBbox.width,
100+
height: selectedNodesBbox.height,
101+
top: selectedNodesBbox.y,
102+
left: selectedNodesBbox.x,
105103
}}
106104
/>
107105
</ReactDraggable>

src/container/EdgeRenderer/index.tsx

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -187,32 +187,21 @@ function renderEdge(
187187
}
188188

189189
const EdgeRenderer = memo((props: EdgeRendererProps) => {
190-
const {
191-
transform,
192-
edges,
193-
nodes,
194-
connectionSourceId,
195-
connectionPosition: { x, y },
196-
selectedElements,
197-
isInteractive,
198-
} = useStoreState((s) => ({
199-
transform: s.transform,
200-
edges: s.edges,
201-
nodes: s.nodes,
202-
connectionSourceId: s.connectionSourceId,
203-
connectionPosition: s.connectionPosition,
204-
selectedElements: s.selectedElements,
205-
isInteractive: s.isInteractive,
206-
}));
190+
const [tX, tY, tScale] = useStoreState((s) => s.transform);
191+
const edges = useStoreState((s) => s.edges);
192+
const nodes = useStoreState((s) => s.nodes);
193+
const connectionSourceId = useStoreState((s) => s.connectionSourceId);
194+
const connectionPosition = useStoreState((s) => s.connectionPosition);
195+
const selectedElements = useStoreState((s) => s.selectedElements);
196+
const isInteractive = useStoreState((s) => s.isInteractive);
207197

208198
const { width, height, connectionLineStyle, connectionLineType } = props;
209199

210200
if (!width) {
211201
return null;
212202
}
213203

214-
const [tx, ty, tScale] = transform;
215-
const transformStyle = `translate(${tx},${ty}) scale(${tScale})`;
204+
const transformStyle = `translate(${tX},${tY}) scale(${tScale})`;
216205

217206
return (
218207
<svg width={width} height={height} className="react-flow__edges">
@@ -222,9 +211,9 @@ const EdgeRenderer = memo((props: EdgeRendererProps) => {
222211
<ConnectionLine
223212
nodes={nodes}
224213
connectionSourceId={connectionSourceId}
225-
connectionPositionX={x}
226-
connectionPositionY={y}
227-
transform={transform}
214+
connectionPositionX={connectionPosition.x}
215+
connectionPositionY={connectionPosition.y}
216+
transform={[tX, tY, tScale]}
228217
connectionLineStyle={connectionLineStyle}
229218
connectionLineType={connectionLineType}
230219
isInteractive={isInteractive}

src/container/GraphView/index.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,11 @@ const GraphView = memo(
5858
}: GraphViewProps) => {
5959
const zoomPane = useRef<HTMLDivElement>(null);
6060
const rendererNode = useRef<HTMLDivElement>(null);
61-
const state = useStoreState((s) => ({
62-
width: s.width,
63-
height: s.height,
64-
nodes: s.nodes,
65-
edges: s.edges,
66-
d3Initialised: s.d3Initialised,
67-
nodesSelectionActive: s.nodesSelectionActive,
68-
}));
61+
const width = useStoreState((s) => s.width);
62+
const height = useStoreState((s) => s.height);
63+
const d3Initialised = useStoreState((s) => s.d3Initialised);
64+
const nodesSelectionActive = useStoreState((s) => s.nodesSelectionActive);
65+
6966
const updateSize = useStoreActions((actions) => actions.updateSize);
7067
const setNodesSelection = useStoreActions((actions) => actions.setNodesSelection);
7168
const setOnConnect = useStoreActions((a) => a.setOnConnect);
@@ -106,15 +103,15 @@ const GraphView = memo(
106103
useD3Zoom({ zoomPane, onMove, selectionKeyPressed });
107104

108105
useEffect(() => {
109-
if (state.d3Initialised && onLoad) {
106+
if (d3Initialised && onLoad) {
110107
onLoad({
111108
fitView,
112109
zoomIn,
113110
zoomOut,
114111
project,
115112
});
116113
}
117-
}, [state.d3Initialised, onLoad]);
114+
}, [d3Initialised, onLoad]);
118115

119116
useEffect(() => {
120117
setSnapGrid({ snapToGrid, snapGrid });
@@ -137,15 +134,15 @@ const GraphView = memo(
137134
onlyRenderVisibleNodes={onlyRenderVisibleNodes}
138135
/>
139136
<EdgeRenderer
140-
width={state.width}
141-
height={state.height}
137+
width={width}
138+
height={height}
142139
edgeTypes={edgeTypes}
143140
onElementClick={onElementClick}
144141
connectionLineType={connectionLineType}
145142
connectionLineStyle={connectionLineStyle}
146143
/>
147144
{selectionKeyPressed && <UserSelection isInteractive={isInteractive} />}
148-
{state.nodesSelectionActive && <NodesSelection />}
145+
{nodesSelectionActive && <NodesSelection />}
149146
<div className="react-flow__zoompane" onClick={onZoomPaneClick} ref={zoomPane} />
150147
</div>
151148
);

src/container/NodeRenderer/index.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,15 @@ function renderNode(
4949
}
5050

5151
const NodeRenderer = memo(({ onlyRenderVisibleNodes = true, ...props }: NodeRendererProps) => {
52-
const { nodes, transform, selectedElements, width, height, isInteractive } = useStoreState((s) => ({
53-
nodes: s.nodes,
54-
transform: s.transform,
55-
selectedElements: s.selectedElements,
56-
width: s.width,
57-
height: s.height,
58-
isInteractive: s.isInteractive,
59-
}));
60-
61-
const [tx, ty, tScale] = transform;
52+
const nodes = useStoreState((s) => s.nodes);
53+
const transform = useStoreState((s) => s.transform);
54+
const selectedElements = useStoreState((s) => s.selectedElements);
55+
const width = useStoreState((s) => s.width);
56+
const height = useStoreState((s) => s.height);
57+
const isInteractive = useStoreState((s) => s.isInteractive);
58+
const [tX, tY, tScale] = transform;
6259
const transformStyle = {
63-
transform: `translate(${tx}px,${ty}px) scale(${tScale})`,
60+
transform: `translate(${tX}px,${tY}px) scale(${tScale})`,
6461
};
6562

6663
const renderNodes = onlyRenderVisibleNodes

src/hooks/useD3Zoom.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, MutableRefObject } from 'react';
2-
import * as d3Zoom from 'd3-zoom';
2+
import { zoom, zoomIdentity } from 'd3-zoom';
33
import { select, event } from 'd3-selection';
44

55
import { useStoreState, useStoreActions } from '../store/hooks';
@@ -10,17 +10,14 @@ interface UseD3ZoomParams {
1010
onMove?: () => void;
1111
}
1212

13-
const d3ZoomInstance = d3Zoom
14-
.zoom()
13+
const d3ZoomInstance = zoom()
1514
.scaleExtent([0.5, 2])
1615
.filter(() => !event.button);
1716

1817
export default ({ zoomPane, onMove, selectionKeyPressed }: UseD3ZoomParams): void => {
19-
const state = useStoreState((s) => ({
20-
transform: s.transform,
21-
d3Selection: s.d3Selection,
22-
d3Zoom: s.d3Zoom,
23-
}));
18+
const transform = useStoreState((s) => s.transform);
19+
const d3Selection = useStoreState((s) => s.d3Selection);
20+
const d3Zoom = useStoreState((s) => s.d3Zoom);
2421

2522
const initD3 = useStoreActions((actions) => actions.initD3);
2623
const updateTransform = useStoreActions((actions) => actions.updateTransform);
@@ -48,13 +45,10 @@ export default ({ zoomPane, onMove, selectionKeyPressed }: UseD3ZoomParams): voi
4845
}
4946
});
5047

51-
if (state.d3Selection && state.d3Zoom) {
48+
if (d3Selection && d3Zoom) {
5249
// we need to restore the graph transform otherwise d3 zoom transform and graph transform are not synced
53-
const graphTransform = d3Zoom.zoomIdentity
54-
.translate(state.transform[0], state.transform[1])
55-
.scale(state.transform[2]);
56-
57-
state.d3Selection.call(state.d3Zoom.transform, graphTransform);
50+
const graphTransform = zoomIdentity.translate(transform[0], transform[1]).scale(transform[2]);
51+
d3Selection.call(d3Zoom.transform, graphTransform);
5852
}
5953
}
6054

src/hooks/useElementUpdater.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@ import { parseElement, isNode, isEdge } from '../utils/graph';
66
import { Elements, Node, Edge } from '../types';
77

88
const useElementUpdater = (elements: Elements): void => {
9-
const state = useStoreState((s) => ({
10-
nodes: s.nodes,
11-
edges: s.edges,
12-
transform: s.transform,
13-
snapToGrid: s.snapToGrid,
14-
snapGrid: s.snapGrid,
15-
}));
9+
const stateNodes = useStoreState((s) => s.nodes);
10+
const stateEdges = useStoreState((s) => s.edges);
1611

1712
const setNodes = useStoreActions((a) => a.setNodes);
1813
const setEdges = useStoreActions((a) => a.setEdges);
@@ -22,7 +17,7 @@ const useElementUpdater = (elements: Elements): void => {
2217
const edges: Edge[] = elements.filter(isEdge).map((e) => parseElement(e) as Edge);
2318

2419
const nextNodes: Node[] = nodes.map((propNode) => {
25-
const existingNode = state.nodes.find((n) => n.id === propNode.id);
20+
const existingNode = stateNodes.find((n) => n.id === propNode.id);
2621

2722
if (existingNode) {
2823
const data = !isEqual(existingNode.data, propNode.data)
@@ -59,8 +54,8 @@ const useElementUpdater = (elements: Elements): void => {
5954
return parseElement(propNode) as Node;
6055
});
6156

62-
const nodesChanged: boolean = !isEqual(state.nodes, nextNodes);
63-
const edgesChanged: boolean = !isEqual(state.edges, edges);
57+
const nodesChanged: boolean = !isEqual(stateNodes, nextNodes);
58+
const edgesChanged: boolean = !isEqual(stateEdges, edges);
6459

6560
if (nodesChanged) {
6661
setNodes(nextNodes);
@@ -69,7 +64,7 @@ const useElementUpdater = (elements: Elements): void => {
6964
if (edgesChanged) {
7065
setEdges(edges);
7166
}
72-
}, [elements, state.nodes, state.edges]);
67+
}, [elements, stateNodes, stateEdges]);
7368
};
7469

7570
export default useElementUpdater;

0 commit comments

Comments
 (0)