-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathArchitecture.tsx
220 lines (195 loc) · 5.95 KB
/
Architecture.tsx
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
import ReactFlow, {
Background,
MiniMap,
addEdge,
useEdgesState,
useNodesState,
BackgroundVariant,
type Node,
useReactFlow,
type Edge,
ReactFlowProvider,
Position,
Panel,
} from 'reactflow'
import Dagre from '@dagrejs/dagre'
import 'reactflow/dist/style.css'
import './styles.css'
import AppLayout from '../layout/AppLayout'
import { useCallback, useEffect, useState } from 'react'
import { useWebSocket } from '@/lib/hooks/use-web-socket'
import ExportButton from './ExportButton'
import {
generateArchitectureData,
nodeTypes,
} from '@/lib/utils/generate-architecture-data'
import NitricEdge from './NitricEdge'
import { Switch } from '../ui/switch'
import { Label } from '../ui/label'
const g = new Dagre.graphlib.Graph().setDefaultEdgeLabel(() => ({}))
const nodeWidth = 200
const nodeHeight = 150
const getLayoutedElements = (
nodes: Node<any, string | undefined>[],
edges: Edge[],
direction = 'LR',
) => {
const isHorizontal = direction === 'LR'
g.setGraph({ rankdir: direction })
edges.forEach((edge) => g.setEdge(edge.source, edge.target))
nodes.forEach((node) =>
g.setNode(node.id, {
width: isHorizontal ? nodeWidth * 1.25 : nodeWidth,
height: nodeHeight,
}),
)
Dagre.layout(g)
return {
nodes: nodes.map((node) => {
const { x, y } = g.node(node.id)
return {
...node,
position: {
x: x - nodeWidth / 2,
y: y - nodeHeight / 2,
},
targetPosition: isHorizontal ? Position.Left : Position.Top,
sourcePosition: isHorizontal ? Position.Right : Position.Bottom,
}
}),
edges,
}
}
const edgeTypes = {
nitric: NitricEdge,
}
const LOCAL_STORAGE_KEY = 'nitric-local-dash-arch-options'
interface ArchOptions {
isHorizontal: boolean
}
const defaultOptions: ArchOptions = { isHorizontal: false }
const getOptions = (): ArchOptions => {
try {
const key = localStorage.getItem(LOCAL_STORAGE_KEY)
return key ? JSON.parse(key) : defaultOptions
} catch (e) {
return defaultOptions
}
}
const setOptions = (options: ArchOptions) => {
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(options))
}
function ReactFlowLayout() {
const [isHorizontal, setIsHorizontal] = useState(getOptions().isHorizontal)
const { fitView } = useReactFlow()
const { data } = useWebSocket()
const [nodes, setNodes, onNodesChange] = useNodesState([])
const [edges, setEdges, onEdgesChange] = useEdgesState([])
const onConnect = useCallback(
(params: any) => setEdges((eds) => addEdge(params, eds)),
[setEdges],
)
useEffect(() => {
if (!data) return
const { nodes, edges } = generateArchitectureData(data)
const layouted = getLayoutedElements(
nodes,
edges,
isHorizontal ? 'LR' : 'TB',
)
setNodes([...layouted.nodes])
setEdges([...layouted.edges])
setOptions({ isHorizontal })
window.requestAnimationFrame(() => {
setTimeout(
() =>
fitView({
minZoom: 1,
maxZoom: 1.5,
duration: 500, // animation duration of repositioning the arch diagram
}),
100, // ensure the diagram is 100% ready before re-fitting
)
})
}, [data, isHorizontal])
return (
<AppLayout
title="Architecture"
hideTitle
mainClassName="py-0 px-0 sm:px-0 lg:px-0 lg:py-0"
routePath={'/architecture'}
>
<div className="h-full overflow-hidden">
<div className="h-[calc(100vh-58px)] w-full overflow-x-hidden">
<ReactFlow
nodes={nodes}
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
defaultEdgeOptions={{
type: 'nitric',
className: 'text-foreground',
}}
className="bg-background"
onConnect={onConnect}
fitView
fitViewOptions={{
maxZoom: 1.5,
minZoom: 1,
}}
>
<MiniMap
pannable
zoomable
className="!bg-background border border-border"
/>
<Background
variant={BackgroundVariant.Dots}
gap={12}
size={1}
className="!text-border"
/>
{data?.projectName && (
<Panel position="top-right">
<div className="flex items-center gap-x-6">
<div className="flex items-center gap-x-2">
<Switch
id="horizontal-mode"
aria-label="Toggle Horizontal Mode"
checked={isHorizontal}
onCheckedChange={setIsHorizontal}
/>
<Label htmlFor="horizontal-mode">Horizontal</Label>
</div>
<ExportButton projectName={data.projectName} />
</div>
</Panel>
)}
<Panel position="bottom-left" className="flex flex-col gap-y-1">
<div className="rounded-md border-border bg-background p-2">
<div className="mb-2 text-center text-xs font-semibold text-foreground">
Connector Types
</div>
<div className="grid grid-cols-2 items-center gap-x-4 gap-y-2 text-xs font-semibold">
<span className="h-1 border-b-2 border-dashed border-foreground" />
<span className="text-foreground">Triggers</span>
<span className="h-1 border-b-2 border-dashed border-foreground" />
<span className="text-foreground">Dependencies</span>
</div>
</div>
</Panel>
</ReactFlow>
</div>
</div>
</AppLayout>
)
}
export default function Architecture() {
return (
<ReactFlowProvider>
<ReactFlowLayout />
</ReactFlowProvider>
)
}