'use client' import { useEffect, useState } from 'react' import NotificationsNoneIcon from '@mui/icons-material/NotificationsNone' import { Box, Switch } from '@mui/material' import { useI18n } from '@d3ro/i18n' import { d3roFocusRing, d3roPalette } from '@d3ro/ui/theme' import { useAuth } from '@/components/providers/auth-provider' import { disableCurrentWebPush, enableCurrentWebPush, getCurrentWebPushConnectionStatus, reconcileFailedWebPushEnable, type WebPushConnectionStatus, } from '@/lib/web-push-session' type ControlStatus = | WebPushConnectionStatus | 'checking' | 'enabling' | 'disabling' | 'error' const STATUS_MESSAGE_KEYS = { checking: 'web.push.checking', enabling: 'web.push.enabling', disabling: 'web.push.disabling', subscribed: 'web.push.enabled', unsubscribed: 'web.push.disabled', unsupported: 'web.push.unsupported', unconfigured: 'web.push.unconfigured', blocked: 'web.push.blocked', orphaned: 'web.push.disabled', revoked: 'web.push.revoked', error: 'web.push.error', } as const function isBusy(status: ControlStatus): boolean { return status === 'checking' || status === 'enabling' || status === 'disabling' } function isUnavailable(status: ControlStatus): boolean { return status === 'unsupported' || status === 'unconfigured' || status === 'blocked' || status === 'revoked' } export function WebPushControl(): React.ReactElement { const { t } = useI18n() const { user } = useAuth() const [status, setStatus] = useState('checking') const [enabled, setEnabled] = useState(false) const userId = user?.id useEffect(() => { let active = true if (!userId) { setEnabled(false) setStatus('checking') return () => { active = false } } void getCurrentWebPushConnectionStatus(userId) .then(async (nextStatus) => { if (!active) return if (nextStatus === 'orphaned' || nextStatus === 'revoked') { try { await disableCurrentWebPush(userId) } catch { if (active) setStatus('error') return } } if (!active) return setEnabled(nextStatus === 'subscribed') setStatus(nextStatus === 'orphaned' ? 'unsubscribed' : nextStatus) }) .catch(() => { if (active) setStatus('error') }) return () => { active = false } }, [userId]) async function handleToggle(shouldEnable: boolean): Promise { if (!userId || isBusy(status) || isUnavailable(status)) return if (shouldEnable) { setStatus('enabling') try { await enableCurrentWebPush(userId) setEnabled(true) setStatus('subscribed') } catch { try { const current = await getCurrentWebPushConnectionStatus(userId) const reconciled = reconcileFailedWebPushEnable(current) setEnabled(reconciled.enabled) setStatus(reconciled.status) } catch { setEnabled(false) setStatus('error') } } return } setStatus('disabling') try { await disableCurrentWebPush(userId) setEnabled(false) setStatus('unsubscribed') } catch { try { const current = await getCurrentWebPushConnectionStatus(userId) setEnabled(current === 'subscribed') } catch { setEnabled(false) } setStatus('error') } } const disabled = !userId || isBusy(status) || isUnavailable(status) const isError = status === 'error' || status === 'revoked' return ( {t(STATUS_MESSAGE_KEYS[status])} ) }