1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00
portainer/app/react/portainer/notifications/notifications-store.ts
itsconquest 648c1db437
feat(notifications): track toast notifications [EE-4132] (#7711)
* feat(notifications): track toast notifications [EE-4132]

* suggested refactoring

* fix failing test

* remove duplicate styles

* applying spacing to context icon
2022-09-23 17:17:44 +12:00

64 lines
1.9 KiB
TypeScript

import create from 'zustand/vanilla';
import { persist } from 'zustand/middleware';
import { keyBuilder } from '@/portainer/hooks/useLocalStorage';
import { ToastNotification } from './types';
interface NotificationsState {
userNotifications: Record<string, ToastNotification[]>;
addNotification: (userId: number, notification: ToastNotification) => void;
removeNotification: (userId: number, notificationId: string) => void;
removeNotifications: (userId: number, notifications: string[]) => void;
clearUserNotifications: (userId: number) => void;
}
export const notificationsStore = create<NotificationsState>()(
persist(
(set) => ({
userNotifications: {},
addNotification: (userId: number, notification: ToastNotification) => {
set((state) => ({
userNotifications: {
...state.userNotifications,
[userId]: [
...(state.userNotifications[userId] || []),
notification,
],
},
}));
},
removeNotification: (userId: number, notificationId: string) => {
set((state) => ({
userNotifications: {
...state.userNotifications,
[userId]: state.userNotifications[userId].filter(
(notif) => notif.id !== notificationId
),
},
}));
},
removeNotifications: (userId: number, notificationIds: string[]) => {
set((state) => ({
userNotifications: {
...state.userNotifications,
[userId]: state.userNotifications[userId].filter(
(notification) => !notificationIds.includes(notification.id)
),
},
}));
},
clearUserNotifications: (userId: number) => {
set((state) => ({
userNotifications: {
...state.userNotifications,
[userId]: [],
},
}));
},
}),
{
name: keyBuilder('notifications'),
}
)
);