'use client'; import { useEffect, useState } from 'react'; import { AlertTriangle } from 'lucide-react'; import { ONE_DAY, ONE_SECOND } from '@documenso/lib/constants/time'; import { trpc } from '@documenso/trpc/react'; import { Button } from '@documenso/ui/primitives/button'; import { Dialog, DialogContent, DialogDescription, DialogTitle, } from '@documenso/ui/primitives/dialog'; import { useToast } from '@documenso/ui/primitives/use-toast'; export type VerifyEmailBannerProps = { email: string; }; const RESEND_CONFIRMATION_EMAIL_TIMEOUT = 20 * ONE_SECOND; export const VerifyEmailBanner = ({ email }: VerifyEmailBannerProps) => { const { toast } = useToast(); const [isOpen, setIsOpen] = useState(false); const [isButtonDisabled, setIsButtonDisabled] = useState(false); const { mutateAsync: sendConfirmationEmail, isLoading } = trpc.profile.sendConfirmationEmail.useMutation(); const onResendConfirmationEmail = async () => { try { setIsButtonDisabled(true); await sendConfirmationEmail({ email: email }); toast({ title: 'Success', description: 'Verification email sent successfully.', }); setIsOpen(false); setTimeout(() => setIsButtonDisabled(false), RESEND_CONFIRMATION_EMAIL_TIMEOUT); } catch (err) { setIsButtonDisabled(false); toast({ title: 'Error', description: 'Something went wrong while sending the confirmation email.', variant: 'destructive', }); } }; useEffect(() => { // Check localStorage to see if we've recently automatically displayed the dialog // if it was within the past 24 hours, don't show it again // otherwise, show it again and update the localStorage timestamp const emailVerificationDialogLastShown = localStorage.getItem( 'emailVerificationDialogLastShown', ); if (emailVerificationDialogLastShown) { const lastShownTimestamp = parseInt(emailVerificationDialogLastShown); if (Date.now() - lastShownTimestamp < ONE_DAY) { return; } } setIsOpen(true); localStorage.setItem('emailVerificationDialogLastShown', Date.now().toString()); }, []); return ( <>