import { Form } from '@inertiajs/react'; import { Eye, EyeOff, LockKeyhole, RefreshCw } from 'lucide-react'; import { useCallback, useEffect, useRef, useState } from 'react'; import AlertError from '@/components/alert-error'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card'; import { regenerateRecoveryCodes } from '@/routes/two-factor'; type Props = { recoveryCodesList: string[]; fetchRecoveryCodes: () => Promise; errors: string[]; }; export default function TwoFactorRecoveryCodes({ recoveryCodesList, fetchRecoveryCodes, errors, }: Props) { const [codesAreVisible, setCodesAreVisible] = useState(false); const codesSectionRef = useRef(null); const canRegenerateCodes = recoveryCodesList.length > 0 && codesAreVisible; const toggleCodesVisibility = useCallback(async () => { if (!codesAreVisible && !recoveryCodesList.length) { await fetchRecoveryCodes(); } setCodesAreVisible(!codesAreVisible); if (!codesAreVisible) { setTimeout(() => { codesSectionRef.current?.scrollIntoView({ behavior: 'smooth', block: 'nearest', }); }); } }, [codesAreVisible, recoveryCodesList.length, fetchRecoveryCodes]); useEffect(() => { if (!recoveryCodesList.length) { void fetchRecoveryCodes(); } }, [recoveryCodesList.length, fetchRecoveryCodes]); const RecoveryCodeIconComponent = codesAreVisible ? EyeOff : Eye; return ( {' '} Os códigos de recuperação permitem recuperar o acesso se perder o dispositivo autenticador. Guarde-os num gestor de palavras-passe seguro.{' '}
{canRegenerateCodes && (
{({ processing }) => ( )}
)}
{errors?.length ? ( ) : ( <>
{recoveryCodesList.length ? ( recoveryCodesList.map((code, index) => (
{code}
)) ) : (
{Array.from( { length: 8 }, (_, index) => ( )}

{' '} Cada código permite uma única entrada e deixa de ser válido após a utilização. Para obter novos códigos, seleccione{' '} {' '} Gerar novos códigos{' '} {' '} acima.{' '}

)}
); }