import { useState } from 'react' import { useMqttTopic } from '../../hooks/useMqtt' interface Notification { message: string } interface UsersEventPayload { type: 'UserCreated' | 'UserUpdated' | 'UserDeleted' | 'RoleAssigned' user: { name: string } } const MESSAGES: Record string> = { UserCreated: (name) => `${name} was added.`, UserUpdated: (name) => `${name} was updated.`, UserDeleted: (name) => `${name} was removed.`, RoleAssigned: (name) => `${name}'s role was changed.`, } const MAX_NOTIFICATIONS = 10 /** Rolling list of recent Users-module events shown in the navbar bell. */ export function useNotifications(): Notification[] { const [notifications, setNotifications] = useState([]) useMqttTopic('module/users/events', (payload) => { const event = payload as UsersEventPayload const message = MESSAGES[event.type]?.(event.user.name) ?? 'Users list updated.' setNotifications((prev) => [{ message }, ...prev].slice(0, MAX_NOTIFICATIONS)) }) return notifications }