-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathhelp.ts
More file actions
231 lines (198 loc) · 6.33 KB
/
help.ts
File metadata and controls
231 lines (198 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { Position } from '@xyflow/react'
import {
DEFAULT_NODE_HEIGHT,
DEFAULT_NODE_WIDTH,
type EdgeId,
type LineageAdjacencyList,
type LineageDetails,
type LineageEdge,
type LineageEdgeData,
type LineageNode,
type LineageNodeData,
type LineageNodesMap,
type NodeId,
type PortId,
toEdgeID,
toNodeID,
type TransformEdgeFn,
type TransformNodeFn,
} from './utils'
export function getOnlySelectedNodes<
TNodeData extends LineageNodeData = LineageNodeData,
TNodeID extends string = NodeId,
>(nodeMaps: LineageNodesMap<TNodeData, TNodeID>, selectedNodes: Set<TNodeID>) {
return (
Object.values(nodeMaps) satisfies LineageNode<TNodeData, TNodeID>[]
).reduce(
(acc, node) =>
selectedNodes.has(node.id) ? { ...acc, [node.id]: node } : acc,
{} as LineageNodesMap<TNodeData, TNodeID>,
)
}
export function getTransformedNodes<
TAdjacencyListKey extends string,
TDetailsNode,
TNodeData extends LineageNodeData = LineageNodeData,
TNodeID extends string = NodeId,
>(
adjacencyListKeys: TAdjacencyListKey[],
lineageDetails: LineageDetails<TAdjacencyListKey, TDetailsNode>,
transformNode: TransformNodeFn<TDetailsNode, TNodeData, TNodeID>,
): LineageNodesMap<TNodeData, TNodeID> {
const nodesCount = adjacencyListKeys.length
const nodesMap: LineageNodesMap<TNodeData, TNodeID> = Object.create(null)
for (let i = 0; i < nodesCount; i++) {
const adjacencyListKey = adjacencyListKeys[i]
const encodedNodeId = toNodeID<TNodeID>(adjacencyListKey)
nodesMap[encodedNodeId] = transformNode(
encodedNodeId,
lineageDetails[adjacencyListKey],
)
}
return nodesMap
}
export function getTransformedModelEdgesSourceTargets<
TAdjacencyListKey extends string,
TEdgeData extends LineageEdgeData = LineageEdgeData,
TNodeID extends string = NodeId,
TEdgeID extends string = EdgeId,
TPortID extends string = PortId,
>(
adjacencyListKeys: TAdjacencyListKey[],
lineageAdjacencyList: LineageAdjacencyList<TAdjacencyListKey>,
transformEdge: TransformEdgeFn<TEdgeData, TNodeID, TEdgeID, TPortID>,
) {
const nodesCount = adjacencyListKeys.length
if (nodesCount === 0) return []
const edges = []
for (let i = 0; i < nodesCount; i++) {
const sourceAdjacencyListKey = adjacencyListKeys[i]
const sourceNodeId = toNodeID<TNodeID>(sourceAdjacencyListKey)
const targets = lineageAdjacencyList[sourceAdjacencyListKey]
const targetsCount = targets?.length || 0
if (targets == null || targetsCount < 1) continue
for (let j = 0; j < targetsCount; j++) {
const targetAdjacencyListKey = targets[j]
if (!(targetAdjacencyListKey in lineageAdjacencyList)) continue
const edgeId = toEdgeID<TEdgeID>(
sourceAdjacencyListKey,
targetAdjacencyListKey,
)
const targetNodeId = toNodeID<TNodeID>(targetAdjacencyListKey)
edges.push(transformEdge('edge', edgeId, sourceNodeId, targetNodeId))
}
}
return edges
}
export function getTransformedModelEdgesTargetSources<
TAdjacencyListKey extends string,
TEdgeData extends LineageEdgeData = LineageEdgeData,
TNodeID extends string = NodeId,
TEdgeID extends string = EdgeId,
TPortID extends string = PortId,
>(
adjacencyListKeys: TAdjacencyListKey[],
lineageAdjacencyList: LineageAdjacencyList<TAdjacencyListKey>,
transformEdge: TransformEdgeFn<TEdgeData, TNodeID, TEdgeID, TPortID>,
) {
const nodesCount = adjacencyListKeys.length
if (nodesCount === 0) return []
const edges = []
for (let i = 0; i < nodesCount; i++) {
const targetAdjacencyListKey = adjacencyListKeys[i]
const targetNodeId = toNodeID<TNodeID>(targetAdjacencyListKey)
const sources = lineageAdjacencyList[targetAdjacencyListKey]
const sourcesCount = sources?.length || 0
if (sources == null || sourcesCount < 1) continue
for (let j = 0; j < sourcesCount; j++) {
const sourceAdjacencyListKey = sources[j]
if (!(sourceAdjacencyListKey in lineageAdjacencyList)) continue
const edgeId = toEdgeID<TEdgeID>(
sourceAdjacencyListKey,
targetAdjacencyListKey,
)
const sourceNodeId = toNodeID<TNodeID>(sourceAdjacencyListKey)
edges.push(transformEdge('edge', edgeId, sourceNodeId, targetNodeId))
}
}
return edges
}
export function createNode<
TNodeData extends LineageNodeData = LineageNodeData,
TNodeID extends string = NodeId,
>(type: string, nodeId: TNodeID, data: TNodeData) {
return {
id: nodeId,
sourcePosition: Position.Right,
targetPosition: Position.Left,
width: DEFAULT_NODE_WIDTH,
height: DEFAULT_NODE_HEIGHT,
data,
type,
hidden: false,
position: { x: 0, y: 0 },
zIndex: 10,
}
}
export function calculateNodeBaseHeight({
includeNodeFooterHeight = false,
includeCeilingHeight = false,
includeFloorHeight = false,
}: {
includeNodeFooterHeight?: boolean
includeCeilingHeight?: boolean
includeFloorHeight?: boolean
}) {
const border = 2
const footerHeight = 20 // tailwind h-5
const base = 28 // tailwind h-7
const ceilingHeight = 20 // tailwind h-5
const floorHeight = 20 // tailwind h-5
const ceilingGap = 4
const floorGap = 4
return [
border * 2,
base,
includeNodeFooterHeight ? footerHeight : 0,
includeCeilingHeight ? ceilingHeight + ceilingGap : 0,
includeFloorHeight ? floorHeight + floorGap : 0,
].reduce((acc, h) => acc + h, 0)
}
export function calculateNodeDetailsHeight({
nodeDetailsCount = 0,
}: {
nodeDetailsCount?: number
}) {
const nodeOptionHeight = 24 // tailwind h-6
const nodeOptionsSeparator = 1
const nodeOptionsSeparators = nodeDetailsCount > 1 ? nodeDetailsCount - 1 : 0
return [
nodeOptionsSeparators * nodeOptionsSeparator,
nodeDetailsCount * nodeOptionHeight,
].reduce((acc, h) => acc + h, 0)
}
export function createEdge<
TEdgeData extends LineageEdgeData = LineageEdgeData,
TNodeID extends string = NodeId,
TEdgeID extends string = EdgeId,
TPortID extends string = PortId,
>(
type: string,
edgeId: TEdgeID,
sourceId: TNodeID,
targetId: TNodeID,
sourceHandleId?: TPortID,
targetHandleId?: TPortID,
data?: TEdgeData,
): LineageEdge<TEdgeData, TNodeID, TEdgeID, TPortID> {
return {
id: edgeId,
source: sourceId,
target: targetId,
type,
sourceHandle: sourceHandleId ? sourceHandleId : undefined,
targetHandle: targetHandleId ? targetHandleId : undefined,
data,
zIndex: 1,
}
}