1"use client"
2
3import { useEffect, useState } from "react"
4import { motion } from "motion/react"
5
6import { cn } from "@/lib/utils"
7
8const generateRandomDigits = () => {
9 return Array.from({ length: 6 }, () =>
10 Math.floor(Math.random() * 10).toString()
11 )
12}
13
14type AnimatedOTPProps = {
15 delay?: number
16 cardTitle?: string
17 cardDescription?: string
18 whileHover?: boolean
19}
20
21const AnimatedOTP = ({
22 delay = 3500,
23 cardTitle = "Multifactor Authentication",
24 cardDescription = "Each user's self-serve multifactor settings are enforced automatically during sign-in.",
25 whileHover = false,
26}: AnimatedOTPProps) => {
27 const [animationKey, setAnimationKey] = useState(0)
28 const delayTime = Math.max(delay, 3500)
29 useEffect(() => {
30 const interval = setInterval(() => {
31 setAnimationKey((prev) => prev + 1)
32 }, delayTime)
33
34 return () => clearInterval(interval)
35 }, [delayTime])
36
37 return (
38 <OTPinput
39 key={animationKey}
40 cardTitle={cardTitle}
41 cardDescription={cardDescription}
42 whileHover={whileHover}
43 />
44 )
45}
46
47export default AnimatedOTP
48
49const OTPinput = ({
50 cardTitle,
51 cardDescription,
52 whileHover,
53}: AnimatedOTPProps) => {
54 const [activeIndex, setActiveIndex] = useState(0)
55 const [fadeOut, setFadeOut] = useState(false)
56 const [digits] = useState(() => generateRandomDigits())
57 const [isCardHovered, setIsCardHovered] = useState(false)
58
59 useEffect(() => {
60 if (activeIndex > digits.length - 1) return
61
62 const shouldAnimate = !whileHover || (whileHover && isCardHovered)
63
64 if (!shouldAnimate) return
65
66 const interval = setInterval(() => {
67 setActiveIndex((prev) => prev + 1)
68 }, 400)
69
70 if (activeIndex === digits.length - 1) {
71 setTimeout(() => {
72 setFadeOut(true)
73 }, 450)
74 }
75
76 return () => clearInterval(interval)
77 }, [activeIndex, digits.length, whileHover, isCardHovered])
78
79 return (
80 <motion.div
81 initial={{ opacity: 1 }}
82 onHoverStart={() => setIsCardHovered(true)}
83 onHoverEnd={() => setIsCardHovered(false)}
84 className={cn(
85 "relative",
86 "flex items-center justify-center",
87 "h-[14rem] w-full max-w-[350px]",
88 "rounded-md border bg-neutral-50 dark:bg-neutral-900",
89 "shadow-[0_3px_10px_rgb(0,0,0,0.2)]"
90 )}
91 >
92 <div className="absolute top-[25%] left-1/2 -translate-x-1/2">
93 <div className="flex w-full items-center justify-center gap-3">
94 {digits.map((digit, idx) => (
95 <div
96 key={idx}
97 className={cn(
98 "text-primary relative flex h-10 w-8 cursor-default items-center justify-center rounded-md bg-gradient-to-br from-neutral-100 to-neutral-50 dark:from-neutral-800 dark:to-neutral-800",
99 "shadow-[0_3px_10px_rgb(0,0,0,0.2)]"
100 )}
101 >
102 {(!whileHover || (whileHover && isCardHovered)) && (
103 <motion.div
104 className="absolute inset-0 rounded-md border border-cyan-400"
105 initial={{ opacity: 0 }}
106 animate={{ opacity: [0, 1, 0] }}
107 transition={{
108 duration: 0.5,
109 ease: "easeInOut",
110 delay: 2.25,
111 }}
112 style={{
113 boxShadow: "inset 0 0 12px rgba(34, 211, 238, 0.5)",
114 }}
115 />
116 )}
117 {activeIndex === idx &&
118 (!whileHover || (whileHover && isCardHovered)) && (
119 <motion.div
120 key={idx}
121 className="absolute inset-0 rounded-md border border-cyan-400"
122 initial={{ opacity: 0 }}
123 animate={{ opacity: 1 }}
124 transition={{
125 duration: 0.3,
126 ease: "easeInOut",
127 }}
128 style={{
129 boxShadow: "inset 0 0 12px rgba(34, 211, 238, 0.6)",
130 }}
131 >
132 <svg
133 viewBox="0 0 20 20"
134 className="absolute inset-0 h-full w-full"
135 strokeWidth="0.4"
136 >
137 <path
138 d="M 3 19 h 14"
139 className="stroke-cyan-400 dark:stroke-cyan-500"
140 />
141 </svg>
142 </motion.div>
143 )}
144 <motion.span
145 initial={{ opacity: 0 }}
146 animate={{
147 opacity: whileHover
148 ? isCardHovered
149 ? fadeOut
150 ? 0
151 : 1
152 : 0
153 : fadeOut
154 ? 0
155 : 1,
156 }}
157 transition={{
158 duration: fadeOut ? 0.1 : 0.2,
159 ease: "easeInOut",
160 delay: fadeOut ? 0 : idx * 0.43,
161 }}
162 className="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2"
163 >
164 {digit}
165 </motion.span>
166 </div>
167 ))}
168 </div>
169 </div>
170 <div className="absolute bottom-4 left-0 w-full px-3">
171 <h3 className="text-primary text-sm font-semibold">{cardTitle}</h3>
172 <p className="mt-2 text-xs text-neutral-600 dark:text-neutral-400">
173 {cardDescription}
174 </p>
175 </div>
176 </motion.div>
177 )
178}
179