1"use client"
2
3import * as React from "react"
4import Image from "next/image"
5import { CheckIcon, ExternalLinkIcon } from "lucide-react"
6import {
7 AnimatePresence,
8 motion,
9 useAnimation,
10 type PanInfo,
11} from "motion/react"
12
13import { cn } from "@/lib/utils"
14import { AspectRatio } from "@/components/ui/aspect-ratio"
15import { Button } from "@/components/ui/button"
16import { Checkbox } from "@/components/ui/checkbox"
17import {
18 Dialog,
19 DialogContent,
20 DialogHeader,
21 DialogTitle,
22} from "@/components/ui/dialog"
23import {
24 Drawer,
25 DrawerContent,
26 DrawerHeader,
27 DrawerTitle,
28} from "@/components/ui/drawer"
29import { Progress } from "@/components/ui/progress"
30
31function useMediaQuery(query: string) {
32 const [matches, setMatches] = React.useState<boolean | null>(null)
33
34 React.useEffect(() => {
35 const media = window.matchMedia(query)
36 setMatches(media.matches)
37
38 const listener = (e: MediaQueryListEvent) => setMatches(e.matches)
39 media.addEventListener("change", listener)
40 return () => media.removeEventListener("change", listener)
41 }, [query])
42
43 return matches ?? false
44}
45
46function useFeatureVisibility(
47 featureId: string,
48 variant: "desktop" | "mobile"
49) {
50 const storageKey = `feature_${featureId}_${variant}`
51 const [isVisible, setIsVisible] = React.useState<boolean | null>(null)
52
53 React.useEffect(() => {
54 const storedValue = localStorage.getItem(storageKey)
55 setIsVisible(storedValue ? JSON.parse(storedValue) : true)
56 }, [storageKey])
57
58 const hideFeature = () => {
59 localStorage.setItem(storageKey, JSON.stringify(false))
60 setIsVisible(false)
61 }
62
63 return { isVisible: isVisible === null ? false : isVisible, hideFeature }
64}
65
66function useSwipe(onSwipe: (direction: "left" | "right") => void) {
67 const handleDragEnd = (
68 event: MouseEvent | TouchEvent | PointerEvent,
69 info: PanInfo
70 ) => {
71 if (info.offset.x > 100) {
72 onSwipe("right")
73 } else if (info.offset.x < -100) {
74 onSwipe("left")
75 }
76 }
77
78 return { handleDragEnd }
79}
80
81const fadeInScale = {
82 initial: { opacity: 0, scale: 0.95 },
83 animate: { opacity: 1, scale: 1 },
84 exit: { opacity: 0, scale: 0.95 },
85 transition: { duration: 0.2, ease: [0.23, 1, 0.32, 1] as const },
86}
87
88const slideInOut = (direction: 1 | -1) => ({
89 initial: { opacity: 0, x: 20 * direction },
90 animate: { opacity: 1, x: 0 },
91 exit: { opacity: 0, x: -20 * direction },
92 transition: { duration: 0.3, ease: [0.25, 0.1, 0.25, 1] as const },
93})
94
95const hoverScale = {
96 whileHover: { scale: 1.01 },
97 whileTap: { scale: 0.95 },
98 transition: { duration: 0.2 },
99}
100
101function StepPreview({ step, direction }: { step: Step; direction: 1 | -1 }) {
102 const controls = useAnimation()
103
104 React.useEffect(() => {
105 controls.start({
106 opacity: 1,
107 y: 0,
108 transition: { delay: 0.2, duration: 0.3 },
109 })
110 }, [controls, step])
111
112 return (
113 <motion.div
114 {...slideInOut(direction)}
115 className="relative h-full w-full overflow-hidden rounded-sm rounded-rb-lg rounded-tl-xl ring-2 ring-black/10 dark:ring-black/10 dark:ring-offset-black ring-offset-8"
116 >
117 {step.media ? (
118 <div className="relative bg-black h-full w-full">
119 <motion.div
120 initial={{ opacity: 0, y: 20 }}
121 animate={controls}
122 className="h-full w-full max-h-[700px]"
123 >
124 {step.media.type === "image" ? (
125 <Image
126 src={step.media.src || "/placeholder.svg"}
127 alt={step.media.alt || ""}
128 fill
129 className="object-cover"
130 />
131 ) : (
132 <video
133 src={step.media.src}
134 controls
135 className="h-full w-full object-cover"
136 />
137 )}
138 </motion.div>
139 <div className="absolute inset-0 bg-gradient-to-t from-black/80 to-transparent" />
140 <motion.div
141 initial={{ opacity: 0, y: 20 }}
142 animate={controls}
143 className="absolute bottom-0 left-0 right-0 p-6"
144 >
145 <h3 className="mb-2 text-2xl font-semibold text-white">
146 {step.title}
147 </h3>
148 <p className="text-white hidden md:block">
149 {step.full_description}
150 </p>
151 </motion.div>
152 </div>
153 ) : (
154 <div className="flex h-full items-center justify-center p-6">
155 <motion.div
156 initial={{ opacity: 0, y: 20 }}
157 animate={controls}
158 className="text-center"
159 >
160 <h3 className="mb-2 text-2xl font-semibold text-primary">
161 {step.title}
162 </h3>
163 <p className="text-muted-foreground">{step.full_description}</p>
164 </motion.div>
165 </div>
166 )}
167 </motion.div>
168 )
169}
170
171interface StepTabProps {
172 step: Step
173 isActive: boolean
174 onClick: () => void
175 isCompleted: boolean
176}
177
178function StepTab({ step, isActive, onClick, isCompleted }: StepTabProps) {
179 return (
180 <motion.button
181 {...hoverScale}
182 onClick={onClick}
183 className={cn(
184 "flex flex-col items-start rounded-lg px-4 py-2 text-left transition-colors w-full",
185 isActive ? "bg-muted border border-border" : "hover:bg-muted/70",
186 "relative"
187 )}
188 aria-current={isActive ? "step" : undefined}
189 aria-label={`${step.title}${isCompleted ? " (completed)" : ""}`}
190 >
191 <div className="mb-1 text-sm font-medium">{step.title}</div>
192 <div className="text-xs hidden md:block text-muted-foreground line-clamp-2">
193 {step.short_description}
194 </div>
195 {isCompleted && (
196 <motion.div {...fadeInScale} className="absolute right-2 top-2">
197 <div className="rounded-full bg-primary p-1">
198 <CheckIcon className="w-2 h-2 text-primary-foreground" />
199 </div>
200 </motion.div>
201 )}
202 </motion.button>
203 )
204}
205
206interface Step {
207 title: string
208 short_description: string
209 full_description: string
210 action?: {
211 label: string
212 onClick?: () => void
213 href?: string
214 }
215 media?: {
216 type: "image" | "video"
217 src: string
218 alt?: string
219 }
220}
221
222interface FeatureDisclosureProps {
223 steps: Step[]
224 featureId: string
225 onComplete?: () => void
226 onSkip?: () => void
227 showProgressBar?: boolean
228 open: boolean
229 setOpen: (open: boolean) => void
230 forceVariant?: "mobile" | "desktop"
231}
232
233interface StepContentProps {
234 steps: Step[]
235 currentStep: number
236 onSkip: () => void
237 onNext: () => void
238 onPrevious: () => void
239 hideFeature: () => void
240 completedSteps: number[]
241 onStepSelect: (index: number) => void
242 direction: 1 | -1
243 isDesktop: boolean
244}
245
246function StepContent({
247 steps,
248 currentStep,
249 onSkip,
250 onNext,
251 onPrevious,
252 hideFeature,
253 completedSteps,
254 onStepSelect,
255 direction,
256 isDesktop,
257 stepRef,
258}: StepContentProps & { stepRef: React.RefObject<HTMLButtonElement | null> }) {
259 const [skipNextTime, setSkipNextTime] = React.useState(false)
260
261 const renderActionButton = (action: Step["action"]) => {
262 if (!action) return null
263
264 if (action.href) {
265 return (
266 <Button asChild className="w-full " size="sm" variant="link">
267 <a href={action.href} target="_blank" rel="noopener noreferrer">
268 <span className="flex items-center gap-2">
269 {action.label}
270 <ExternalLinkIcon className="w-4 h-4" />
271 </span>
272 </a>
273 </Button>
274 )
275 }
276
277 return (
278 <Button
279 className="w-full rounded-full"
280 size="sm"
281 variant="secondary"
282 onClick={action.onClick}
283 >
284 {action.label}
285 </Button>
286 )
287 }
288
289 return (
290 <div className="flex h-full flex-col max-w-3xl mx-auto">
291 {isDesktop && (
292 <div className="flex-1 px-2 py-3">
293 <motion.div
294 initial={{ opacity: 0, y: 20 }}
295 animate={{ opacity: 1, y: 0 }}
296 transition={{ duration: 0.5, ease: [0.25, 0.1, 0.25, 1] }}
297 className="space-y-2 flex flex-col justify-center items-center px-1"
298 >
299 {steps.map((step, index) => (
300 <StepTab
301 key={index}
302 step={step}
303 isActive={currentStep === index}
304 onClick={() => onStepSelect(index)}
305 isCompleted={completedSteps.includes(index)}
306 />
307 ))}
308 </motion.div>
309 </div>
310 )}
311 <AnimatePresence mode="wait" initial={false}>
312 <motion.div
313 key={currentStep}
314 {...slideInOut(direction)}
315 className="mt-6 space-y-4 "
316 >
317 {!isDesktop && steps[currentStep]?.media && (
318 <AspectRatio
319 ratio={16 / 9}
320 className="lg:overflow-hidden rounded-lg bg-muted "
321 >
322 {steps[currentStep]?.media?.type === "image" ? (
323 <Image
324 src={steps[currentStep]?.media?.src || "/placeholder.svg"}
325 alt={steps[currentStep]?.media?.alt || ""}
326 fill
327 className="object-cover "
328 />
329 ) : (
330 <video
331 src={steps[currentStep]?.media?.src}
332 controls
333 className="h-full w-full object-cover"
334 />
335 )}
336 </AspectRatio>
337 )}
338
339 {steps[currentStep]?.action ? (
340 <div className=" px-2">
341 {renderActionButton(steps[currentStep]?.action)}
342 </div>
343 ) : (
344 <div className="h-10" />
345 )}
346
347 {/* Navigation buttons */}
348 <div className="flex items-center justify-between pr-4">
349 <Button
350 variant="ghost"
351 onClick={onSkip}
352 className="text-muted-foreground hover:bg-card rounded-full"
353 >
354 Skip all
355 </Button>
356 <div className="space-x-2">
357 {currentStep > 0 && (
358 <Button
359 onClick={onPrevious}
360 size="sm"
361 variant="ghost"
362 className="rounded-full hover:bg-transparent"
363 >
364 Previous
365 </Button>
366 )}
367 <Button
368 onClick={() => {
369 if (skipNextTime) {
370 hideFeature()
371 }
372 onNext()
373 }}
374 size="sm"
375 ref={stepRef}
376 className="rounded-full"
377 >
378 {currentStep === steps.length - 1 ? "Done" : "Next"}
379 </Button>
380 </div>
381 {/* Don't show again checkbox */}
382 </div>
383 <div className="flex items-center space-x-2 pb-4 px-4">
384 <Checkbox
385 id="skipNextTime"
386 checked={skipNextTime}
387 onCheckedChange={(checked) => setSkipNextTime(checked as boolean)}
388 />
389 <label
390 htmlFor="skipNextTime"
391 className="text-sm text-muted-foreground"
392 >
393 Don't show this again
394 </label>
395 </div>
396 </motion.div>
397 </AnimatePresence>
398 </div>
399 )
400}
401
402export function IntroDisclosure({
403 steps,
404 open,
405 setOpen,
406 featureId,
407 onComplete,
408 onSkip,
409 showProgressBar = true,
410 forceVariant,
411}: FeatureDisclosureProps) {
412 const [currentStep, setCurrentStep] = React.useState(0)
413 const [completedSteps, setCompletedSteps] = React.useState<number[]>([0])
414 const [direction, setDirection] = React.useState<1 | -1>(1)
415 const isDesktopQuery = useMediaQuery("(min-width: 768px)")
416 const isDesktop = forceVariant ? forceVariant === "desktop" : isDesktopQuery
417 const variant = isDesktop ? "desktop" : "mobile"
418 const { isVisible, hideFeature } = useFeatureVisibility(featureId, variant)
419 const stepRef = React.useRef<HTMLButtonElement>(null)
420
421 // Close the dialog if feature is hidden
422 React.useEffect(() => {
423 if (!isVisible) {
424 setOpen(false)
425 }
426 }, [isVisible, setOpen])
427
428 // Focus management
429 React.useEffect(() => {
430 if (open && stepRef.current) {
431 stepRef.current.focus()
432 }
433 }, [open, currentStep])
434
435 // Early return if feature should be hidden
436 if (!isVisible || !open) {
437 return null
438 }
439
440 const handleNext = () => {
441 setDirection(1)
442 setCompletedSteps((prev) =>
443 prev.includes(currentStep) ? prev : [...prev, currentStep]
444 )
445 if (currentStep < steps.length - 1) {
446 setCurrentStep(currentStep + 1)
447 } else {
448 setOpen(false)
449 onComplete?.()
450 }
451 }
452
453 const handlePrevious = () => {
454 setDirection(-1)
455 if (currentStep > 0) {
456 setCurrentStep(currentStep - 1)
457 }
458 }
459
460 const handleSkip = () => {
461 setOpen(false)
462 onSkip?.()
463 }
464
465 const handleStepSelect = (index: number) => {
466 setDirection(index > currentStep ? 1 : -1)
467 // Mark all steps up to and including the selected step as completed
468 setCompletedSteps((prev) => {
469 const newCompletedSteps = new Set(prev)
470 // If moving forward, mark all steps up to the selected one as completed
471 if (index > currentStep) {
472 for (let i = currentStep; i <= index; i++) {
473 newCompletedSteps.add(i)
474 }
475 }
476 return Array.from(newCompletedSteps)
477 })
478 setCurrentStep(index)
479 }
480
481 const handleSwipe = (swipeDirection: "left" | "right") => {
482 if (swipeDirection === "left") {
483 handleNext()
484 } else {
485 handlePrevious()
486 }
487 }
488
489 const handleKeyDown = (event: React.KeyboardEvent) => {
490 if (event.key === "ArrowRight" || event.key === "ArrowDown") {
491 handleNext()
492 } else if (event.key === "ArrowLeft" || event.key === "ArrowUp") {
493 handlePrevious()
494 }
495 }
496
497 const { handleDragEnd } = useSwipe(handleSwipe)
498
499 if (isDesktop) {
500 return (
501 <Dialog open={open} onOpenChange={setOpen}>
502 <DialogContent
503 className="max-w-[calc(100dvw-2rem)] p-0 gap-0 overflow-hidden sm:max-w-5xl"
504 onKeyDown={handleKeyDown}
505 >
506 <DialogHeader className="p-6 space-y-2 bg-muted border-b border-border">
507 <DialogTitle>Feature Tour</DialogTitle>
508 {showProgressBar && (
509 <div className="flex mt-2 w-full justify-center ">
510 <Progress
511 value={((currentStep + 1) / steps.length) * 100}
512 className=" h-1 "
513 />
514 </div>
515 )}
516 </DialogHeader>
517
518 <div className="grid grid-cols-2 h-full">
519 <div className="p-2 pr-[18px] ">
520 <StepContent
521 steps={steps}
522 currentStep={currentStep}
523 onSkip={handleSkip}
524 onNext={handleNext}
525 onPrevious={handlePrevious}
526 hideFeature={hideFeature}
527 completedSteps={completedSteps}
528 onStepSelect={handleStepSelect}
529 direction={direction}
530 isDesktop={isDesktop}
531 stepRef={stepRef}
532 />
533 </div>
534 <AnimatePresence mode="wait" initial={false}>
535 <StepPreview
536 key={currentStep}
537 step={steps[currentStep]}
538 direction={direction}
539 />
540 </AnimatePresence>
541 </div>
542 </DialogContent>
543 </Dialog>
544 )
545 }
546
547 return (
548 <Drawer open={open} onOpenChange={setOpen}>
549 <DrawerContent className="h-[95vh] max-h-[95vh] ">
550 <motion.div
551 drag="x"
552 dragConstraints={{ left: 0, right: 0 }}
553 onDragEnd={handleDragEnd}
554 onKeyDown={handleKeyDown}
555 className="h-full flex flex-col max-w-3xl mx-auto"
556 >
557 <DrawerHeader className="text-left pb-4 space-y-4">
558 {showProgressBar && (
559 <Progress
560 value={((currentStep + 1) / steps.length) * 100}
561 className="mb-4"
562 />
563 )}
564 <DrawerTitle>{steps[currentStep]?.title}</DrawerTitle>
565 </DrawerHeader>
566
567 <div className="flex-1 overflow-y-auto">
568 <div className="p-4 space-y-4 pb-32">
569 {/* Step tabs */}
570 <div className="grid grid-cols-2 gap-2 mb-6">
571 {steps.map((step, index) => (
572 <StepTab
573 key={index}
574 step={step}
575 isActive={currentStep === index}
576 onClick={() => handleStepSelect(index)}
577 isCompleted={completedSteps.includes(index)}
578 />
579 ))}
580 </div>
581 {/* Preview */}
582 <div className="relative aspect-[16/9] ring-2 ring-border ring-offset-8 ring-offset-background rounded-lg overflow-hidden">
583 <StepPreview step={steps[currentStep]} direction={direction} />
584 </div>
585
586 {/* Step content */}
587 <div className="space-y-4 border border-border p-3 rounded-lg">
588 <p className="text-muted-foreground">
589 {steps[currentStep]?.short_description}
590 </p>
591 {steps[currentStep]?.action && (
592 <Button
593 asChild
594 className="w-full"
595 variant={
596 steps[currentStep]?.action?.href ? "outline" : "default"
597 }
598 >
599 {steps[currentStep]?.action?.href ? (
600 <a
601 href={steps[currentStep]?.action?.href}
602 target="_blank"
603 rel="noopener noreferrer"
604 className="flex items-center justify-center gap-2"
605 >
606 {steps[currentStep]?.action?.label}
607 <ExternalLinkIcon className="h-4 w-4" />
608 </a>
609 ) : (
610 <button onClick={steps[currentStep]?.action?.onClick}>
611 {steps[currentStep]?.action?.label}
612 </button>
613 )}
614 </Button>
615 )}
616 </div>
617 </div>
618 </div>
619
620 {/* Fixed bottom navigation */}
621 <div className="absolute bottom-0 left-0 right-0 border-t bg-background">
622 <div className="p-4">
623 <div className="flex items-center justify-between mb-4">
624 <Button
625 variant="ghost"
626 onClick={onSkip}
627 className="text-muted-foreground hover:bg-card rounded-full"
628 >
629 Skip all
630 </Button>
631 <div className="space-x-2">
632 {currentStep > 0 && (
633 <Button
634 onClick={handlePrevious}
635 size="sm"
636 variant="ghost"
637 className="rounded-full hover:bg-transparent"
638 >
639 Previous
640 </Button>
641 )}
642 <Button
643 onClick={() => {
644 handleNext()
645 }}
646 size="sm"
647 ref={stepRef}
648 className="rounded-full"
649 >
650 {currentStep === steps.length - 1 ? "Done" : "Next"}
651 </Button>
652 </div>
653 </div>
654 <div className="flex items-center space-x-2">
655 <Checkbox
656 id="skipNextTime"
657 onCheckedChange={(checked) => {
658 hideFeature()
659 }}
660 />
661 <label
662 htmlFor="skipNextTime"
663 className="text-sm text-muted-foreground"
664 >
665 Don't show this again
666 </label>
667 </div>
668 </div>
669 </div>
670 </motion.div>
671 </DrawerContent>
672 </Drawer>
673 )
674}
675
676export default IntroDisclosure
677