All files / src/store notification-store.ts

100% Statements 39/39
100% Branches 6/6
100% Functions 22/22
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154                                                                                                    10x                                 38x             38x           38x         38x       5x 5x   7x     5x       3x 3x 8x   3x       3x 3x 5x   3x       26x 26x 26x       28x 30x 30x       2x 2x       77x 83x   77x       68x   68x     68x       77x   77x      
import { create } from 'zustand';
 
export interface Notification {
  id: string;
  type: 'success' | 'error' | 'warning' | 'info';
  title: string;
  message: string;
  timestamp: Date;
  read: boolean;
  actionUrl?: string;
  actionLabel?: string;
}
 
export interface NotificationCounts {
  total: number;
  unread: number;
  trade: number;
  wallet: number;
  security: number;
}
 
export interface NotificationState {
  // Notifications data
  notifications: Notification[];
  counts: NotificationCounts;
 
  // UI state
  isOpen: boolean;
  isLoading: boolean;
  error: string | null;
 
  // Actions
  addNotification: (
    notification: Omit<Notification, 'id' | 'timestamp' | 'read'>
  ) => void;
  markAsRead: (id: string) => void;
  markAllAsRead: () => void;
  removeNotification: (id: string) => void;
  clearAllNotifications: () => void;
 
  // UI actions
  setOpen: (open: boolean) => void;
  setLoading: (loading: boolean) => void;
  setError: (error: string | null) => void;
 
  // Data actions
  setNotifications: (notifications: Notification[]) => void;
  updateCounts: () => void;
}
 
export const useNotificationStore = create<NotificationState>((set, get) => ({
  // Initial state
  notifications: [],
  counts: {
    total: 0,
    unread: 0,
    trade: 0,
    wallet: 0,
    security: 0,
  },
 
  isOpen: false,
  isLoading: false,
  error: null,
 
  // Actions
  addNotification: notification => {
    const newNotification: Notification = {
      ...notification,
      id: Date.now().toString(),
      timestamp: new Date(),
      read: false,
    };
 
    console.log('🔔 [NotificationStore] Adding notification:', {
      id: newNotification.id,
      type: newNotification.type,
      title: newNotification.title,
    });
 
    set(state => ({
      notifications: [newNotification, ...state.notifications],
    }));
 
    // Update counts
    get().updateCounts();
  },
 
  markAsRead: id => {
    console.log('🔔 [NotificationStore] Marking as read:', id);
    set(state => ({
      notifications: state.notifications.map(n =>
        n.id === id ? { ...n, read: true } : n
      ),
    }));
    get().updateCounts();
  },
 
  markAllAsRead: () => {
    console.log('🔔 [NotificationStore] Marking all as read');
    set(state => ({
      notifications: state.notifications.map(n => ({ ...n, read: true })),
    }));
    get().updateCounts();
  },
 
  removeNotification: id => {
    console.log('🔔 [NotificationStore] Removing notification:', id);
    set(state => ({
      notifications: state.notifications.filter(n => n.id !== id),
    }));
    get().updateCounts();
  },
 
  clearAllNotifications: () => {
    console.log('🔔 [NotificationStore] Clearing all notifications');
    set({ notifications: [] });
    get().updateCounts();
  },
 
  // UI actions
  setOpen: open => set({ isOpen: open }),
  setLoading: loading => set({ isLoading: loading }),
  setError: error => set({ error }),
 
  // Data actions
  setNotifications: notifications => {
    set({ notifications });
    get().updateCounts();
  },
 
  updateCounts: () => {
    const { notifications } = get();
    const unread = notifications.filter(n => !n.read);
 
    const newCounts = {
      total: notifications.length,
      unread: unread.length,
      trade: unread.filter(
        n => n.type === 'info' && n.title.toLowerCase().includes('trade')
      ).length,
      wallet: unread.filter(n => n.title.toLowerCase().includes('wallet'))
        .length,
      security: unread.filter(
        n => n.type === 'warning' || n.title.toLowerCase().includes('security')
      ).length,
    };
 
    console.log('🔔 [NotificationStore] Updating counts:', newCounts);
 
    set({ counts: newCounts });
  },
}));