mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 07:56:02 +01:00
refactor(web/notifications): notifications rewrite
This commit is contained in:
@@ -29,6 +29,7 @@
|
|||||||
"prettier": "^2.7.1",
|
"prettier": "^2.7.1",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
"react-dom": "18.2.0",
|
"react-dom": "18.2.0",
|
||||||
|
"react-hot-toast": "^2.4.0",
|
||||||
"react-markdown": "^8.0.1",
|
"react-markdown": "^8.0.1",
|
||||||
"typescript": "^4.6.3",
|
"typescript": "^4.6.3",
|
||||||
"vite": "^2.9.13",
|
"vite": "^2.9.13",
|
||||||
|
|||||||
22
web/pnpm-lock.yaml
generated
22
web/pnpm-lock.yaml
generated
@@ -34,6 +34,7 @@ specifiers:
|
|||||||
prop-types: ^15.8.1
|
prop-types: ^15.8.1
|
||||||
react: 18.2.0
|
react: 18.2.0
|
||||||
react-dom: 18.2.0
|
react-dom: 18.2.0
|
||||||
|
react-hot-toast: ^2.4.0
|
||||||
react-markdown: ^8.0.1
|
react-markdown: ^8.0.1
|
||||||
rimraf: ^3.0.2
|
rimraf: ^3.0.2
|
||||||
typescript: ^4.6.3
|
typescript: ^4.6.3
|
||||||
@@ -66,6 +67,7 @@ dependencies:
|
|||||||
prettier: 2.7.1
|
prettier: 2.7.1
|
||||||
react: 18.2.0
|
react: 18.2.0
|
||||||
react-dom: 18.2.0_react@18.2.0
|
react-dom: 18.2.0_react@18.2.0
|
||||||
|
react-hot-toast: 2.4.0_biqbaboplfbrettd7655fr4n2y
|
||||||
react-markdown: 8.0.1_bbvjflvjoibwhtpmedigb26h6y
|
react-markdown: 8.0.1_bbvjflvjoibwhtpmedigb26h6y
|
||||||
typescript: 4.6.3
|
typescript: 4.6.3
|
||||||
vite: 2.9.13
|
vite: 2.9.13
|
||||||
@@ -2984,6 +2986,12 @@ packages:
|
|||||||
resolution: {integrity: sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==}
|
resolution: {integrity: sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==}
|
||||||
engines: {node: '>=4'}
|
engines: {node: '>=4'}
|
||||||
|
|
||||||
|
/goober/2.1.11:
|
||||||
|
resolution: {integrity: sha512-5SS2lmxbhqH0u9ABEWq7WPU69a4i2pYcHeCxqaNq6Cw3mnrF0ghWNM4tEGid4dKy8XNIAUbuThuozDHHKJVh3A==}
|
||||||
|
peerDependencies:
|
||||||
|
csstype: ^3.0.10
|
||||||
|
dev: false
|
||||||
|
|
||||||
/has-flag/3.0.0:
|
/has-flag/3.0.0:
|
||||||
resolution: {integrity: sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==}
|
resolution: {integrity: sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==}
|
||||||
engines: {node: '>=4'}
|
engines: {node: '>=4'}
|
||||||
@@ -3547,6 +3555,20 @@ packages:
|
|||||||
use-sidecar: 1.1.2_bbvjflvjoibwhtpmedigb26h6y
|
use-sidecar: 1.1.2_bbvjflvjoibwhtpmedigb26h6y
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
|
/react-hot-toast/2.4.0_biqbaboplfbrettd7655fr4n2y:
|
||||||
|
resolution: {integrity: sha512-qnnVbXropKuwUpriVVosgo8QrB+IaPJCpL8oBI6Ov84uvHZ5QQcTp2qg6ku2wNfgJl6rlQXJIQU5q+5lmPOutA==}
|
||||||
|
engines: {node: '>=10'}
|
||||||
|
peerDependencies:
|
||||||
|
react: '>=16'
|
||||||
|
react-dom: '>=16'
|
||||||
|
dependencies:
|
||||||
|
goober: 2.1.11
|
||||||
|
react: 18.2.0
|
||||||
|
react-dom: 18.2.0_react@18.2.0
|
||||||
|
transitivePeerDependencies:
|
||||||
|
- csstype
|
||||||
|
dev: false
|
||||||
|
|
||||||
/react-is/16.13.1:
|
/react-is/16.13.1:
|
||||||
resolution: {integrity: sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==}
|
resolution: {integrity: sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==}
|
||||||
|
|
||||||
|
|||||||
@@ -1,31 +1,46 @@
|
|||||||
import { CustomNotificationProps, NotificationProps } from '../../notifications/NotificationWrapper';
|
import { CustomNotificationProps, NotificationProps } from '../../notifications/NotificationWrapper';
|
||||||
import { debugData } from '../../../utils/debugData';
|
import { debugData } from '../../../utils/debugData';
|
||||||
|
|
||||||
export const debugNotification = () => {
|
|
||||||
debugData<NotificationProps>([
|
|
||||||
{
|
|
||||||
action: 'notify',
|
|
||||||
data: {
|
|
||||||
description: 'Dunak is nerd',
|
|
||||||
title: 'Dunak',
|
|
||||||
id: 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const debugCustomNotification = () => {
|
export const debugCustomNotification = () => {
|
||||||
debugData<CustomNotificationProps>([
|
debugData<CustomNotificationProps>([
|
||||||
{
|
{
|
||||||
action: 'customNotify',
|
action: 'customNotify',
|
||||||
data: {
|
data: {
|
||||||
description: 'Dunak is nerd',
|
title: 'Success',
|
||||||
icon: 'basket-shopping',
|
description: 'Notification description',
|
||||||
style: {
|
type: 'success',
|
||||||
backgroundColor: '#2D3748',
|
|
||||||
color: 'white',
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
]);
|
||||||
|
debugData<CustomNotificationProps>([
|
||||||
|
{
|
||||||
|
action: 'customNotify',
|
||||||
|
data: {
|
||||||
|
title: 'Info',
|
||||||
|
description: 'Notification description',
|
||||||
|
type: 'info',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
debugData<CustomNotificationProps>([
|
||||||
|
{
|
||||||
|
action: 'customNotify',
|
||||||
|
data: {
|
||||||
|
title: 'Error',
|
||||||
|
description: 'Notification description',
|
||||||
|
type: 'error',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
debugData<CustomNotificationProps>([
|
||||||
|
{
|
||||||
|
action: 'customNotify',
|
||||||
|
data: {
|
||||||
|
title: 'Custom icon success',
|
||||||
|
description: 'Notification description',
|
||||||
|
type: 'success',
|
||||||
|
icon: 'microchip',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { debugAlert } from './debug/alert';
|
|||||||
import { debugContext } from './debug/context';
|
import { debugContext } from './debug/context';
|
||||||
import { debugInput } from './debug/input';
|
import { debugInput } from './debug/input';
|
||||||
import { debugMenu } from './debug/menu';
|
import { debugMenu } from './debug/menu';
|
||||||
import { debugCustomNotification, debugNotification } from './debug/notification';
|
import { debugCustomNotification } from './debug/notification';
|
||||||
import { debugCircleProgressbar, debugProgressbar } from './debug/progress';
|
import { debugCircleProgressbar, debugProgressbar } from './debug/progress';
|
||||||
import { debugTextUI } from './debug/textui';
|
import { debugTextUI } from './debug/textui';
|
||||||
import { debugSkillCheck } from './debug/skillcheck';
|
import { debugSkillCheck } from './debug/skillcheck';
|
||||||
@@ -48,10 +48,7 @@ const Dev: React.FC = () => {
|
|||||||
</Button>
|
</Button>
|
||||||
<Divider />
|
<Divider />
|
||||||
<Button fullWidth onClick={() => debugCustomNotification()}>
|
<Button fullWidth onClick={() => debugCustomNotification()}>
|
||||||
Send custom notification
|
Send notification
|
||||||
</Button>
|
|
||||||
<Button fullWidth onClick={() => debugNotification()}>
|
|
||||||
Send default notification
|
|
||||||
</Button>
|
</Button>
|
||||||
<Divider />
|
<Divider />
|
||||||
<Button fullWidth onClick={() => debugProgressbar()}>
|
<Button fullWidth onClick={() => debugProgressbar()}>
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
import { useToast, type ToastPosition, Box, HStack, Text } from '@chakra-ui/react';
|
import { useToast, Box, HStack, Text } from '@chakra-ui/react';
|
||||||
import { useNuiEvent } from '../../hooks/useNuiEvent';
|
import { useNuiEvent } from '../../hooks/useNuiEvent';
|
||||||
import { IconProp } from '@fortawesome/fontawesome-svg-core';
|
import { IconProp } from '@fortawesome/fontawesome-svg-core';
|
||||||
|
import { toast, Toaster, ToastPosition } from 'react-hot-toast';
|
||||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||||
import ReactMarkdown from 'react-markdown';
|
import ReactMarkdown from 'react-markdown';
|
||||||
|
import { Avatar, createStyles, Group, keyframes, Stack } from '@mantine/core';
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
export interface NotificationProps {
|
export interface NotificationProps {
|
||||||
title?: string;
|
title?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
duration?: number;
|
duration?: number;
|
||||||
position?: ToastPosition;
|
position?: ToastPosition | 'top' | 'bottom';
|
||||||
variant?: string;
|
variant?: string;
|
||||||
status?: 'info' | 'warning' | 'success' | 'error';
|
status?: 'info' | 'warning' | 'success' | 'error';
|
||||||
id?: number;
|
id?: number;
|
||||||
@@ -21,64 +24,92 @@ export interface CustomNotificationProps {
|
|||||||
duration?: number;
|
duration?: number;
|
||||||
icon?: IconProp;
|
icon?: IconProp;
|
||||||
iconColor?: string;
|
iconColor?: string;
|
||||||
position?: ToastPosition;
|
position?: ToastPosition | 'top' | 'bottom';
|
||||||
id?: number;
|
id?: number;
|
||||||
type?: string;
|
type?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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],
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
const Notifications: React.FC = () => {
|
const Notifications: React.FC = () => {
|
||||||
const toast = useToast();
|
const { classes } = useStyles();
|
||||||
|
|
||||||
useNuiEvent<CustomNotificationProps>('customNotify', (data) => {
|
useNuiEvent<CustomNotificationProps>('customNotify', (data) => {
|
||||||
if (!data.title && !data.description) return;
|
if (!data.title && !data.description) return;
|
||||||
if (data.id && toast.isActive(data.id)) return;
|
// Backwards compat with old notifications
|
||||||
|
let position = data.position;
|
||||||
|
switch (position) {
|
||||||
|
case 'top':
|
||||||
|
position = 'top-center';
|
||||||
|
break;
|
||||||
|
case 'bottom':
|
||||||
|
position = 'bottom-center';
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (!data.icon) {
|
if (!data.icon) {
|
||||||
data.icon = data.type === 'error' ? 'circle-xmark' : data.type === 'success' ? 'circle-check' : 'circle-info';
|
data.icon = data.type === 'error' ? 'xmark' : data.type === 'success' ? 'check' : 'info';
|
||||||
}
|
}
|
||||||
|
|
||||||
const id = data.id;
|
toast(
|
||||||
toast({
|
<Box style={data.style} className={`${classes.container}`}>
|
||||||
id,
|
<Group noWrap spacing={12}>
|
||||||
duration: data.duration || 3000,
|
|
||||||
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 && (
|
{data.icon && (
|
||||||
<FontAwesomeIcon
|
<>
|
||||||
fixedWidth
|
{!data.iconColor ? (
|
||||||
icon={data.icon}
|
<Avatar
|
||||||
fontSize="1.3em"
|
color={data.type === 'error' ? 'red' : data.type === 'success' ? 'teal' : 'blue'}
|
||||||
style={{ paddingRight: 8 }}
|
radius="xl"
|
||||||
color={data.iconColor}
|
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" />
|
||||||
)}
|
)}
|
||||||
<Box w="100%">
|
</>
|
||||||
{data.title && <Text as="b">{data.title}</Text>}
|
)}
|
||||||
{data.description && <Text><ReactMarkdown>{data.description}</ReactMarkdown></Text>}
|
<Stack spacing={0}>
|
||||||
</Box>
|
{data.title && <Text className={classes.title}>{data.title}</Text>}
|
||||||
</HStack>
|
{data.description && (
|
||||||
</Box>
|
<Text className={classes.description}>
|
||||||
),
|
<ReactMarkdown>{data.description}</ReactMarkdown>
|
||||||
});
|
</Text>
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
|
</Group>
|
||||||
|
</Box>,
|
||||||
|
{
|
||||||
|
id: data.id?.toString(),
|
||||||
|
duration: data.duration || 3000,
|
||||||
|
position: position || 'top-right',
|
||||||
|
style: {
|
||||||
|
padding: 0,
|
||||||
|
boxShadow: 'none',
|
||||||
|
backgroundColor: 'transparent',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
useNuiEvent<NotificationProps>('notify', (data) => {
|
return <Toaster></Toaster>;
|
||||||
if (!data.title && !data.description) return;
|
|
||||||
if (data.id && toast.isActive(data.id)) return;
|
|
||||||
const id = data.id;
|
|
||||||
toast({
|
|
||||||
id,
|
|
||||||
title: data.title,
|
|
||||||
description: data.description,
|
|
||||||
duration: data.duration || 4000,
|
|
||||||
position: data.position || 'top-right',
|
|
||||||
variant: data.variant,
|
|
||||||
status: data.status,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return <></>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Notifications;
|
export default Notifications;
|
||||||
|
|||||||
Reference in New Issue
Block a user