Files
TREK/client/src/pages/admin/useNotificationDefaults.ts
T
Maurice f4626c9f3e feat(notifications): admin defaults for users, and channels blocked for everyone
Admin > Notifications gets a Defaults for users matrix: each event and channel starts On, Off (a user may still turn it on) or Blocked (off for everyone). A user's own choice is kept against the default, and a value equal to it is stored as no row, so a later change of the default reaches everyone who never chose. Blocked cells show as locked in the user settings on desktop and phone, and the server ignores any attempt to turn one on. (#1536)
2026-10-01 12:28:36 +02:00

55 lines
2.0 KiB
TypeScript

import { useEffect, useRef, useState } from 'react'
import type { NotificationDefault } from '@trek/shared'
import { adminApi } from '../../api/client'
import { useToast } from '../../components/shared/Toast'
import { useTranslation } from '../../i18n'
import type { NotificationChannelDescriptor } from '../../components/Settings/notificationLabels'
export interface NotificationDefaultsMatrix {
defaults: Record<string, Record<string, NotificationDefault>>
channels: NotificationChannelDescriptor[]
event_types: string[]
implemented_combos: Record<string, string[]>
}
/**
* The admin's defaults for user notifications (#1536). Each change saves at
* once and is rolled back with a toast when the server refuses it.
*/
export function useNotificationDefaults() {
const { t } = useTranslation()
const toast = useToast()
const [matrix, setMatrix] = useState<NotificationDefaultsMatrix | null>(null)
const [saving, setSaving] = useState(false)
const notify = useRef({ toast, t })
notify.current = { toast, t }
useEffect(() => {
let cancelled = false
adminApi.getNotificationDefaults()
.then((data: NotificationDefaultsMatrix) => { if (!cancelled) setMatrix(data) })
.catch(() => { if (!cancelled) notify.current.toast.error(notify.current.t('common.error')) })
return () => { cancelled = true }
}, [])
const cycle = async (eventType: string, channel: string, next: NotificationDefault) => {
if (!matrix) return
const before = matrix.defaults[eventType]?.[channel] ?? 'on'
const apply = (value: NotificationDefault) =>
setMatrix(m => m ? { ...m, defaults: { ...m.defaults, [eventType]: { ...m.defaults[eventType], [channel]: value } } } : m)
apply(next)
setSaving(true)
try {
const fresh: NotificationDefaultsMatrix = await adminApi.updateNotificationDefaults({ [eventType]: { [channel]: next } })
setMatrix(fresh)
} catch {
apply(before)
toast.error(t('common.error'))
} finally {
setSaving(false)
}
}
return { matrix, saving, cycle }
}