2022-10-27 16:44:21 +02:00
|
|
|
import { useToast, type ToastPosition, Box, HStack, Text } from '@chakra-ui/react';
|
2022-08-27 15:58:36 +02:00
|
|
|
import { useNuiEvent } from '../../hooks/useNuiEvent';
|
|
|
|
|
import { IconProp } from '@fortawesome/fontawesome-svg-core';
|
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2022-12-15 17:43:14 -05:00
|
|
|
import ReactMarkdown from 'react-markdown';
|
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;
|
2022-10-27 16:44:21 +02:00
|
|
|
position?: ToastPosition;
|
2022-08-27 15:58:36 +02:00
|
|
|
status?: 'info' | 'warning' | 'success' | 'error';
|
2022-03-14 15:53:59 +01:00
|
|
|
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-10-27 16:44:21 +02:00
|
|
|
position?: ToastPosition;
|
2022-03-14 15:53:59 +01:00
|
|
|
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:58:36 +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) {
|
2022-08-27 15:58:36 +02:00
|
|
|
data.icon = data.type === 'error' ? 'circle-xmark' : data.type === 'success' ? '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-08-27 15:58:36 +02:00
|
|
|
position: data.position || 'top-right',
|
2022-03-14 15:53:59 +01:00
|
|
|
render: () => (
|
2022-08-27 15:58:36 +02: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>}
|
2022-12-15 17:43:14 -05:00
|
|
|
{data.description && <Text><ReactMarkdown>{data.description}</ReactMarkdown></Text>}
|
2022-03-20 17:38:38 +01:00
|
|
|
</Box>
|
|
|
|
|
</HStack>
|
2022-03-14 15:53:59 +01:00
|
|
|
</Box>
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-27 15:58:36 +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,
|
2022-08-27 15:58:36 +02:00
|
|
|
position: data.position || 'top-right',
|
2022-03-14 15:53:59 +01:00
|
|
|
status: data.status,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return <></>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Notifications;
|