feat(nui): Notifications

This commit is contained in:
Luke
2022-03-14 15:53:59 +01:00
parent 5314b59e8b
commit 28ea678b92
2 changed files with 83 additions and 1 deletions

View File

@@ -1,7 +1,9 @@
import Notifications from "./Notifications";
const App: React.FC = () => {
return (
<>
<></>
<Notifications />
</>
);
};

View File

@@ -0,0 +1,80 @@
import { useToast, type ToastPositionWithLogical, Box } from "@chakra-ui/react";
import { useNuiEvent } from "../hooks/useNuiEvent";
import { debugData } from "../utils/debugData";
interface Props {
title?: string;
description: string;
duration?: number;
position?: ToastPositionWithLogical;
status?: "info" | "warning" | "success" | "error";
color?: string;
icon?: string;
id?: number;
}
interface CustomProps {
style?: React.CSSProperties;
text: string;
duration?: number;
icon?: string;
position?: ToastPositionWithLogical;
id?: number;
}
debugData<Props>([
{
action: "notify",
data: {
description: "Dunak is nerd",
color: "red",
id: 1,
},
},
]);
debugData<CustomProps>([
{
action: "customNotify",
data: {
text: "Dunak is nerd",
style: {
backgroundColor: "red",
color: "purple",
},
},
},
]);
const Notifications: React.FC = () => {
const toast = useToast();
// todo: figure out icon support
useNuiEvent<CustomProps>("customNotify", (data) => {
if (data.id && toast.isActive(data.id)) return;
toast({
duration: data.duration || 4000,
position: data.position || "top-right",
render: () => (
<Box style={data.style} p={3}>
{data.text}
</Box>
),
});
});
useNuiEvent<Props>("notify", (data) => {
if (data.id && toast.isActive(data.id)) return;
toast({
title: data.title,
description: data.description,
duration: data.duration || 4000,
position: data.position || "top-right",
status: data.status,
});
});
return <></>;
};
export default Notifications;