haixunMaster/lib/notifications/use-notifications.ts

58 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

2026-06-21 12:50:31 +00:00
"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,
};
}