1 "use client"
2
3 import { Text } from "@medusajs/ui"
4 import clsx from "clsx"
5 import Link from "next/link"
6 import React, { useEffect, useMemo, useRef, useState } from "react"
7 import { WorkflowStepUi } from "types"
8 import { CodeBlock } from "@/components/CodeBlock"
9 import { MarkdownContent } from "@/components/MarkdownContent"
10 import { Tooltip } from "@/components/Tooltip"
11 import { Bolt, InformationCircle } from "@medusajs/icons"
12 import { getBrowser } from "@/utils/os-browser-utils"
13
14 export type WorkflowDiagramNodeProps = {
15 step: WorkflowStepUi
16 }
17
18 export const WorkflowDiagramStepNode = ({ step }: WorkflowDiagramNodeProps) => {
19 const stepId = step.name.split(".").pop()
20 const [offset, setOffset] = useState<number | undefined>(undefined)
21 const ref = useRef<HTMLSpanElement>(null)
22
23 const description = useMemo(() => {
24 return step.description?.replaceAll(/:::[a-z]*/g, "") || ""
25 }, [step.description])
26
27 useEffect(() => {
28 if (!ref.current) {
29 return
30 }
31
32 // find parent
33 const diagramParent = ref.current.closest(".workflow-list-diagram")
34 const nodeParent = ref.current.closest(".workflow-node-group")
35
36 if (!diagramParent || !nodeParent) {
37 return
38 }
39
40 const firstChild = nodeParent.firstChild as HTMLElement
41
42 const nodeBoundingRect = nodeParent.getBoundingClientRect()
43 const diagramBoundingRect = diagramParent.getBoundingClientRect()
44 const browser = getBrowser()
45
46 if (browser === "Safari") {
47 // React Tooltip has a bug in Safari where the offset is not calculated correctly
48 // when place is set.
49 const firstChildBoundingRect = firstChild.getBoundingClientRect()
50 setOffset(diagramBoundingRect.width - firstChildBoundingRect.width + 20)
51 } else {
52 setOffset(
53 Math.max(diagramBoundingRect.width - nodeBoundingRect.width + 10, 10)
54 )
55 }
56 }, [ref.current])
57
58 const unindentLines = (str: string) => {
59 let minIndent = 4
60 return str
61 .split("\n")
62 .reverse()
63 .map((line, index) => {
64 const trimmedStartLine = line.trimStart()
65 const numberOfSpaces = line.length - trimmedStartLine.length
66 if (index === 0) {
67 minIndent = numberOfSpaces || minIndent
68 }
69 if (numberOfSpaces >= minIndent) {
70 return " ".repeat(numberOfSpaces - 4) + trimmedStartLine
71 }
72
73 return line
74 })
75 .reverse()
76 .join("\n")
77 }
78
79 return (
80 <Tooltip
81 tooltipClassName="!text-left max-w-[300px] text-pretty overflow-scroll"
82 tooltipChildren={
83 <>
84 <h4 className="text-compact-x-small-plus">{step.name}</h4>
85 {description && (
86 <MarkdownContent
87 allowedElements={["a", "strong", "code"]}
88 unwrapDisallowed={true}
89 >
90 {description}
91 </MarkdownContent>
92 )}
93 {step.when?.condition && (
94 <CodeBlock
95 lang="typescript"
96 source={unindentLines(step.when.condition)}
97 noReport
98 noCopy
99 noAskAi
100 noLineNumbers
101 title="when Condition"
102 wrapperClassName="mt-docs_0.5"
103 />
104 )}
105 </>
106 }
107 clickable={true}
108 place="right"
109 offset={offset}
110 ref={ref}
111 >
112 <Link
113 href={step.link || `#${step.name.toLowerCase()}`}
114 className="focus-visible:shadow-borders-focus transition-fg rounded-docs_sm outline-none"
115 >
116 <div
117 className={clsx(
118 "shadow-borders-base flex w-fit bg-medusa-bg-base",
119 "items-center rounded-docs_sm py-docs_0.125 px-docs_0.5",
120 (step.type === "hook" || step.when) && "gap-x-docs_0.125"
121 )}
122 data-step-id={step.name}
123 >
124 {step.type === "hook" && (
125 <div className="flex size-[20px] items-center justify-center text-medusa-tag-orange-icon">
126 <Bolt />
127 </div>
128 )}
129 {step.when && (
130 <div className="flex size-[20px] items-center justify-center text-medusa-tag-green-icon">
131 <InformationCircle />
132 </div>
133 )}
134 <Text
135 size="xsmall"
136 leading="compact"
137 weight="plus"
138 className="select-none"
139 data-testid="step-name"
140 >
141 {stepId}
142 </Text>
143 </div>
144 </Link>
145 </Tooltip>
146 )
147 }