|
1
|
"use client" |
|
2
|
|
|
3
|
import clsx from "clsx" |
|
4
|
import { |
|
5
|
useAnimationControls, |
|
6
|
useDragControls, |
|
7
|
useMotionValue, |
|
8
|
motion, |
|
9
|
} from "framer-motion" |
|
10
|
import React, { useEffect, useRef, useState } from "react" |
|
11
|
import { ArrowPathMini, MinusMini, PlusMini } from "@medusajs/icons" |
|
12
|
import { DropdownMenu, Text } from "@medusajs/ui" |
|
13
|
import { |
|
14
|
createNodeClusters, |
|
15
|
getNextCluster, |
|
16
|
} from "@/utils/workflow-diagram-utils" |
|
17
|
import { WorkflowDiagramCanvasDepth } from "./Depth" |
|
18
|
import { WorkflowDiagramCommonProps } from "../../.." |
|
19
|
|
|
20
|
type ZoomScale = 0.5 | 0.75 | 1 |
|
21
|
|
|
22
|
const defaultState = { |
|
23
|
x: -1000, |
|
24
|
y: -1020, |
|
25
|
scale: 1, |
|
26
|
} |
|
27
|
|
|
28
|
const MAX_ZOOM = 1.5 |
|
29
|
const MIN_ZOOM = 0.5 |
|
30
|
const ZOOM_STEP = 0.25 |
|
31
|
|
|
32
|
export const WorkflowDiagramCanvas = ({ |
|
33
|
workflow, |
|
34
|
}: WorkflowDiagramCommonProps) => { |
|
35
|
const [zoom, setZoom] = useState<number>(1) |
|
36
|
const [isDragging, setIsDragging] = useState(false) |
|
37
|
|
|
38
|
const scale = useMotionValue(defaultState.scale) |
|
39
|
const x = useMotionValue(defaultState.x) |
|
40
|
const y = useMotionValue(defaultState.y) |
|
41
|
|
|
42
|
const controls = useAnimationControls() |
|
43
|
|
|
44
|
const dragControls = useDragControls() |
|
45
|
const dragConstraints = useRef<HTMLDivElement>(null) |
|
46
|
|
|
47
|
const canZoomIn = zoom < MAX_ZOOM |
|
48
|
const canZoomOut = zoom > MIN_ZOOM |
|
49
|
|
|
50
|
useEffect(() => { |
|
51
|
const unsubscribe = scale.on("change", (latest) => { |
|
52
|
setZoom(latest as ZoomScale) |
|
53
|
}) |
|
54
|
|
|
55
|
return () => { |
|
56
|
unsubscribe() |
|
57
|
} |
|
58
|
}, [scale]) |
|
59
|
|
|
60
|
const clusters = createNodeClusters(workflow.steps) |
|
61
|
|
|
62
|
function scaleXandY( |
|
63
|
prevScale: number, |
|
64
|
newScale: number, |
|
65
|
x: number, |
|
66
|
y: number |
|
67
|
) { |
|
68
|
const scaleRatio = newScale / prevScale |
|
69
|
return { |
|
70
|
x: x * scaleRatio, |
|
71
|
y: y * scaleRatio, |
|
72
|
} |
|
73
|
} |
|
74
|
|
|
75
|
const changeZoom = (newScale: number) => { |
|
76
|
const { x: newX, y: newY } = scaleXandY(zoom, newScale, x.get(), y.get()) |
|
77
|
|
|
78
|
setZoom(newScale) |
|
79
|
controls.set({ scale: newScale, x: newX, y: newY }) |
|
80
|
} |
|
81
|
|
|
82
|
const zoomIn = () => { |
|
83
|
const curr = scale.get() |
|
84
|
|
|
85
|
if (curr < 1.5) { |
|
86
|
const newScale = curr + ZOOM_STEP |
|
87
|
changeZoom(newScale) |
|
88
|
} |
|
89
|
} |
|
90
|
|
|
91
|
const zoomOut = () => { |
|
92
|
const curr = scale.get() |
|
93
|
|
|
94
|
if (curr > 0.5) { |
|
95
|
const newScale = curr - ZOOM_STEP |
|
96
|
changeZoom(newScale) |
|
97
|
} |
|
98
|
} |
|
99
|
|
|
100
|
const resetCanvas = async () => await controls.start(defaultState) |
|
101
|
|
|
102
|
return ( |
|
103
|
<div className="h-[200px] w-full rounded-docs_DEFAULT"> |
|
104
|
<div |
|
105
|
ref={dragConstraints} |
|
106
|
className="relative size-full rounded-docs_DEFAULT" |
|
107
|
> |
|
108
|
<div className="relative size-full overflow-hidden object-contain rounded-docs_DEFAULT shadow-elevation-card-rest"> |
|
109
|
<div> |
|
110
|
{/* @ts-expect-error React v19 doesn't see this type as a React element */} |
|
111
|
<motion.div |
|
112
|
// @ts-expect-error React v19 isn't recognizing accepted props |
|
113
|
onMouseDown={() => setIsDragging(true)} |
|
114
|
onMouseUp={() => setIsDragging(false)} |
|
115
|
drag |
|
116
|
dragConstraints={dragConstraints} |
|
117
|
dragElastic={0} |
|
118
|
dragMomentum={false} |
|
119
|
dragControls={dragControls} |
|
120
|
initial={false} |
|
121
|
animate={controls} |
|
122
|
transition={{ duration: 0.25 }} |
|
123
|
style={{ |
|
124
|
x, |
|
125
|
y, |
|
126
|
scale, |
|
127
|
}} |
|
128
|
className={clsx( |
|
129
|
"bg-medusa-bg-subtle relative size-[500rem] origin-top-left items-start justify-start overflow-hidden", |
|
130
|
"bg-[radial-gradient(var(--docs-border-base)_1.5px,transparent_0)] bg-[length:20px_20px] bg-repeat", |
|
131
|
!isDragging && "cursor-grab", |
|
132
|
isDragging && "cursor-grabbing" |
|
133
|
)} |
|
134
|
> |
|
135
|
<main className="size-full"> |
|
136
|
<div className="absolute left-[1100px] top-[1100px] flex select-none items-start"> |
|
137
|
{Object.entries(clusters).map(([depth, cluster]) => { |
|
138
|
const next = getNextCluster(clusters, Number(depth)) |
|
139
|
|
|
140
|
return ( |
|
141
|
<WorkflowDiagramCanvasDepth |
|
142
|
cluster={cluster} |
|
143
|
next={next} |
|
144
|
key={depth} |
|
145
|
/> |
|
146
|
) |
|
147
|
})} |
|
148
|
</div> |
|
149
|
</main> |
|
150
|
</motion.div> |
|
151
|
</div> |
|
152
|
</div> |
|
153
|
<div className="bg-medusa-bg-base shadow-borders-base text-medusa-fg-subtle absolute bottom-docs_1 left-docs_1.5 flex h-[28px] items-center overflow-hidden rounded-docs_sm"> |
|
154
|
<div className="flex items-center"> |
|
155
|
<button |
|
156
|
onClick={zoomIn} |
|
157
|
type="button" |
|
158
|
disabled={!canZoomIn} |
|
159
|
aria-label="Zoom in" |
|
160
|
className="disabled:text-medusa-fg-disabled transition-fg hover:bg-medusa-bg-base-hover active:bg-medusa-bg-base-pressed focus-visible:bg-medusa-bg-base-pressed border-r p-docs_0.25 outline-none" |
|
161
|
> |
|
162
|
<PlusMini /> |
|
163
|
</button> |
|
164
|
<div> |
|
165
|
<DropdownMenu> |
|
166
|
<DropdownMenu.Trigger |
|
167
|
className="disabled:text-medusa-fg-disabled transition-fg hover:bg-medusa-bg-base-hover active:bg-medusa-bg-base-pressed focus-visible:bg-medusa-bg-base-pressed flex w-[50px] items-center justify-center border-r p-docs_0.25 outline-none" |
|
168
|
data-testid="zoom-percentage-dropdown-trigger" |
|
169
|
> |
|
170
|
<Text |
|
171
|
as="span" |
|
172
|
size="xsmall" |
|
173
|
leading="compact" |
|
174
|
className="select-none tabular-nums" |
|
175
|
> |
|
176
|
{Math.round(zoom * 100)}% |
|
177
|
</Text> |
|
178
|
</DropdownMenu.Trigger> |
|
179
|
<DropdownMenu.Content className="bg-medusa-bg-base"> |
|
180
|
{[50, 75, 100, 125, 150].map((value) => ( |
|
181
|
<DropdownMenu.Item |
|
182
|
key={value} |
|
183
|
onClick={() => changeZoom(value / 100)} |
|
184
|
className="px-docs_0.5 py-[6px]" |
|
185
|
> |
|
186
|
{value}% |
|
187
|
</DropdownMenu.Item> |
|
188
|
))} |
|
189
|
</DropdownMenu.Content> |
|
190
|
</DropdownMenu> |
|
191
|
</div> |
|
192
|
<button |
|
193
|
onClick={zoomOut} |
|
194
|
type="button" |
|
195
|
disabled={!canZoomOut} |
|
196
|
aria-label="Zoom out" |
|
197
|
className="disabled:text-medusa-fg-disabled transition-fg hover:bg-medusa-bg-base-hover active:bg-medusa-bg-base-pressed focus-visible:bg-medusa-bg-base-pressed border-r p-docs_0.25 outline-none" |
|
198
|
> |
|
199
|
<MinusMini /> |
|
200
|
</button> |
|
201
|
</div> |
|
202
|
<button |
|
203
|
onClick={resetCanvas} |
|
204
|
type="button" |
|
205
|
aria-label="Reset canvas" |
|
206
|
className="disabled:text-medusa-fg-disabled transition-fg hover:bg-medusa-bg-base-hover active:bg-medusa-bg-base-pressed focus-visible:bg-medusa-bg-base-pressed p-docs_0.25 outline-none" |
|
207
|
> |
|
208
|
<ArrowPathMini /> |
|
209
|
</button> |
|
210
|
</div> |
|
211
|
</div> |
|
212
|
</div> |
|
213
|
) |
|
214
|
} |