Loading data from network...
import { create } from 'zustand'
import { persist } from 'zustand/middleware'
const useNotificationsStore = create(
persist(
(set, get) => ({
notifications: [],
addNotification: (notification) => {
const exists = get().notifications.find((item) => item.id === notification.id)
if (exists) return
set({ notifications: [notification, ...get().notifications].slice(0, 20) })
},
markAllRead: () => {
set({ notifications: get().notifications.map((n) => ({ ...n, read: true })) })
}
}),
{ name: 'cosave-notifications' }
)
)
export default function useNotifications() {
const { notifications, addNotification, markAllRead } = useNotificationsStore()
const unreadCount = notifications.filter((n) => !n.read).length
return { notifications, unreadCount, addNotification, markAllRead }
}