import Avatar from './social/Avatar' import { useSocial } from './social/SocialProvider' import { t } from '../i18n' import { AlertTriangle, Check, CloudOff, KeyRound, Loader2, LogOut, RefreshCw, UserRound } from 'lucide-react' import { useState, type FormEvent } from 'react' import { PASSWORD_MIN, updatePassword } from '../lib/cloud/auth' import { timeAgo } from '../lib/timeAgo' import type { AccountApi } from '../store' import ConfirmDialog from './ConfirmDialog' import { SectionTitle } from './ui' import { cx } from '../lib/utils' /** Réglages → Mon compte : mot de passe, déconnexion. La synchro tourne en arrière-plan. */ export default function AccountSection({ account, onMessage }: { account: AccountApi; onMessage: (kind: 'ok' | 'error', text: string) => void }) { const { sync } = account const social = useSocial() const [pwOpen, setPwOpen] = useState(false) const [pw, setPw] = useState('') const [busy, setBusy] = useState<'pw' | 'logout' | 'import' | null>(null) const [confirmLogout, setConfirmLogout] = useState(false) const changePassword = async (e: FormEvent) => { e.preventDefault() setBusy('pw') try { await updatePassword(pw) setPw('') setPwOpen(false) onMessage('ok', t('account.passwordChanged')) } catch (err) { onMessage('error', (err as Error).message) } finally { setBusy(null) } } const logout = async (force = false) => { setBusy('logout') try { const r = await account.logout(force) if (r === 'pending') setConfirmLogout(true) } catch (err) { onMessage('error', (err as Error).message) } finally { setBusy(null) } } const importDevice = async () => { setBusy('import') try { const n = await account.importDeviceData() onMessage('ok', t('account.imported', { count: n })) } catch { onMessage('error', t('account.importFailed')) } finally { setBusy(null) } } return ( <> {t('account.title')}
{account.email}
{/* État de la synchronisation : visible, surtout quand quelque chose bloque */}
{sync.state === 'syncing' ? ( ) : sync.state === 'offline' ? ( ) : sync.state === 'error' ? ( ) : ( )} {sync.state === 'syncing' ? t('sync.syncing') : sync.state === 'offline' ? t('sync.offline') : sync.state === 'error' ? t('sync.error') : sync.lastSyncAt ? t('sync.synced', { when: timeAgo(sync.lastSyncAt) }) : t('sync.never')} {(sync.pending > 0 || (sync.state === 'error' && sync.error)) && ( {[sync.pending > 0 ? t('sync.pending', { count: sync.pending }) : '', sync.state === 'error' ? sync.error : ''].filter(Boolean).join(' · ')} )}
{pwOpen ? (
setPw(e.target.value)} placeholder={t('account.newPasswordPh', { n: PASSWORD_MIN })} autoComplete="new-password" minLength={PASSWORD_MIN} maxLength={72} required />
) : ( )}
{account.deviceData && (

{account.deviceData.lists > 0 ? t('account.deviceDataWithLists', { titles: t('account.nTitles', { count: account.deviceData.items }), lists: t('account.nLists', { count: account.deviceData.lists }) }) : t('account.deviceData', { titles: t('account.nTitles', { count: account.deviceData.items }) })}

{t('account.deviceHint')}

)} { setConfirmLogout(false) void logout(true) }} onCancel={() => setConfirmLogout(false)} /> ) }