2022-03-20 17:38:38 +01:00
|
|
|
import {
|
|
|
|
|
useToast,
|
|
|
|
|
type ToastPositionWithLogical,
|
|
|
|
|
Box,
|
|
|
|
|
HStack,
|
|
|
|
|
Text,
|
|
|
|
|
} from "@chakra-ui/react";
|
2022-04-01 21:41:53 +02:00
|
|
|
import { useNuiEvent } from "../../hooks/useNuiEvent";
|
2022-03-20 11:14:46 +01:00
|
|
|
import { IconProp } from "@fortawesome/fontawesome-svg-core";
|
|
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
2022-03-14 15:53:59 +01:00
|
|
|
|
2022-08-27 15:40:41 +02:00
|
|
|
export interface NotificationProps {
|
2022-03-14 15:53:59 +01:00
|
|
|
title?: string;
|
2022-04-11 14:51:28 +02:00
|
|
|
description?: string;
|
2022-03-14 15:53:59 +01:00
|
|
|
duration?: number;
|
|
|
|
|
position?: ToastPositionWithLogical;
|
|
|
|
|
status?: "info" | "warning" | "success" | "error";
|
|
|
|
|
id?: number;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 15:40:41 +02:00
|
|
|
export interface CustomNotificationProps {
|
2022-03-14 15:53:59 +01:00
|
|
|
style?: React.CSSProperties;
|
2022-04-11 14:51:28 +02:00
|
|
|
description?: string;
|
2022-03-20 17:38:38 +01:00
|
|
|
title?: string;
|
2022-03-14 15:53:59 +01:00
|
|
|
duration?: number;
|
2022-03-20 11:14:46 +01:00
|
|
|
icon?: IconProp;
|
2022-03-20 17:38:38 +01:00
|
|
|
iconColor?: string;
|
2022-03-14 15:53:59 +01:00
|
|
|
position?: ToastPositionWithLogical;
|
|
|
|
|
id?: number;
|
2022-04-11 03:52:18 +00:00
|
|
|
type?: string;
|
2022-03-14 15:53:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Notifications: React.FC = () => {
|
|
|
|
|
const toast = useToast();
|
|
|
|
|
|
2022-08-27 15:40:41 +02:00
|
|
|
useNuiEvent<CustomNotificationProps>("customNotify", (data) => {
|
2022-04-11 14:51:28 +02:00
|
|
|
if (!data.title && !data.description) return;
|
2022-03-14 15:53:59 +01:00
|
|
|
if (data.id && toast.isActive(data.id)) return;
|
2022-04-11 03:52:18 +00:00
|
|
|
if (!data.icon) {
|
|
|
|
|
data.icon =
|
|
|
|
|
data.type === "error"
|
2022-04-11 14:41:07 +02:00
|
|
|
? "circle-xmark"
|
2022-04-11 03:52:18 +00:00
|
|
|
: data.type === "success"
|
2022-04-11 14:41:07 +02:00
|
|
|
? "circle-check"
|
|
|
|
|
: "circle-info";
|
2022-04-11 03:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-27 15:40:41 +02:00
|
|
|
const id = data.id;
|
2022-03-14 15:53:59 +01:00
|
|
|
toast({
|
2022-08-26 09:37:28 -07:00
|
|
|
id,
|
2022-04-11 03:52:18 +00:00
|
|
|
duration: data.duration || 3000,
|
2022-03-14 15:53:59 +01:00
|
|
|
position: data.position || "top-right",
|
|
|
|
|
render: () => (
|
2022-04-11 03:52:18 +00:00
|
|
|
<Box
|
|
|
|
|
className={`toast-${data.type || "inform"}`}
|
|
|
|
|
style={data.style}
|
|
|
|
|
p={2}
|
|
|
|
|
borderRadius="sm"
|
|
|
|
|
boxShadow="md"
|
|
|
|
|
>
|
2022-03-20 17:38:38 +01:00
|
|
|
<HStack spacing={0}>
|
2022-04-11 14:41:07 +02:00
|
|
|
{data.icon && (
|
2022-03-20 17:38:38 +01:00
|
|
|
<FontAwesomeIcon
|
2022-04-16 12:08:55 +02:00
|
|
|
fixedWidth
|
2022-03-20 17:38:38 +01:00
|
|
|
icon={data.icon}
|
2022-04-11 03:52:18 +00:00
|
|
|
fontSize="1.3em"
|
|
|
|
|
style={{ paddingRight: 8 }}
|
2022-03-20 17:38:38 +01:00
|
|
|
color={data.iconColor}
|
|
|
|
|
/>
|
2022-04-11 14:41:07 +02:00
|
|
|
)}
|
2022-03-20 17:38:38 +01:00
|
|
|
<Box w="100%">
|
|
|
|
|
{data.title && <Text as="b">{data.title}</Text>}
|
|
|
|
|
{data.description && <Text>{data.description}</Text>}
|
|
|
|
|
</Box>
|
|
|
|
|
</HStack>
|
2022-03-14 15:53:59 +01:00
|
|
|
</Box>
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-27 15:40:41 +02:00
|
|
|
useNuiEvent<NotificationProps>("notify", (data) => {
|
2022-04-11 14:51:28 +02:00
|
|
|
if (!data.title && !data.description) return;
|
2022-03-14 15:53:59 +01:00
|
|
|
if (data.id && toast.isActive(data.id)) return;
|
2022-08-27 15:40:41 +02:00
|
|
|
const id = data.id;
|
2022-03-14 15:53:59 +01:00
|
|
|
toast({
|
2022-08-26 09:37:28 -07:00
|
|
|
id,
|
2022-03-14 15:53:59 +01:00
|
|
|
title: data.title,
|
|
|
|
|
description: data.description,
|
|
|
|
|
duration: data.duration || 4000,
|
|
|
|
|
position: data.position || "top-right",
|
|
|
|
|
status: data.status,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return <></>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Notifications;
|