The tree carried uncommitted work from several areas (web push, mobile screens, admin charts, test-hygiene cleanup, CI and map docs). It is landed as one commit so the release pipeline can start from a clean tree; the desktop transcript-correction behaviour shipped in the previous commit and is unchanged here.
209 lines
5.7 KiB
TypeScript
209 lines
5.7 KiB
TypeScript
'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<ControlStatus>('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<void> {
|
|
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 (
|
|
<Box sx={{ px: 2, py: 1.5, borderTop: `1px solid ${d3roPalette.border.default}` }}>
|
|
<Box sx={{ minHeight: 44, display: 'flex', alignItems: 'center', gap: 1 }}>
|
|
<NotificationsNoneIcon
|
|
aria-hidden="true"
|
|
sx={{ width: 20, height: 20, color: d3roPalette.text.label }}
|
|
/>
|
|
<Box
|
|
id="web-push-label"
|
|
sx={{ flex: 1, color: d3roPalette.text.secondary, fontSize: 13 }}
|
|
>
|
|
{t('web.push.label')}
|
|
</Box>
|
|
<Box
|
|
component="label"
|
|
htmlFor="web-push-toggle"
|
|
sx={{
|
|
width: 44,
|
|
height: 44,
|
|
flexShrink: 0,
|
|
display: 'grid',
|
|
placeItems: 'center',
|
|
cursor: disabled ? 'not-allowed' : 'pointer',
|
|
}}
|
|
>
|
|
<Switch
|
|
id="web-push-toggle"
|
|
size="small"
|
|
checked={enabled}
|
|
disabled={disabled}
|
|
onChange={(_event, checked) => void handleToggle(checked)}
|
|
sx={{
|
|
width: 44,
|
|
height: 44,
|
|
p: '17px 9px',
|
|
'& .MuiSwitch-switchBase': {
|
|
top: 10,
|
|
left: 2,
|
|
},
|
|
'& .MuiSwitch-input': {
|
|
top: -10,
|
|
left: -2,
|
|
width: 44,
|
|
height: 44,
|
|
},
|
|
'& .MuiSwitch-switchBase.Mui-checked .MuiSwitch-input': {
|
|
transform: 'translateX(-16px)',
|
|
},
|
|
'& .MuiSwitch-switchBase.Mui-focusVisible': d3roFocusRing,
|
|
}}
|
|
slotProps={{
|
|
input: {
|
|
role: 'switch',
|
|
'aria-labelledby': 'web-push-label',
|
|
'aria-describedby': 'web-push-status',
|
|
},
|
|
}}
|
|
/>
|
|
</Box>
|
|
</Box>
|
|
<Box
|
|
id="web-push-status"
|
|
role="status"
|
|
aria-live="polite"
|
|
aria-atomic="true"
|
|
sx={{
|
|
minHeight: 32,
|
|
mt: 0.5,
|
|
color: isError ? d3roPalette.status.danger : d3roPalette.text.muted,
|
|
fontSize: 11,
|
|
lineHeight: 1.45,
|
|
}}
|
|
>
|
|
{t(STATUS_MESSAGE_KEYS[status])}
|
|
</Box>
|
|
</Box>
|
|
)
|
|
}
|