import { BellRing, Loader2 } from 'lucide-react' import { useEffect, useState } from 'react' import { t } from '../i18n' import { enablePush, disablePush, getPushState, PushError, type PushState } from '../lib/push' import { cx } from '../lib/utils' /** Réglages → Notifications : « Notifications sur cet appareil » (Web Push, même app fermée). */ export default function PushToggle() { const [state, setState] = useState(null) const [busy, setBusy] = useState(false) const [error, setError] = useState(null) useEffect(() => { let alive = true void getPushState().then((s) => alive && setState(s)) return () => { alive = false } }, []) if (state === null) return null const on = state === 'on' const available = state === 'on' || state === 'off' const toggle = async () => { if (busy || !available) return setBusy(true) setError(null) try { if (on) { await disablePush() setState('off') } else { // La permission n'est demandée qu'ici, au clic setState(await enablePush()) } } catch (e) { const code = e instanceof PushError ? e.code : 'failed' if (code === 'denied') setState('denied') else setError(code === 'not-ready' ? t('push.notReady') : t('push.error')) } finally { setBusy(false) } } const hint = state === 'needs-install' ? t('push.iosInstall') : state === 'unsupported' ? t('push.unsupported') : state === 'denied' ? t('push.denied') : t('push.deviceHint') return (
{error && (

{error}

)}
) }