'use client' import { useState, useEffect, forwardRef, useImperativeHandle, useCallback } from 'react' import { getNewCaptcha, CaptchaResponse } from '@/app/actions/captcha' import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Loader2, RefreshCw } from "lucide-react" interface CaptchaProps { onVerify: (hash: string, value: string) => void } export interface CaptchaRef { reset: () => void } export const Captcha = forwardRef(({ onVerify }, ref) => { const [captcha, setCaptcha] = useState(null) const [input, setInput] = useState("") const [loading, setLoading] = useState(true) const fetchCaptcha = useCallback(async () => { setLoading(true) try { const data = await getNewCaptcha() setCaptcha(data) setInput("") onVerify(data.hash, "") // Reset parent state } catch (error) { console.error("Captcha fetch failed", error) } finally { setLoading(false) } }, [onVerify]) useEffect(() => { fetchCaptcha() }, [fetchCaptcha]) useImperativeHandle(ref, () => ({ reset: fetchCaptcha })) const handleChange = (e: React.ChangeEvent) => { const value = e.target.value setInput(value) if (captcha) { onVerify(captcha.hash, value) } } return (
{loading && (
)}
) }) Captcha.displayName = "Captcha"