1 import { useLearningPath } from "@/providers/LearningPath"
2 import React, { useCallback, useEffect, useRef, useState } from "react"
3 import { LearningPathStepActions } from "./Actions"
4 import clsx from "clsx"
5 import { IconCircleDottedLine } from "@/components/Icons"
6 import { CheckCircleSolid, CircleMiniSolid, ListBullet } from "@medusajs/icons"
7 import { Badge } from "@/components/Badge"
8 import { Button } from "@/components/Button"
9 import { Link } from "@/components/Link"
10 // @ts-expect-error can't install the types package because it doesn't support React v19
11 import { CSSTransition, SwitchTransition } from "react-transition-group"
12
13 type LearningPathStepsProps = {
14 onFinish?: () => void
15 onClose?: () => void
16 }
17
18 export const LearningPathSteps = ({ ...rest }: LearningPathStepsProps) => {
19 const { path, currentStep, goToStep } = useLearningPath()
20 const [collapsed, setCollapsed] = useState(false)
21 const stepsRef = useRef<HTMLDivElement>(null)
22 const buttonRef = useRef<HTMLButtonElement>(null)
23 const nodeRef = collapsed ? buttonRef : stepsRef
24
25 const handleScroll = useCallback(() => {
26 if (window.scrollY > 100 && !collapsed) {
27 // automatically collapse steps
28 setCollapsed(true)
29 } else if (
30 (window.scrollY === 0 ||
31 window.scrollY + window.innerHeight >= document.body.scrollHeight) &&
32 collapsed
33 ) {
34 // automatically open steps
35 setCollapsed(false)
36 }
37 }, [collapsed])
38
39 useEffect(() => {
40 window.addEventListener("scroll", handleScroll)
41
42 return () => {
43 window.removeEventListener("scroll", handleScroll)
44 }
45 }, [handleScroll])
46
47 if (!path) {
48 return <></>
49 }
50
51 return (
52 <SwitchTransition>
53 <CSSTransition
54 key={collapsed ? "show_path" : "show_button"}
55 nodeRef={nodeRef}
56 timeout={300}
57 addEndListener={(done: () => void) => {
58 nodeRef.current?.addEventListener("transitionend", done, false)
59 }}
60 classNames={{
61 enter: "animate-maximize animate-fast",
62 exit: "animate-minimize animate-fast",
63 }}
64 >
65 <>
66 {!collapsed && (
67 <div
68 className={clsx(
69 "bg-medusa-bg-base shadow-elevation-flyout dark:shadow-elevation-flyout-dark rounded",
70 "transition-transform origin-bottom-right flex flex-col"
71 )}
72 ref={stepsRef}
73 >
74 <div className="overflow-auto basis-3/4">
75 {path.steps.map((step, index) => (
76 <div
77 className={clsx(
78 "border-0 border-b border-solid border-medusa-border-base",
79 "relative p-docs_1"
80 )}
81 key={index}
82 >
83 <div
84 className={clsx("flex items-center gap-docs_1 relative")}
85 >
86 <div className="w-docs_2 flex-none flex items-center justify-center">
87 {index === currentStep && (
88 <IconCircleDottedLine
89 className={clsx(
90 "shadow-active dark:shadow-active-dark rounded-full",
91 "!text-ui-fg-interactive"
92 )}
93 />
94 )}
95 {index < currentStep && (
96 <CheckCircleSolid className="text-ui-fg-interactive" />
97 )}
98 {index > currentStep && (
99 <CircleMiniSolid className="text-ui-fg-subtle" />
100 )}
101 </div>
102 <span
103 className={clsx(
104 "text-compact-medium-plus text-medusa-fg-base"
105 )}
106 >
107 {step.title}
108 </span>
109 <Link
110 href={step.path}
111 className={clsx("absolute top-0 left-0 w-full h-full")}
112 onClick={(e) => {
113 e.preventDefault()
114 goToStep(index)
115 }}
116 />
117 </div>
118 {index === currentStep && (
119 <div className={clsx("flex items-center gap-docs_1")}>
120 <div className="w-docs_2 flex-none"></div>
121 <div
122 className={clsx(
123 "text-medium text-ui-fg-subtle mt-docs_1"
124 )}
125 >
126 {(step.descriptionJSX as React.ReactNode) ??
127 step.description}
128 </div>
129 </div>
130 )}
131 </div>
132 ))}
133 </div>
134 <LearningPathStepActions setCollapsed={setCollapsed} {...rest} />
135 </div>
136 )}
137 {collapsed && (
138 <Button
139 variant="secondary"
140 className={clsx(
141 "!p-[10px] !shadow-elevation-flyout dark:!shadow-elevation-flyout-dark !text-medusa-fg-subtle w-fit h-fit",
142 "rounded-full border-0 mr-0 ml-auto fixed md:relative max-[767px]:bottom-docs_1 max-[767px]:right-docs_1 "
143 )}
144 onClick={() => setCollapsed(false)}
145 buttonRef={buttonRef}
146 >
147 <ListBullet />
148 <Badge
149 variant="blue"
150 className={clsx("absolute -top-docs_0.25 -right-docs_0.25")}
151 >
152 !
153 </Badge>
154 </Button>
155 )}
156 </>
157 </CSSTransition>
158 </SwitchTransition>
159 )
160 }