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

99 lines
2.4 KiB
TypeScript
Raw Normal View History

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;
status?: "info" | "warning" | "success" | "error";
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();
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) {
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-03-14 15:53:59 +01:00
position: data.position || "top-right",
render: () => (
<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>
),
});
});
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,
position: data.position || "top-right",
status: data.status,
});
});
return <></>;
};
export default Notifications;