Skip to content

Instantly share code, notes, and snippets.

@dovranJorayev
Last active May 28, 2024 05:42
Show Gist options
  • Save dovranJorayev/33cdcac5899490ed6b6aed620e6ecb20 to your computer and use it in GitHub Desktop.
Save dovranJorayev/33cdcac5899490ed6b6aed620e6ecb20 to your computer and use it in GitHub Desktop.
// shared/lib/notification/core.ts
import {
cleanNotifications,
cleanNotificationsQueue,
hideNotification,
showNotification,
updateNotification,
updateNotificationsState,
NotificationsStore,
NotificationData,
createNotificationsStore as createMantineNotificationsStore,
} from '@mantine/notifications';
import { createInjectionToken } from '../inversify';
import { inject, injectable } from 'inversify';
export const NOTIFICATION_STORE_TOKEN =
createInjectionToken<NotificationsStore>('NotificationStoreToken');
export const createNotificationsStore = createMantineNotificationsStore;
export const NOTIFICATION_SERVICE_TOKEN = createInjectionToken<
import('.').NotificationService
>('NotificationService');
@injectable()
export class NotificationService {
constructor(
@inject(NOTIFICATION_STORE_TOKEN) private store: NotificationsStore
) {}
show = (notification: NotificationData) =>
showNotification(notification, this.store);
hide = (id: string) => hideNotification(id, this.store);
update = (notification: NotificationData) =>
updateNotification(notification, this.store);
clean = () => cleanNotifications(this.store);
cleanQueue = () => cleanNotificationsQueue(this.store);
updateState = (
update: (notifications: NotificationData[]) => NotificationData[]
) => updateNotificationsState(this.store, update);
}
// shared/lib/notification/react.ts
import {
Notifications,
NotificationsProps,
NotificationsStore,
} from '@mantine/notifications';
export type NotificationViewProviderProps = Omit<
NotificationsProps,
'store'
> & {
store: NotificationsStore;
children: React.ReactNode;
};
export const NotificationViewProvider: React.FC<
NotificationViewProviderProps
> = ({ store, children, ...props }) => {
return (
<>
<Notifications store={store} {...props} />
{children}
</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment