2022-08-27 15:58:36 +02:00
|
|
|
import { useNuiEvent } from '../../hooks/useNuiEvent';
|
|
|
|
|
import { IconProp } from '@fortawesome/fontawesome-svg-core';
|
2023-01-01 14:31:11 +01:00
|
|
|
import { toast, Toaster, ToastPosition } from 'react-hot-toast';
|
2022-08-27 15:58:36 +02:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2022-12-15 17:43:14 -05:00
|
|
|
import ReactMarkdown from 'react-markdown';
|
2023-01-01 19:25:12 +01:00
|
|
|
import { Avatar, createStyles, Group, Stack, Box, Text } from '@mantine/core';
|
2023-01-01 14:31:11 +01:00
|
|
|
import React from 'react';
|
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;
|
2023-01-01 14:31:11 +01:00
|
|
|
position?: ToastPosition | 'top' | 'bottom';
|
2022-12-15 15:56:45 -08:00
|
|
|
variant?: string;
|
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;
|
2023-01-01 14:31:11 +01:00
|
|
|
position?: ToastPosition | 'top' | 'bottom';
|
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
|
|
|
}
|
|
|
|
|
|
2023-01-01 14:31:11 +01:00
|
|
|
const useStyles = createStyles((theme) => ({
|
|
|
|
|
container: {
|
|
|
|
|
width: 300,
|
|
|
|
|
height: 'fit-content',
|
|
|
|
|
backgroundColor: theme.colors.dark[6],
|
|
|
|
|
color: theme.colors.dark[0],
|
|
|
|
|
padding: 12,
|
|
|
|
|
borderRadius: theme.radius.sm,
|
|
|
|
|
fontFamily: 'Roboto',
|
|
|
|
|
boxShadow: theme.shadows.sm,
|
|
|
|
|
},
|
|
|
|
|
title: {
|
|
|
|
|
fontWeight: 500,
|
|
|
|
|
},
|
|
|
|
|
description: {
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
color: theme.colors.dark[2],
|
2023-01-01 19:25:12 +01:00
|
|
|
fontFamily: 'Roboto',
|
2023-01-01 14:31:11 +01:00
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2022-03-14 15:53:59 +01:00
|
|
|
const Notifications: React.FC = () => {
|
2023-01-01 14:31:11 +01:00
|
|
|
const { classes } = useStyles();
|
2022-03-14 15:53:59 +01:00
|
|
|
|
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;
|
2023-01-01 14:31:11 +01:00
|
|
|
// Backwards compat with old notifications
|
|
|
|
|
let position = data.position;
|
|
|
|
|
switch (position) {
|
|
|
|
|
case 'top':
|
|
|
|
|
position = 'top-center';
|
|
|
|
|
break;
|
|
|
|
|
case 'bottom':
|
|
|
|
|
position = 'bottom-center';
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-04-11 03:52:18 +00:00
|
|
|
if (!data.icon) {
|
2023-01-01 14:31:11 +01:00
|
|
|
data.icon = data.type === 'error' ? 'xmark' : data.type === 'success' ? 'check' : 'info';
|
2022-04-11 03:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-01 14:31:11 +01:00
|
|
|
toast(
|
|
|
|
|
<Box style={data.style} className={`${classes.container}`}>
|
|
|
|
|
<Group noWrap spacing={12}>
|
|
|
|
|
{data.icon && (
|
|
|
|
|
<>
|
|
|
|
|
{!data.iconColor ? (
|
|
|
|
|
<Avatar
|
|
|
|
|
color={data.type === 'error' ? 'red' : data.type === 'success' ? 'teal' : 'blue'}
|
|
|
|
|
radius="xl"
|
|
|
|
|
size={(data.title && !data.description) || (data.description && !data.title) ? 28 : undefined}
|
|
|
|
|
>
|
|
|
|
|
<FontAwesomeIcon icon={data.icon} fixedWidth size="lg" />
|
|
|
|
|
</Avatar>
|
|
|
|
|
) : (
|
|
|
|
|
<FontAwesomeIcon icon={data.icon} style={{ color: data.iconColor }} fixedWidth size="lg" />
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
<Stack spacing={0}>
|
|
|
|
|
{data.title && <Text className={classes.title}>{data.title}</Text>}
|
2023-01-01 19:25:12 +01:00
|
|
|
{data.description && <ReactMarkdown className={classes.description}>{data.description}</ReactMarkdown>}
|
2023-01-01 14:31:11 +01:00
|
|
|
</Stack>
|
|
|
|
|
</Group>
|
|
|
|
|
</Box>,
|
|
|
|
|
{
|
|
|
|
|
id: data.id?.toString(),
|
|
|
|
|
duration: data.duration || 3000,
|
|
|
|
|
position: position || 'top-right',
|
|
|
|
|
style: {
|
|
|
|
|
padding: 0,
|
|
|
|
|
boxShadow: 'none',
|
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
2022-03-14 15:53:59 +01:00
|
|
|
});
|
|
|
|
|
|
2023-01-01 19:25:12 +01:00
|
|
|
return <Toaster />;
|
2022-03-14 15:53:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Notifications;
|