/** * PGP settings — import private key, view contact public keys. */ import React, { useState, useEffect, useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; import { Card } from '@/components/ui/Card'; import { EmptyState } from '@/components/ui/EmptyState'; import { useToast } from '@/components/ui/Toast'; import { Loader2 } from 'lucide-react'; import { importPgpKey, fetchPgpKeys, fetchContactPgpKeys, storeContactPgpKey, type PgpKey, type ContactPgpKey, } from '@/api/mail'; export function PgpSettings() { const { t } = useTranslation(); const toast = useToast(); const [keys, setKeys] = useState([]); const [contactKeys, setContactKeys] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [privateKey, setPrivateKey] = useState(''); const [passphrase, setPassphrase] = useState(''); const [importing, setImporting] = useState(false); const [contactId, setContactId] = useState(''); const [contactPublicKey, setContactPublicKey] = useState(''); const [storingContact, setStoringContact] = useState(false); const load = useCallback(() => { setLoading(true); Promise.all([fetchPgpKeys(), fetchContactPgpKeys()]) .then(([k, ck]) => { setKeys(k); setContactKeys(ck); setError(null); }) .catch((err) => { setError(err instanceof Error ? err.message : String(err)); }) .finally(() => setLoading(false)); }, []); useEffect(() => { load(); }, [load]); const handleImport = useCallback(async () => { if (!privateKey.trim()) return; setImporting(true); try { const result = await importPgpKey({ private_key: privateKey, passphrase: passphrase || undefined }); setKeys((prev) => [...prev, result]); toast.success(t('mail.pgpKeyImported')); setPrivateKey(''); setPassphrase(''); } catch (err) { toast.error(err instanceof Error ? err.message : String(err)); } finally { setImporting(false); } }, [privateKey, passphrase, toast, t]); const handleStoreContactKey = useCallback(async () => { if (!contactId.trim() || !contactPublicKey.trim()) return; setStoringContact(true); try { await storeContactPgpKey(contactId, { public_key: contactPublicKey }); toast.success(t('mail.contactPgpStored')); setContactId(''); setContactPublicKey(''); load(); } catch (err) { toast.error(err instanceof Error ? err.message : String(err)); } finally { setStoringContact(false); } }, [contactId, contactPublicKey, toast, t, load]); if (loading) { return (
); } if (error) { return (

{error}

); } return (