mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-07 12:49:00 +00:00
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
|
|
import { useCallback, useMemo, useState, type ReactNode } from "react";
|
||
|
|
import { NotificationSessionContext } from "./notification-session-context";
|
||
|
|
|
||
|
|
function addAll(current: ReadonlySet<string>, ids: string[]): ReadonlySet<string> {
|
||
|
|
if (ids.every((id) => current.has(id))) {
|
||
|
|
return current;
|
||
|
|
}
|
||
|
|
const next = new Set(current);
|
||
|
|
ids.forEach((id) => next.add(id));
|
||
|
|
return next;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Dismissed and read notification ids for this app session only. Nothing is persisted, so a
|
||
|
|
* reload starts over and a condition that still holds shows up again.
|
||
|
|
*/
|
||
|
|
export function NotificationSessionProvider({ children }: { children: ReactNode }) {
|
||
|
|
const [dismissedIds, setDismissedIds] = useState<ReadonlySet<string>>(() => new Set());
|
||
|
|
const [readIds, setReadIds] = useState<ReadonlySet<string>>(() => new Set());
|
||
|
|
|
||
|
|
const dismiss = useCallback((ids: string[]) => {
|
||
|
|
setDismissedIds((current) => addAll(current, ids));
|
||
|
|
}, []);
|
||
|
|
const markRead = useCallback((ids: string[]) => {
|
||
|
|
setReadIds((current) => addAll(current, ids));
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
const value = useMemo(
|
||
|
|
() => ({ dismissedIds, readIds, dismiss, markRead }),
|
||
|
|
[dismissedIds, readIds, dismiss, markRead],
|
||
|
|
);
|
||
|
|
|
||
|
|
return (
|
||
|
|
<NotificationSessionContext.Provider value={value}>
|
||
|
|
{children}
|
||
|
|
</NotificationSessionContext.Provider>
|
||
|
|
);
|
||
|
|
}
|