Skip to content

Commit ba46c4d

Browse files
committed
simplify layout component
1 parent a5fd075 commit ba46c4d

File tree

13 files changed

+40
-146
lines changed

13 files changed

+40
-146
lines changed

web/common/src/components/Lineage/LineageColumnLevel/FactoryColumn.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export function FactoryColumn<
233233
<NodeBadge className="FactoryColumn__NodeBadge">{type}</NodeBadge>
234234
}
235235
className={cn(
236-
'FactoryColumn__Metadata relative overflow-visible group',
236+
'FactoryColumn__Metadata relative overflow-visible',
237237
isDisabledColumn && 'cursor-not-allowed',
238238
className,
239239
)}

web/common/src/components/Lineage/LineageColumnLevel/help.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ export function calculateColumnsHeight({
236236
const hasColumns = columnsCount > 0
237237
const columnHeight = 24 // tailwind h-6
238238
const columnsTopSeparator = 1
239-
const columnSeparator = 1
239+
const columnSeparator = 0
240240
const columnsContainerPadding = 4
241-
const columnsPadding = 4
241+
const columnsPadding = 0
242242
const columnsFilterHeight = hasColumnsFilter && hasColumns ? columnHeight : 0
243243
const columnsSeparators = columnsCount > 1 ? columnsCount - 1 : 0
244244

web/common/src/components/Lineage/LineageContext.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export interface LineageContextValue<
6464
>
6565
currentNodeId: TNodeID | null
6666
selectedNode: LineageNode<TNodeData, TNodeID> | null
67-
currentNode: LineageNode<TNodeData, TNodeID> | null
6867
}
6968

7069
export function getInitial<
@@ -89,7 +88,6 @@ export function getInitial<
8988
selectedNodeId: null,
9089
selectedNode: null,
9190
currentNodeId: null,
92-
currentNode: null,
9391
}
9492
}
9593

web/common/src/components/Lineage/LineageLayout.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@ export function LineageLayout<
3535
edgeTypes,
3636
className,
3737
controls,
38-
nodesDraggable,
39-
nodesConnectable,
4038
isBuildingLayout,
4139
useLineage,
4240
onNodeClick,
4341
onNodeDoubleClick,
4442
showControlOnlySelectedNodes,
45-
showControlZoomToCurrentNode,
4643
showControlZoomToSelectedNode,
4744
}: {
4845
useLineage: LineageContextHook<
@@ -59,10 +56,7 @@ export function LineageLayout<
5956
nodeTypes?: NodeTypes
6057
edgeTypes?: EdgeTypes
6158
className?: string
62-
nodesDraggable?: boolean
63-
nodesConnectable?: boolean
6459
showControlOnlySelectedNodes?: boolean
65-
showControlZoomToCurrentNode?: boolean
6660
showControlZoomToSelectedNode?: boolean
6761
controls?:
6862
| React.ReactNode
@@ -85,14 +79,11 @@ export function LineageLayout<
8579
<LineageLayoutBase
8680
nodeTypes={nodeTypes}
8781
edgeTypes={edgeTypes}
88-
nodesDraggable={nodesDraggable}
89-
nodesConnectable={nodesConnectable}
9082
controls={controls}
9183
useLineage={useLineage}
9284
onNodeClick={onNodeClick}
9385
onNodeDoubleClick={onNodeDoubleClick}
9486
showControlOnlySelectedNodes={showControlOnlySelectedNodes}
95-
showControlZoomToCurrentNode={showControlZoomToCurrentNode}
9687
showControlZoomToSelectedNode={showControlZoomToSelectedNode}
9788
/>
9889
</LineageLayoutContainer>

web/common/src/components/Lineage/LineageLayoutBase.tsx

Lines changed: 9 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import {
22
Background,
33
BackgroundVariant,
44
Controls,
5-
type EdgeChange,
65
type EdgeTypes,
7-
type NodeChange,
86
type NodeTypes,
97
ReactFlow,
108
type SetCenter,
@@ -13,14 +11,12 @@ import {
1311
getOutgoers,
1412
useReactFlow,
1513
useViewport,
16-
applyNodeChanges,
17-
applyEdgeChanges,
1814
} from '@xyflow/react'
1915

2016
import '@xyflow/react/dist/style.css'
2117
import './Lineage.css'
2218

23-
import { CircuitBoard, Crosshair, LocateFixed, RotateCcw } from 'lucide-react'
19+
import { CircuitBoard, LocateFixed, RotateCcw } from 'lucide-react'
2420
import React from 'react'
2521

2622
import { type LineageContextHook } from './LineageContext'
@@ -60,13 +56,10 @@ export function LineageLayoutBase<
6056
edgeTypes,
6157
className,
6258
controls,
63-
nodesDraggable = false,
64-
nodesConnectable = false,
6559
useLineage,
6660
onNodeClick,
6761
onNodeDoubleClick,
6862
showControlOnlySelectedNodes = true,
69-
showControlZoomToCurrentNode = true,
7063
showControlZoomToSelectedNode = true,
7164
}: {
7265
useLineage: LineageContextHook<
@@ -79,17 +72,14 @@ export function LineageLayoutBase<
7972
TSourceHandleID,
8073
TTargetHandleID
8174
>
82-
nodesDraggable?: boolean
83-
nodesConnectable?: boolean
8475
nodeTypes?: NodeTypes
8576
edgeTypes?: EdgeTypes
8677
className?: string
8778
showControlOnlySelectedNodes?: boolean
88-
showControlZoomToCurrentNode?: boolean
8979
showControlZoomToSelectedNode?: boolean
9080
controls?:
91-
| React.ReactNode
92-
| (({ setCenter }: { setCenter: SetCenter }) => React.ReactNode)
81+
| React.ReactNode
82+
| (({ setCenter }: { setCenter: SetCenter }) => React.ReactNode)
9383
onNodeClick?: (
9484
event: React.MouseEvent<Element, MouseEvent>,
9585
node: LineageNode<TNodeData, TNodeID>,
@@ -103,74 +93,20 @@ export function LineageLayoutBase<
10393
const { setCenter } = useReactFlow()
10494

10595
const {
106-
currentNode,
10796
zoom,
108-
nodes: initialNodes,
97+
nodes,
10998
edges,
110-
setEdges,
11199
selectedNode,
112100
showOnlySelectedNodes,
113101
selectedNodeId,
114102
setZoom,
115-
setSelectedNodeId,
116103
setShowOnlySelectedNodes,
117104
setSelectedNodes,
118105
setSelectedEdges,
119106
} = useLineage()
120107

121-
const [nodes, setNodes] =
122-
React.useState<LineageNode<TNodeData, TNodeID>[]>(initialNodes)
123-
124-
const onNodesChange = React.useCallback(
125-
(changes: NodeChange<LineageNode<TNodeData, TNodeID>>[]) => {
126-
setNodes(applyNodeChanges(changes, nodes))
127-
},
128-
[nodes],
129-
)
130-
131-
const onEdgesChange = React.useCallback(
132-
(
133-
changes: EdgeChange<
134-
LineageEdge<
135-
TEdgeData,
136-
TEdgeID,
137-
TSourceID,
138-
TTargetID,
139-
TSourceHandleID,
140-
TTargetHandleID
141-
>
142-
>[],
143-
) => {
144-
setEdges(
145-
applyEdgeChanges<
146-
LineageEdge<
147-
TEdgeData,
148-
TEdgeID,
149-
TSourceID,
150-
TTargetID,
151-
TSourceHandleID,
152-
TTargetHandleID
153-
>
154-
>(changes, edges),
155-
)
156-
},
157-
[edges],
158-
)
159-
160108
const updateZoom = React.useMemo(() => debounce(setZoom, 200), [setZoom])
161109

162-
const zoomToCurrentNode = React.useCallback(
163-
(zoom: number = DEFAULT_ZOOM) => {
164-
if (currentNode) {
165-
setCenter(currentNode.position.x, currentNode.position.y, {
166-
zoom,
167-
duration: 0,
168-
})
169-
}
170-
},
171-
[currentNode?.position.x, currentNode?.position.y],
172-
)
173-
174110
const zoomToSelectedNode = React.useCallback(
175111
(zoom: number = DEFAULT_ZOOM) => {
176112
if (selectedNode) {
@@ -195,13 +131,13 @@ export function LineageLayoutBase<
195131
return Array.from(
196132
new Set<LineageNode<TNodeData, TNodeID>>([
197133
node,
198-
...getIncomers(node, initialNodes, edges)
134+
...getIncomers(node, nodes, edges)
199135
.map(n => getAllIncomers(n, visited))
200136
.flat(),
201137
]),
202138
)
203139
},
204-
[initialNodes, edges],
140+
[nodes, edges],
205141
)
206142

207143
const getAllOutgoers = React.useCallback(
@@ -216,13 +152,13 @@ export function LineageLayoutBase<
216152
return Array.from(
217153
new Set<LineageNode<TNodeData, TNodeID>>([
218154
node,
219-
...getOutgoers(node, initialNodes, edges)
155+
...getOutgoers(node, nodes, edges)
220156
.map(n => getAllOutgoers(n, visited))
221157
.flat(),
222158
]),
223159
)
224160
},
225-
[initialNodes, edges],
161+
[nodes, edges],
226162
)
227163

228164
const connectedNodes = React.useMemo(() => {
@@ -233,12 +169,8 @@ export function LineageLayoutBase<
233169
...getAllOutgoers(selectedNode),
234170
]
235171

236-
if (currentNode) {
237-
all.push(currentNode)
238-
}
239-
240172
return all
241-
}, [selectedNode, currentNode, getAllIncomers, getAllOutgoers])
173+
}, [selectedNode, getAllIncomers, getAllOutgoers])
242174

243175
const connectedEdges = React.useMemo(() => {
244176
return getConnectedEdges<
@@ -254,19 +186,11 @@ export function LineageLayoutBase<
254186
>(connectedNodes, edges)
255187
}, [connectedNodes, edges])
256188

257-
React.useEffect(() => {
258-
setNodes(initialNodes)
259-
}, [initialNodes])
260-
261189
React.useEffect(() => {
262190
if (selectedNodeId == null) {
263191
setShowOnlySelectedNodes(false)
264192
setSelectedNodes(new Set())
265193
setSelectedEdges(new Set())
266-
} else {
267-
if (selectedNode == null) {
268-
setSelectedNodeId(null)
269-
}
270194
}
271195
}, [selectedNodeId, selectedNode])
272196

@@ -317,17 +241,12 @@ export function LineageLayoutBase<
317241
edges={edges}
318242
nodeTypes={nodeTypes}
319243
edgeTypes={edgeTypes}
320-
onNodesChange={onNodesChange}
321-
onEdgesChange={onEdgesChange}
322-
nodesDraggable={nodesDraggable}
323-
nodesConnectable={nodesConnectable}
324244
zoomOnDoubleClick={false}
325245
panOnScroll={true}
326246
zoomOnScroll={true}
327247
minZoom={nodes.length > NODES_TRESHOLD ? NODES_TRESHOLD_ZOOM : MIN_ZOOM}
328248
maxZoom={MAX_ZOOM}
329249
fitView={false}
330-
nodeOrigin={[0.5, 0.5]}
331250
onlyRenderVisibleElements
332251
onNodeClick={onNodeClick}
333252
onNodeDoubleClick={onNodeDoubleClick}
@@ -346,14 +265,6 @@ export function LineageLayoutBase<
346265
position="top-right"
347266
className="m-1 border-2 border-lineage-control-border rounded-sm overflow-hidden"
348267
>
349-
{currentNode && showControlZoomToCurrentNode && (
350-
<LineageControlButton
351-
text="Zoom to current node"
352-
onClick={() => zoomToCurrentNode(DEFAULT_ZOOM)}
353-
>
354-
<LineageControlIcon Icon={Crosshair} />
355-
</LineageControlButton>
356-
)}
357268
{selectedNodeId && (
358269
<>
359270
{showControlOnlySelectedNodes && (

web/common/src/components/Lineage/help.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ export function getTransformedNodes<
4141
adjacencyListKeys: TAdjacencyListKey[],
4242
lineageDetails: LineageDetails<TAdjacencyListKey, TDetailsNode>,
4343
transformNode: TransformNodeFn<TDetailsNode, TNodeData, TNodeID>,
44+
allNodesMap?: LineageNodesMap<TNodeData, TNodeID>,
4445
): LineageNodesMap<TNodeData, TNodeID> {
4546
const nodesCount = adjacencyListKeys.length
4647
const nodesMap: LineageNodesMap<TNodeData, TNodeID> = Object.create(null)
4748

4849
for (let i = 0; i < nodesCount; i++) {
4950
const adjacencyListKey = adjacencyListKeys[i]
50-
const encodedNodeId = toNodeID<TNodeID>(adjacencyListKey)
51-
nodesMap[encodedNodeId] = transformNode(
52-
encodedNodeId,
53-
lineageDetails[adjacencyListKey],
54-
)
51+
const nodeId = toNodeID<TNodeID>(adjacencyListKey)
52+
nodesMap[nodeId] =
53+
allNodesMap?.[nodeId] ||
54+
transformNode(nodeId, lineageDetails[adjacencyListKey])
5555
}
5656

5757
return nodesMap

web/common/src/components/Lineage/layout/dagreLayout.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function buildLayout<
2121
>({
2222
edges,
2323
nodesMap,
24+
shouldReuseExistingPosition = true,
2425
}: {
2526
edges: LineageEdge<
2627
TEdgeData,
@@ -31,6 +32,7 @@ export function buildLayout<
3132
TTargetHandleID
3233
>[]
3334
nodesMap: LineageNodesMap<TNodeData>
35+
shouldReuseExistingPosition?: boolean
3436
}) {
3537
const nodes = Object.values(nodesMap)
3638
const nodeCount = nodes.length
@@ -46,7 +48,7 @@ export function buildLayout<
4648

4749
g.setGraph({
4850
rankdir: 'LR',
49-
nodesep: 0,
51+
nodesep: 12,
5052
ranksep: 48,
5153
edgesep: 0,
5254
ranker: 'longest-path',
@@ -78,12 +80,19 @@ export function buildLayout<
7880
const nodeWithPosition = g.node(nodeId)
7981
const halfWidth = width / 2
8082
const halfHeight = height / 2
83+
const isDefaultPosition = node.position.x === 0 && node.position.y === 0
8184

8285
nodesMap[nodeId] = {
8386
...node,
8487
position: {
85-
x: nodeWithPosition.x - halfWidth,
86-
y: nodeWithPosition.y - halfHeight,
88+
x:
89+
shouldReuseExistingPosition && isDefaultPosition
90+
? nodeWithPosition.x - halfWidth
91+
: node.position.x,
92+
y:
93+
shouldReuseExistingPosition && isDefaultPosition
94+
? nodeWithPosition.y - halfHeight
95+
: node.position.y,
8796
},
8897
}
8998
}

0 commit comments

Comments
 (0)