Files
ox_lib/web/src/features/progress/CircleProgressbar.tsx
T

100 lines
3.1 KiB
TypeScript
Raw Normal View History

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 {
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
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 && (
<Flex alignItems="center" flexDirection="column">
<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'}
sx={
!cancelled
? {
2022-08-27 15:58:36 +02:00
'.chakra-progress__indicator': {
transition: 'none !important',
animation: 'progress linear forwards !important',
animationDuration: `${progressDuration}ms !important`,
2022-08-27 15:58:36 +02:00
opacity: '1 !important',
},
}
: {
// 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-08-27 15:58:36 +02:00
<CircularProgressLabel fontFamily="Fira Mono">{value}%</CircularProgressLabel>
</CircularProgress>
<Text fontFamily="Inter" isTruncated fontSize={18} fontWeight="light">
{label}
</Text>
</Flex>
2022-05-29 15:26:25 +02:00
)}
2022-03-15 15:42:54 +01:00
</Flex>
);
};
export default CircleProgressbar;