import React, { useEffect, useRef, useState } from 'react'; import { useI18n } from '../application/i18n/I18nProvider'; import type { AppLockReason, AppLockSystemUnlockResult, AppLockSystemUnlockStatus, AppLockUnlockResult, } from '../application/state/useAppLockState'; import { AppLogo } from './AppLogo'; import { Button } from './ui/button'; import { Input } from './ui/input'; import { Label } from './ui/label'; const RESET_REVEAL_CLICK_COUNT = 5; const RESET_REVEAL_WINDOW_MS = 1500; interface AppLockOverlayProps { locked: boolean; reason: AppLockReason | null; onUnlock: (password: string) => Promise; systemUnlockStatus?: AppLockSystemUnlockStatus; onSystemUnlock?: () => Promise; onResetAppLock: (currentPassword: string) => Promise; } export function getAppLockReasonMessageKey(reason: AppLockReason | null): string { switch (reason) { case 'startup': return 'appLock.reason.startup'; case 'idle': return 'appLock.reason.idle'; case 'manual': return 'appLock.reason.manual'; default: return 'appLock.reason.default'; } } export function getAppLockErrorMessageKey(error: AppLockUnlockResult['error'] | null): string | null { switch (error) { case 'empty': return 'appLock.error.emptyPassword'; case 'incorrect': return 'appLock.error.incorrectPassword'; default: return null; } } export const AppLockOverlay: React.FC = ({ locked, reason, onUnlock, systemUnlockStatus, onSystemUnlock, onResetAppLock, }) => { const { t } = useI18n(); const passwordRef = useRef(null); const [password, setPassword] = useState(''); const [error, setError] = useState(null); const [isSubmitting, setIsSubmitting] = useState(false); const [isSystemUnlocking, setIsSystemUnlocking] = useState(false); const [systemUnlockError, setSystemUnlockError] = useState(false); const [logoClickCount, setLogoClickCount] = useState(0); const [lastLogoClickAt, setLastLogoClickAt] = useState(null); const [showReset, setShowReset] = useState(false); const [isResetting, setIsResetting] = useState(false); const [resetError, setResetError] = useState(false); useEffect(() => { if (!locked) { setPassword(''); setError(null); setIsSubmitting(false); setIsSystemUnlocking(false); setSystemUnlockError(false); setLogoClickCount(0); setLastLogoClickAt(null); setShowReset(false); setIsResetting(false); setResetError(false); return; } const timeout = window.setTimeout(() => passwordRef.current?.focus(), 0); return () => window.clearTimeout(timeout); }, [locked]); if (!locked) return null; const errorKey = getAppLockErrorMessageKey(error); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); if (isSubmitting) return; setIsSubmitting(true); setError(null); const result = await onUnlock(password); if (!result.ok) { setError(result.error); setIsSubmitting(false); return; } setPassword(''); setIsSubmitting(false); }; const handleSystemUnlock = async () => { if (isSystemUnlocking || !onSystemUnlock) return; setIsSystemUnlocking(true); setSystemUnlockError(false); const result = await onSystemUnlock(); if (!result.ok) { setSystemUnlockError(true); } setIsSystemUnlocking(false); }; const handleLogoClick = () => { const now = Date.now(); setLogoClickCount((current) => { const isQuickSequence = lastLogoClickAt !== null && now - lastLogoClickAt <= RESET_REVEAL_WINDOW_MS; const nextCount = isQuickSequence ? current + 1 : 1; if (nextCount >= RESET_REVEAL_CLICK_COUNT) { setShowReset(true); } return nextCount; }); setLastLogoClickAt(now); }; const handleReset = async () => { if (isResetting) return; setIsResetting(true); setResetError(false); try { await onResetAppLock(password); } catch { setResetError(true); setIsResetting(false); } }; return (

{t('appLock.title')}

{t(getAppLockReasonMessageKey(reason))}

{ setPassword(event.target.value); if (error) setError(null); }} /> {errorKey && (

{t(errorKey)}

)}
{systemUnlockStatus?.enabled && systemUnlockStatus.available && systemUnlockStatus.label && (
{systemUnlockError && (

{t('appLock.systemUnlock.error')}

)}
)} {showReset && (

{t('appLock.reset.title')}

{t('appLock.reset.description')}

{resetError && (

{t('appLock.reset.error')}

)}
)}
); };