import { loadDevicePrivateKey } from '@chat-app/shared/auth'; import { useCallback, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useAuth } from '../context/AuthContext'; import { devLocalSecretStore } from '../lib/secretStore'; import { BackupExportDialog } from './BackupExportDialog'; import { ShieldIcon, XIcon } from './icons'; const DISMISS_KEY = 'chatapp.backup.prompt.dismissed'; const SESSION_KEY = 'chatapp.backup.prompt'; // Post-registration nudge: right after a fresh device provision we set // `chatapp.backup.prompt` in sessionStorage. This component reads it and // shows a floating "mach jetzt ein Backup" banner until the user either // creates one or explicitly dismisses (persisted in localStorage so we stop // nagging across reloads). export function BackupPromptBanner() { const { t } = useTranslation(['app']); const { profile, device } = useAuth(); const [visible, setVisible] = useState(false); const [dialogOpen, setDialogOpen] = useState(false); const [privateKey, setPrivateKey] = useState(null); useEffect(() => { try { if (window.localStorage.getItem(DISMISS_KEY) === '1') return; if (window.sessionStorage.getItem(SESSION_KEY) !== '1') return; setVisible(true); } catch { /* storage unavailable */ } }, []); const dismiss = useCallback((persist: boolean) => { setVisible(false); try { window.sessionStorage.removeItem(SESSION_KEY); if (persist) window.localStorage.setItem(DISMISS_KEY, '1'); } catch { /* ignore */ } }, []); const openDialog = useCallback(async () => { if (!profile?.userId || !device?.id) return; const priv = await loadDevicePrivateKey(devLocalSecretStore, profile.userId, device.id); if (!priv) return; setPrivateKey(priv); setDialogOpen(true); }, [profile, device]); const closeDialog = useCallback(() => { setDialogOpen(false); if (privateKey) { for (let i = 0; i < privateKey.length; i++) privateKey[i] = 0; } setPrivateKey(null); // After the user interacts with the dialog, drop the banner regardless // of whether they actually completed the backup — they're aware now. dismiss(true); }, [privateKey, dismiss]); if (!visible) return null; return ( <>

{t('app:backup.prompt_title', { defaultValue: 'Erstelle jetzt ein Geräte-Backup' })}

{t('app:backup.prompt_body', { defaultValue: 'Ohne Backup verlierst du Zugriff auf alte Nachrichten, wenn Browser oder Gerät ihren Speicher verlieren. Dauert 10 Sekunden.', })}

{dialogOpen && profile?.userId && device?.id && privateKey && ( )} ); }