/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import { useState } from 'react'
import { Loader2 } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
import { useCountdown } from '@/hooks/use-countdown'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Dialog } from '@/components/dialog'
import { sendEmailVerification, bindEmail } from '../../api'
// ============================================================================
// Email Bind Dialog Component
// ============================================================================
interface EmailBindDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
currentEmail?: string
onSuccess: () => void
}
export function EmailBindDialog({
open,
onOpenChange,
currentEmail,
onSuccess,
}: EmailBindDialogProps) {
const { t } = useTranslation()
const [loading, setLoading] = useState(false)
const [sendingCode, setSendingCode] = useState(false)
const [email, setEmail] = useState('')
const [code, setCode] = useState('')
const {
secondsLeft,
isActive,
start: startCountdown,
reset: resetCountdown,
} = useCountdown({
initialSeconds: 60,
})
const handleSendCode = async () => {
if (!email || !email.includes('@')) {
toast.error(t('Please enter a valid email address'))
return
}
try {
setSendingCode(true)
const response = await sendEmailVerification(email)
if (response.success) {
toast.success(t('Verification code sent! Please check your email.'))
startCountdown()
} else {
toast.error(response.message || t('Failed to send verification code'))
}
} catch (_error) {
toast.error(t('Failed to send verification code'))
} finally {
setSendingCode(false)
}
}
const handleBind = async () => {
if (!email || !code) {
toast.error(t('Please enter email and verification code'))
return
}
try {
setLoading(true)
const response = await bindEmail(email, code)
if (response.success) {
toast.success(t('Email bound successfully!'))
onOpenChange(false)
onSuccess()
// Reset form
setEmail('')
setCode('')
resetCountdown()
} else {
toast.error(response.message || t('Failed to bind email'))
}
} catch (_error) {
toast.error(t('Failed to bind email'))
} finally {
setLoading(false)
}
}
const handleOpenChange = (open: boolean) => {
if (!loading) {
onOpenChange(open)
if (!open) {
// Reset form when closing
setEmail('')
setCode('')
resetCountdown()
}
}
}
return (
)
}