import React from "react";
import { NotificationProvider } from "@pankod/refine-core";
import {
showNotification,
updateNotification,
hideNotification,
} from "@mantine/notifications";
import { ActionIcon, Group, Text } from "@mantine/core";
import { IconCheck, IconRotate2, IconX } from "@tabler/icons";
import { RingCountdown } from "@components";
export const notificationProvider = (): NotificationProvider => {
const activeNotifications: string[] = [];
const isNotificationActive = (key?: string) => {
return activeNotifications.includes(key as string);
};
const addNotification = (key?: string) => {
if (key) {
const index = activeNotifications.indexOf(key);
if (index === -1) {
activeNotifications.push(key);
}
}
};
const removeNotification = (key?: string) => {
if (key) {
const index = activeNotifications.indexOf(key);
if (index > -1) {
activeNotifications.splice(index, 1);
}
}
};
const notificationProvider: NotificationProvider = {
open: ({ message, type, undoableTimeout, key, cancelMutation }) => {
if (type === "progress") {
if (isNotificationActive(key)) {
updateNotification({
id: key!,
message: (
{message}
{
cancelMutation?.();
if (key) {
removeNotification(key);
hideNotification(key);
}
}}
>
),
styles: {
root: {
paddingLeft: "8px",
paddingTop: "0px",
paddingBottom: "0px",
"&::before": { display: "none" },
},
},
disallowClose: true,
autoClose: false,
});
} else {
addNotification(key);
showNotification({
id: key,
message: (
{message}
{
cancelMutation?.();
if (key) {
removeNotification(key);
hideNotification(key);
}
}}
>
),
styles: {
root: {
paddingLeft: "8px",
paddingTop: "0px",
paddingBottom: "0px",
"&::before": { display: "none" },
},
},
disallowClose: true,
autoClose: false,
});
}
} else {
if (isNotificationActive(key)) {
updateNotification({
id: key!,
color: type === "success" ? "primary" : "red",
icon:
type === "success" ? (
) : (
),
message,
autoClose: 5000,
});
} else {
addNotification(key);
showNotification({
color: type === "success" ? "primary" : "red",
icon:
type === "success" ? (
) : (
),
message,
onClose: () => {
removeNotification(key);
},
autoClose: 5000,
});
}
}
},
close: (key) => {
removeNotification(key);
hideNotification(key);
},
};
return notificationProvider;
};