58 lines
1.0 KiB
TypeScript
58 lines
1.0 KiB
TypeScript
|
|
"use client";
|
||
|
|
|
||
|
|
import { useSyncExternalStore } from "react";
|
||
|
|
import type { AppNotification } from "./store";
|
||
|
|
import {
|
||
|
|
clearAll,
|
||
|
|
getNotifications,
|
||
|
|
getUnreadCount,
|
||
|
|
markAllRead,
|
||
|
|
markRead,
|
||
|
|
removeNotification,
|
||
|
|
subscribe,
|
||
|
|
} from "./store";
|
||
|
|
|
||
|
|
const SERVER_NOTIFICATIONS: AppNotification[] = [];
|
||
|
|
const SERVER_UNREAD_COUNT = 0;
|
||
|
|
|
||
|
|
function subscribeAll(cb: () => void) {
|
||
|
|
return subscribe(cb);
|
||
|
|
}
|
||
|
|
|
||
|
|
function getSnapshot() {
|
||
|
|
return getNotifications();
|
||
|
|
}
|
||
|
|
|
||
|
|
function getUnreadSnapshot() {
|
||
|
|
return getUnreadCount();
|
||
|
|
}
|
||
|
|
|
||
|
|
function getServerNotifications() {
|
||
|
|
return SERVER_NOTIFICATIONS;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getServerUnreadCount() {
|
||
|
|
return SERVER_UNREAD_COUNT;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useNotifications() {
|
||
|
|
const notifications = useSyncExternalStore(
|
||
|
|
subscribeAll,
|
||
|
|
getSnapshot,
|
||
|
|
getServerNotifications
|
||
|
|
);
|
||
|
|
const unreadCount = useSyncExternalStore(
|
||
|
|
subscribeAll,
|
||
|
|
getUnreadSnapshot,
|
||
|
|
getServerUnreadCount
|
||
|
|
);
|
||
|
|
|
||
|
|
return {
|
||
|
|
notifications,
|
||
|
|
unreadCount,
|
||
|
|
markRead,
|
||
|
|
markAllRead,
|
||
|
|
clearAll,
|
||
|
|
removeNotification,
|
||
|
|
};
|
||
|
|
}
|