import { create } from "zustand" import { persist } from "zustand/middleware" const MAXIMUM_NOTIFICATIONS = 50 export interface StoredNotification { identifier: string message: string timestamp: string type: "info" | "success" | "error" actionUrl?: string } interface NotificationState { notifications: StoredNotification[] lastViewedAt: string | null addNotification: ( message: string, type?: "info" | "success" | "error", actionUrl?: string ) => void dismissNotification: (identifier: string) => void clearAllNotifications: () => void markAllAsViewed: () => void } export const useNotificationStore = create()( persist( (set) => ({ notifications: [], lastViewedAt: null, addNotification: (message, type = "info", actionUrl) => set((state) => { const newNotification: StoredNotification = { identifier: crypto.randomUUID(), message, timestamp: new Date().toISOString(), type, ...(actionUrl ? { actionUrl } : {}), } const updated = [newNotification, ...state.notifications].slice( 0, MAXIMUM_NOTIFICATIONS ) return { notifications: updated } }), dismissNotification: (identifier) => set((state) => ({ notifications: state.notifications.filter( (notification) => notification.identifier !== identifier ), })), clearAllNotifications: () => set({ notifications: [] }), markAllAsViewed: () => set({ lastViewedAt: new Date().toISOString() }), }), { name: "asa-news-notifications", } ) )