29 lines
833 B
TypeScript
29 lines
833 B
TypeScript
import React from 'react';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import { useEventContext } from '../../context/EventContext';
|
|
import { listGuestNotifications } from '../../api';
|
|
|
|
/**
|
|
* Lightweight badge count for alerts tab.
|
|
* Fetches guest notifications for the active event and returns count.
|
|
*/
|
|
export function useAlertsBadge() {
|
|
const { activeEvent } = useEventContext();
|
|
const slug = activeEvent?.slug;
|
|
|
|
const { data: count = 0 } = useQuery<number>({
|
|
queryKey: ['mobile', 'alerts', 'badge', slug],
|
|
enabled: Boolean(slug),
|
|
staleTime: 60_000,
|
|
queryFn: async () => {
|
|
if (!slug) {
|
|
return 0;
|
|
}
|
|
const alerts = await listGuestNotifications(slug);
|
|
return Array.isArray(alerts) ? alerts.length : 0;
|
|
},
|
|
});
|
|
|
|
return React.useMemo(() => ({ count }), [count]);
|
|
}
|