Files
ox_lib/web/src/features/notifications/NotificationWrapper.tsx

82 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-08-27 15:58:36 +02:00
import { useToast, type ToastPositionWithLogical, Box, HStack, Text } from '@chakra-ui/react';
import { useNuiEvent } from '../../hooks/useNuiEvent';
import { IconProp } from '@fortawesome/fontawesome-svg-core';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
2022-03-14 15:53:59 +01: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;
2022-08-27 15:58:36 +02:00
status?: 'info' | 'warning' | 'success' | 'error';
2022-03-14 15:53:59 +01:00
id?: number;
}
export interface CustomNotificationProps {
2022-03-14 15:53:59 +01:00
style?: React.CSSProperties;
2022-04-11 14:51:28 +02:00
description?: string;
title?: string;
2022-03-14 15:53:59 +01:00
duration?: number;
icon?: IconProp;
iconColor?: string;
2022-03-14 15:53:59 +01:00
position?: ToastPositionWithLogical;
id?: number;
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;
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';
}
const id = data.id;
2022-03-14 15:53:59 +01:00
toast({
id,
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">
<HStack spacing={0}>
{data.icon && (
<FontAwesomeIcon
fixedWidth
icon={data.icon}
fontSize="1.3em"
style={{ paddingRight: 8 }}
color={data.iconColor}
/>
)}
<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: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;
const id = data.id;
2022-03-14 15:53:59 +01:00
toast({
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;