2022-08-27 15:58:36 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import { CircularProgress, CircularProgressLabel, Flex, Text } from '@chakra-ui/react';
|
|
|
|
|
import { useNuiEvent } from '../../hooks/useNuiEvent';
|
|
|
|
|
import { fetchNui } from '../../utils/fetchNui';
|
2022-03-15 15:42:54 +01:00
|
|
|
|
2022-08-27 15:40:41 +02:00
|
|
|
export interface CircleProgressbarProps {
|
2022-05-31 18:30:54 +01:00
|
|
|
label?: string;
|
2022-03-15 15:42:54 +01:00
|
|
|
duration: number;
|
2022-08-27 15:58:36 +02:00
|
|
|
position?: 'middle' | 'bottom';
|
2022-03-20 19:58:52 +01:00
|
|
|
percent?: boolean;
|
2022-03-15 15:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const CircleProgressbar: React.FC = () => {
|
|
|
|
|
const [visible, setVisible] = React.useState(false);
|
|
|
|
|
const [progressDuration, setProgressDuration] = React.useState(0);
|
2022-08-27 15:58:36 +02:00
|
|
|
const [position, setPosition] = React.useState<'middle' | 'bottom'>('middle');
|
2022-03-15 15:42:54 +01:00
|
|
|
const [value, setValue] = React.useState(0);
|
2022-08-27 15:58:36 +02:00
|
|
|
const [label, setLabel] = React.useState('');
|
2022-03-15 21:08:08 +01:00
|
|
|
const [cancelled, setCancelled] = React.useState(false);
|
2022-03-15 15:42:54 +01:00
|
|
|
|
|
|
|
|
const progressComplete = () => {
|
|
|
|
|
setVisible(false);
|
2022-08-27 15:58:36 +02:00
|
|
|
fetchNui('progressComplete');
|
2022-03-15 15:42:54 +01:00
|
|
|
};
|
|
|
|
|
|
2022-03-15 21:08:08 +01:00
|
|
|
const progressCancel = () => {
|
|
|
|
|
setCancelled(true);
|
|
|
|
|
setValue(99); // Sets the final value to 100% kek
|
2022-06-14 19:18:24 +02:00
|
|
|
setVisible(false);
|
2022-03-15 21:08:08 +01:00
|
|
|
};
|
|
|
|
|
|
2022-08-27 15:58:36 +02:00
|
|
|
useNuiEvent('progressCancel', progressCancel);
|
2022-03-15 21:08:08 +01:00
|
|
|
|
2022-08-27 15:58:36 +02:00
|
|
|
useNuiEvent<CircleProgressbarProps>('circleProgress', (data) => {
|
2022-03-15 15:42:54 +01:00
|
|
|
if (visible) return;
|
2022-03-15 21:08:08 +01:00
|
|
|
setCancelled(false);
|
2022-03-15 15:42:54 +01:00
|
|
|
setVisible(true);
|
|
|
|
|
setValue(0);
|
2022-08-27 15:58:36 +02:00
|
|
|
setLabel(data.label || '');
|
2022-03-15 15:42:54 +01:00
|
|
|
setProgressDuration(data.duration);
|
2022-08-27 15:58:36 +02:00
|
|
|
setPosition(data.position || 'middle');
|
2022-03-15 15:42:54 +01:00
|
|
|
const onePercent = data.duration * 0.01;
|
|
|
|
|
const updateProgress = setInterval(() => {
|
|
|
|
|
setValue((previousValue) => {
|
|
|
|
|
const newValue = previousValue + 1;
|
|
|
|
|
newValue >= 100 && clearInterval(updateProgress);
|
|
|
|
|
return newValue;
|
|
|
|
|
});
|
|
|
|
|
}, onePercent);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Flex
|
2022-08-27 15:58:36 +02:00
|
|
|
h={position === 'middle' ? '100%' : '20%'}
|
2022-03-15 15:42:54 +01:00
|
|
|
w="100%"
|
|
|
|
|
position="absolute"
|
|
|
|
|
bottom="0"
|
|
|
|
|
justifyContent="center"
|
|
|
|
|
alignItems="center"
|
|
|
|
|
>
|
2022-05-29 15:26:25 +02:00
|
|
|
{visible && (
|
2022-06-14 19:18:24 +02:00
|
|
|
<Flex alignItems="center" flexDirection="column">
|
2022-05-31 18:30:54 +01:00
|
|
|
<CircularProgress
|
|
|
|
|
value={value}
|
|
|
|
|
size="5rem"
|
|
|
|
|
trackColor="rgba(0, 0, 0, 0.6)"
|
|
|
|
|
onAnimationEnd={progressComplete}
|
|
|
|
|
thickness={6}
|
2022-08-27 15:58:36 +02:00
|
|
|
color={cancelled ? 'rgb(198, 40, 40)' : 'white'}
|
2022-05-31 18:30:54 +01:00
|
|
|
sx={
|
|
|
|
|
!cancelled
|
|
|
|
|
? {
|
2022-08-27 15:58:36 +02:00
|
|
|
'.chakra-progress__indicator': {
|
|
|
|
|
transition: 'none !important',
|
|
|
|
|
animation: 'progress linear forwards !important',
|
2022-05-31 18:30:54 +01:00
|
|
|
animationDuration: `${progressDuration}ms !important`,
|
2022-08-27 15:58:36 +02:00
|
|
|
opacity: '1 !important',
|
2022-05-31 18:30:54 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
: {
|
2022-06-14 19:18:24 +02:00
|
|
|
// Currently unused
|
2022-08-27 15:58:36 +02:00
|
|
|
'.chakra-progress__indicator': {
|
|
|
|
|
transition: 'none !important',
|
|
|
|
|
strokeDasharray: '264, 0 !important', // sets circle to full
|
2022-05-31 18:30:54 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
>
|
2022-08-27 15:58:36 +02:00
|
|
|
<CircularProgressLabel fontFamily="Fira Mono">{value}%</CircularProgressLabel>
|
2022-05-31 18:30:54 +01:00
|
|
|
</CircularProgress>
|
2022-10-27 16:44:21 +02:00
|
|
|
<Text fontFamily="Inter" fontSize={18} fontWeight="light">
|
2022-05-31 18:30:54 +01:00
|
|
|
{label}
|
|
|
|
|
</Text>
|
|
|
|
|
</Flex>
|
2022-05-29 15:26:25 +02:00
|
|
|
)}
|
2022-03-15 15:42:54 +01:00
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default CircleProgressbar;
|