26 lines
755 B
TypeScript
26 lines
755 B
TypeScript
import { useEffect, useSyncExternalStore } from 'react';
|
|
import {
|
|
dismissNotice,
|
|
getNoticesState,
|
|
isClosed,
|
|
startNoticePolling,
|
|
subscribeNotices,
|
|
visibleNotices,
|
|
type Notice,
|
|
} from '../api/notices';
|
|
|
|
/** The notices in force for this app. Reading is free; polling starts with `poll`. */
|
|
export function usePlatformNotices({ poll = false }: { poll?: boolean } = {}) {
|
|
const state = useSyncExternalStore(subscribeNotices, getNoticesState);
|
|
|
|
useEffect(() => (poll ? startNoticePolling() : undefined), [poll]);
|
|
|
|
const notices = visibleNotices(state.notices, state.dismissed);
|
|
return {
|
|
notices,
|
|
mode: state.mode,
|
|
closedNotice: state.notices.find((n) => isClosed(n.mode)),
|
|
dismiss: (n: Notice) => dismissNotice(n),
|
|
};
|
|
}
|