import { useRef, useState, type ReactElement } from 'react'; import { useVault } from '../contexts/vault-context.js'; import { ApiError } from '../lib/api.js'; export function VaultUnlock(): ReactElement { const vault = useVault(); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [uploadError, setUploadError] = useState(null); const fileInputRef = useRef(null); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); await vault.unlock(password); setLoading(false); } async function handleFileChange(e: React.ChangeEvent) { const file = e.target.files?.[0]; if (!file) return; setUploadError(null); try { await vault.upload(file); } catch (err) { if (err instanceof ApiError && err.status === 409) { // 409: vault exists, ask to overwrite const confirmed = window.confirm( 'A vault already exists. Replace it with the uploaded file?', ); if (confirmed) { try { await vault.upload(file, true); } catch { setUploadError('Failed to replace vault. Please try again.'); } } } } // Reset so the same file can be re-selected if (fileInputRef.current) fileInputRef.current.value = ''; } return (

Unlock Vault

setPassword(e.target.value)} disabled={loading} autoFocus />
{vault.error &&

{vault.error}

}

{uploadError &&

{uploadError}

}
); }