mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 07:26:02 +01:00
fix(web/notify): prevent resetting the notifications circle without an id (#566)
This commit is contained in:
@@ -2,7 +2,7 @@ import { useNuiEvent } from '../../hooks/useNuiEvent';
|
|||||||
import { toast, Toaster } from 'react-hot-toast';
|
import { toast, Toaster } from 'react-hot-toast';
|
||||||
import ReactMarkdown from 'react-markdown';
|
import ReactMarkdown from 'react-markdown';
|
||||||
import { Box, Center, createStyles, Group, keyframes, RingProgress, Stack, Text, ThemeIcon } from '@mantine/core';
|
import { Box, Center, createStyles, Group, keyframes, RingProgress, Stack, Text, ThemeIcon } from '@mantine/core';
|
||||||
import React, { useRef } from 'react';
|
import React, { useState } from 'react';
|
||||||
import tinycolor from 'tinycolor2';
|
import tinycolor from 'tinycolor2';
|
||||||
import type { NotificationProps } from '../../typings';
|
import type { NotificationProps } from '../../typings';
|
||||||
import MarkdownComponents from '../../config/MarkdownComponents';
|
import MarkdownComponents from '../../config/MarkdownComponents';
|
||||||
@@ -37,72 +37,39 @@ const useStyles = createStyles((theme) => ({
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// I hate this
|
const createAnimation = (from: string, to: string, visible: boolean) => keyframes({
|
||||||
const enterAnimationTop = keyframes({
|
|
||||||
from: {
|
from: {
|
||||||
opacity: 0,
|
opacity: visible ? 0 : 1,
|
||||||
transform: 'translateY(-30px)',
|
transform: `translate${from}`,
|
||||||
},
|
},
|
||||||
to: {
|
to: {
|
||||||
opacity: 1,
|
opacity: visible ? 1 : 0,
|
||||||
transform: 'translateY(0px)',
|
transform: `translate${to}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const enterAnimationBottom = keyframes({
|
const getAnimation = (visible: boolean, position: string) => {
|
||||||
from: {
|
const animationOptions = visible ? '0.2s ease-out forwards' : '0.4s ease-in forwards'
|
||||||
opacity: 0,
|
let animation: { from: string; to: string };
|
||||||
transform: 'translateY(30px)',
|
|
||||||
},
|
|
||||||
to: {
|
|
||||||
opacity: 1,
|
|
||||||
transform: 'translateY(0px)',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const exitAnimationTop = keyframes({
|
if (visible) {
|
||||||
from: {
|
animation = position.includes('bottom') ? { from: 'Y(30px)', to: 'Y(0px)' } : { from: 'Y(-30px)', to:'Y(0px)' };
|
||||||
opacity: 1,
|
} else {
|
||||||
transform: 'translateY(0px)',
|
if (position.includes('right')) {
|
||||||
},
|
animation = { from: 'X(0px)', to: 'X(100%)' }
|
||||||
to: {
|
} else if (position.includes('left')) {
|
||||||
opacity: 0,
|
animation = { from: 'X(0px)', to: 'X(-100%)' };
|
||||||
transform: 'translateY(-100%)',
|
} else if (position === 'top-center') {
|
||||||
},
|
animation = { from: 'Y(0px)', to: 'Y(-100%)' };
|
||||||
});
|
} else if (position === 'bottom') {
|
||||||
|
animation = { from: 'Y(0px)', to: 'Y(100%)' };
|
||||||
|
} else {
|
||||||
|
animation = { from: 'X(0px)', to: 'X(100%)' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const exitAnimationRight = keyframes({
|
return `${createAnimation(animation.from, animation.to, visible)} ${animationOptions}`
|
||||||
from: {
|
};
|
||||||
opacity: 1,
|
|
||||||
transform: 'translateX(0px)',
|
|
||||||
},
|
|
||||||
to: {
|
|
||||||
opacity: 0,
|
|
||||||
transform: 'translateX(100%)',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const exitAnimationLeft = keyframes({
|
|
||||||
from: {
|
|
||||||
opacity: 1,
|
|
||||||
transform: 'translateX(0px)',
|
|
||||||
},
|
|
||||||
to: {
|
|
||||||
opacity: 0,
|
|
||||||
transform: 'translateX(-100%)',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const exitAnimationBottom = keyframes({
|
|
||||||
from: {
|
|
||||||
opacity: 1,
|
|
||||||
transform: 'translateY(0px)',
|
|
||||||
},
|
|
||||||
to: {
|
|
||||||
opacity: 0,
|
|
||||||
transform: 'translateY(100%)',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const durationCircle = keyframes({
|
const durationCircle = keyframes({
|
||||||
'0%': { strokeDasharray: `0, ${15.1 * 2 * Math.PI}` },
|
'0%': { strokeDasharray: `0, ${15.1 * 2 * Math.PI}` },
|
||||||
@@ -111,22 +78,22 @@ const durationCircle = keyframes({
|
|||||||
|
|
||||||
const Notifications: React.FC = () => {
|
const Notifications: React.FC = () => {
|
||||||
const { classes } = useStyles();
|
const { classes } = useStyles();
|
||||||
const toastKeyRef = useRef(0);
|
const [toastKey, setToastKey] = useState(0);
|
||||||
|
|
||||||
useNuiEvent<NotificationProps>('notify', (data) => {
|
useNuiEvent<NotificationProps>('notify', (data) => {
|
||||||
const toastId = data.id?.toString();
|
|
||||||
|
|
||||||
if (toastId) toastKeyRef.current++;
|
|
||||||
|
|
||||||
if (!data.title && !data.description) return;
|
if (!data.title && !data.description) return;
|
||||||
|
|
||||||
|
const toastId = data.id?.toString();
|
||||||
|
const duration = data.duration || 3000;
|
||||||
|
|
||||||
let iconColor: string;
|
let iconColor: string;
|
||||||
let duration = data.duration || 3000;
|
let position = data.position || 'top-right';
|
||||||
|
|
||||||
data.showDuration = data.showDuration !== undefined ? data.showDuration : true;
|
data.showDuration = data.showDuration !== undefined ? data.showDuration : true;
|
||||||
|
|
||||||
|
if (toastId) setToastKey(prevKey => prevKey + 1);
|
||||||
|
|
||||||
// Backwards compat with old notifications
|
// Backwards compat with old notifications
|
||||||
let position = data.position;
|
|
||||||
switch (position) {
|
switch (position) {
|
||||||
case 'top':
|
case 'top':
|
||||||
position = 'top-center';
|
position = 'top-center';
|
||||||
@@ -135,6 +102,7 @@ const Notifications: React.FC = () => {
|
|||||||
position = 'bottom-center';
|
position = 'bottom-center';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.icon) {
|
if (!data.icon) {
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case 'error':
|
case 'error':
|
||||||
@@ -151,6 +119,7 @@ const Notifications: React.FC = () => {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.iconColor) {
|
if (!data.iconColor) {
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case 'error':
|
case 'error':
|
||||||
@@ -169,23 +138,12 @@ const Notifications: React.FC = () => {
|
|||||||
} else {
|
} else {
|
||||||
iconColor = tinycolor(data.iconColor).toRgbString();
|
iconColor = tinycolor(data.iconColor).toRgbString();
|
||||||
}
|
}
|
||||||
|
|
||||||
toast.custom(
|
toast.custom(
|
||||||
(t) => (
|
(t) => (
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
animation: t.visible
|
animation: getAnimation(t.visible, position),
|
||||||
? `${position?.includes('bottom') ? enterAnimationBottom : enterAnimationTop} 0.2s ease-out forwards`
|
|
||||||
: `${
|
|
||||||
position?.includes('right')
|
|
||||||
? exitAnimationRight
|
|
||||||
: position?.includes('left')
|
|
||||||
? exitAnimationLeft
|
|
||||||
: position === 'top-center'
|
|
||||||
? exitAnimationTop
|
|
||||||
: position
|
|
||||||
? exitAnimationBottom
|
|
||||||
: exitAnimationRight
|
|
||||||
} 0.4s ease-in forwards`,
|
|
||||||
...data.style,
|
...data.style,
|
||||||
}}
|
}}
|
||||||
className={`${classes.container}`}
|
className={`${classes.container}`}
|
||||||
@@ -195,7 +153,7 @@ const Notifications: React.FC = () => {
|
|||||||
<>
|
<>
|
||||||
{data.showDuration ? (
|
{data.showDuration ? (
|
||||||
<RingProgress
|
<RingProgress
|
||||||
key={toastKeyRef.current}
|
key={toastKey}
|
||||||
size={38}
|
size={38}
|
||||||
thickness={2}
|
thickness={2}
|
||||||
sections={[{ value: 100, color: iconColor }]}
|
sections={[{ value: 100, color: iconColor }]}
|
||||||
@@ -215,7 +173,7 @@ const Notifications: React.FC = () => {
|
|||||||
color={iconColor}
|
color={iconColor}
|
||||||
radius="xl"
|
radius="xl"
|
||||||
size={32}
|
size={32}
|
||||||
variant={tinycolor(iconColor).getAlpha() === 0 ? undefined : 'light'}
|
variant={tinycolor(iconColor).getAlpha() < 0 ? undefined : 'light'}
|
||||||
>
|
>
|
||||||
<LibIcon icon={data.icon} fixedWidth color={iconColor} animation={data.iconAnimation} />
|
<LibIcon icon={data.icon} fixedWidth color={iconColor} animation={data.iconAnimation} />
|
||||||
</ThemeIcon>
|
</ThemeIcon>
|
||||||
@@ -227,7 +185,7 @@ const Notifications: React.FC = () => {
|
|||||||
color={iconColor}
|
color={iconColor}
|
||||||
radius="xl"
|
radius="xl"
|
||||||
size={32}
|
size={32}
|
||||||
variant={tinycolor(iconColor).getAlpha() === 0 ? undefined : 'light'}
|
variant={tinycolor(iconColor).getAlpha() < 0 ? undefined : 'light'}
|
||||||
style={{ alignSelf: !data.alignIcon || data.alignIcon === 'center' ? 'center' : 'start' }}
|
style={{ alignSelf: !data.alignIcon || data.alignIcon === 'center' ? 'center' : 'start' }}
|
||||||
>
|
>
|
||||||
<LibIcon icon={data.icon} fixedWidth color={iconColor} animation={data.iconAnimation} />
|
<LibIcon icon={data.icon} fixedWidth color={iconColor} animation={data.iconAnimation} />
|
||||||
@@ -252,7 +210,7 @@ const Notifications: React.FC = () => {
|
|||||||
{
|
{
|
||||||
id: toastId,
|
id: toastId,
|
||||||
duration: duration,
|
duration: duration,
|
||||||
position: position || 'top-right',
|
position: position,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user