|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState, useRef, useEffect } from 'react'; |
| 4 | +import { motion, AnimatePresence } from 'motion/react'; |
| 5 | +import { useI18n } from '@/lib/hooks/use-i18n'; |
| 6 | +import { ArrowRight, ShieldCheck, LoaderCircle } from 'lucide-react'; |
| 7 | + |
| 8 | +interface AccessCodeModalProps { |
| 9 | + open: boolean; |
| 10 | + onSuccess: () => void; |
| 11 | +} |
| 12 | + |
| 13 | +export function AccessCodeModal({ open, onSuccess }: AccessCodeModalProps) { |
| 14 | + const { t } = useI18n(); |
| 15 | + const [code, setCode] = useState(''); |
| 16 | + const [error, setError] = useState(''); |
| 17 | + const [loading, setLoading] = useState(false); |
| 18 | + const [success, setSuccess] = useState(false); |
| 19 | + const inputRef = useRef<HTMLInputElement>(null); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + if (open && inputRef.current) { |
| 23 | + inputRef.current.focus(); |
| 24 | + } |
| 25 | + }, [open]); |
| 26 | + |
| 27 | + async function handleSubmit(e: React.FormEvent) { |
| 28 | + e.preventDefault(); |
| 29 | + if (!code || loading) return; |
| 30 | + setError(''); |
| 31 | + setLoading(true); |
| 32 | + |
| 33 | + try { |
| 34 | + const res = await fetch('/api/access-code/verify', { |
| 35 | + method: 'POST', |
| 36 | + headers: { 'Content-Type': 'application/json' }, |
| 37 | + body: JSON.stringify({ code }), |
| 38 | + }); |
| 39 | + |
| 40 | + if (res.ok) { |
| 41 | + setSuccess(true); |
| 42 | + setTimeout(onSuccess, 600); |
| 43 | + } else { |
| 44 | + setError(t('accessCode.error')); |
| 45 | + setCode(''); |
| 46 | + inputRef.current?.focus(); |
| 47 | + } |
| 48 | + } catch { |
| 49 | + setError(t('accessCode.error')); |
| 50 | + } finally { |
| 51 | + setLoading(false); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return ( |
| 56 | + <AnimatePresence> |
| 57 | + {open && ( |
| 58 | + <motion.div |
| 59 | + className="fixed inset-0 z-[200] flex items-center justify-center overflow-hidden" |
| 60 | + initial={{ opacity: 0 }} |
| 61 | + animate={{ opacity: 1 }} |
| 62 | + exit={{ opacity: 0, transition: { duration: 0.3 } }} |
| 63 | + > |
| 64 | + {/* Background — subtle mesh gradient */} |
| 65 | + <div className="absolute inset-0 bg-background"> |
| 66 | + <div |
| 67 | + className="absolute inset-0 opacity-30 dark:opacity-20" |
| 68 | + style={{ |
| 69 | + backgroundImage: ` |
| 70 | + radial-gradient(ellipse 80% 60% at 20% 40%, var(--primary) 0%, transparent 60%), |
| 71 | + radial-gradient(ellipse 60% 80% at 80% 20%, oklch(0.6 0.15 280) 0%, transparent 50%), |
| 72 | + radial-gradient(ellipse 50% 50% at 60% 80%, oklch(0.5 0.12 300) 0%, transparent 50%) |
| 73 | + `, |
| 74 | + }} |
| 75 | + /> |
| 76 | + {/* Subtle noise texture */} |
| 77 | + <div |
| 78 | + className="absolute inset-0 opacity-[0.03] dark:opacity-[0.05]" |
| 79 | + style={{ |
| 80 | + backgroundImage: `url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E")`, |
| 81 | + backgroundSize: '128px 128px', |
| 82 | + }} |
| 83 | + /> |
| 84 | + </div> |
| 85 | + |
| 86 | + {/* Content card */} |
| 87 | + <motion.div |
| 88 | + className="relative z-10 w-full max-w-sm mx-4" |
| 89 | + initial={{ opacity: 0, y: 20, scale: 0.96 }} |
| 90 | + animate={{ opacity: 1, y: 0, scale: 1 }} |
| 91 | + exit={{ opacity: 0, y: -20, scale: 0.96 }} |
| 92 | + transition={{ duration: 0.4, ease: [0.16, 1, 0.3, 1] }} |
| 93 | + > |
| 94 | + <div className="rounded-2xl border border-border/50 bg-card/80 p-8 shadow-xl shadow-black/5 backdrop-blur-xl dark:bg-card/60 dark:shadow-black/20"> |
| 95 | + {/* Icon */} |
| 96 | + <motion.div |
| 97 | + className="mx-auto mb-6 flex h-14 w-14 items-center justify-center rounded-full bg-primary/10" |
| 98 | + initial={{ scale: 0.5, opacity: 0 }} |
| 99 | + animate={{ scale: 1, opacity: 1 }} |
| 100 | + transition={{ delay: 0.15, duration: 0.4, ease: [0.16, 1, 0.3, 1] }} |
| 101 | + > |
| 102 | + <ShieldCheck className="h-7 w-7 text-primary" strokeWidth={1.5} /> |
| 103 | + </motion.div> |
| 104 | + |
| 105 | + {/* Title */} |
| 106 | + <motion.h1 |
| 107 | + className="mb-1 text-center text-lg font-semibold tracking-tight text-foreground" |
| 108 | + initial={{ opacity: 0, y: 8 }} |
| 109 | + animate={{ opacity: 1, y: 0 }} |
| 110 | + transition={{ delay: 0.2, duration: 0.4 }} |
| 111 | + > |
| 112 | + {t('accessCode.title')} |
| 113 | + </motion.h1> |
| 114 | + |
| 115 | + <motion.p |
| 116 | + className="mb-6 text-center text-sm text-muted-foreground" |
| 117 | + initial={{ opacity: 0 }} |
| 118 | + animate={{ opacity: 1 }} |
| 119 | + transition={{ delay: 0.25, duration: 0.4 }} |
| 120 | + > |
| 121 | + OpenMAIC |
| 122 | + </motion.p> |
| 123 | + |
| 124 | + {/* Form */} |
| 125 | + <motion.form |
| 126 | + onSubmit={handleSubmit} |
| 127 | + className="space-y-4" |
| 128 | + initial={{ opacity: 0, y: 8 }} |
| 129 | + animate={{ opacity: 1, y: 0 }} |
| 130 | + transition={{ delay: 0.3, duration: 0.4 }} |
| 131 | + > |
| 132 | + <div className="relative"> |
| 133 | + <input |
| 134 | + ref={inputRef} |
| 135 | + type="password" |
| 136 | + placeholder={t('accessCode.placeholder')} |
| 137 | + value={code} |
| 138 | + onChange={(e) => { |
| 139 | + setCode(e.target.value); |
| 140 | + if (error) setError(''); |
| 141 | + }} |
| 142 | + className={` |
| 143 | + w-full rounded-xl border bg-background/60 px-4 py-3 pr-12 text-sm |
| 144 | + outline-none transition-all duration-200 |
| 145 | + placeholder:text-muted-foreground/50 |
| 146 | + focus:border-primary/40 focus:ring-2 focus:ring-primary/10 |
| 147 | + ${error ? 'border-destructive/50 focus:border-destructive/50 focus:ring-destructive/10' : 'border-border/60'} |
| 148 | + `} |
| 149 | + disabled={loading || success} |
| 150 | + autoComplete="off" |
| 151 | + /> |
| 152 | + <button |
| 153 | + type="submit" |
| 154 | + disabled={!code || loading || success} |
| 155 | + className={` |
| 156 | + absolute right-2 top-1/2 -translate-y-1/2 flex h-8 w-8 items-center |
| 157 | + justify-center rounded-lg transition-all duration-200 |
| 158 | + ${code && !loading && !success ? 'bg-primary text-primary-foreground hover:opacity-90 cursor-pointer' : 'text-muted-foreground/30 cursor-default'} |
| 159 | + `} |
| 160 | + > |
| 161 | + {loading ? ( |
| 162 | + <LoaderCircle className="h-4 w-4 animate-spin" /> |
| 163 | + ) : success ? ( |
| 164 | + <motion.div |
| 165 | + initial={{ scale: 0 }} |
| 166 | + animate={{ scale: 1 }} |
| 167 | + transition={{ type: 'spring', stiffness: 300, damping: 20 }} |
| 168 | + > |
| 169 | + <ShieldCheck className="h-4 w-4 text-emerald-500" /> |
| 170 | + </motion.div> |
| 171 | + ) : ( |
| 172 | + <ArrowRight className="h-4 w-4" /> |
| 173 | + )} |
| 174 | + </button> |
| 175 | + </div> |
| 176 | + |
| 177 | + {/* Error message */} |
| 178 | + <AnimatePresence mode="wait"> |
| 179 | + {error && ( |
| 180 | + <motion.p |
| 181 | + className="text-center text-sm text-destructive" |
| 182 | + initial={{ opacity: 0, y: -4, height: 0 }} |
| 183 | + animate={{ opacity: 1, y: 0, height: 'auto' }} |
| 184 | + exit={{ opacity: 0, y: -4, height: 0 }} |
| 185 | + transition={{ duration: 0.2 }} |
| 186 | + > |
| 187 | + {error} |
| 188 | + </motion.p> |
| 189 | + )} |
| 190 | + </AnimatePresence> |
| 191 | + </motion.form> |
| 192 | + </div> |
| 193 | + </motion.div> |
| 194 | + </motion.div> |
| 195 | + )} |
| 196 | + </AnimatePresence> |
| 197 | + ); |
| 198 | +} |
0 commit comments