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

105 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-08-27 15:58:36 +02:00
import React from 'react';
import { RingProgress, Text, useMantineTheme, keyframes, Stack, createStyles } from '@mantine/core';
2022-08-27 15:58:36 +02:00
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
}
// 33.5 is the r of the circle
const progressCircle = keyframes({
'0%': { strokeDasharray: `0, ${33.5 * 2 * Math.PI}` },
'100%': { strokeDasharray: `${33.5 * 2 * Math.PI}, 0` },
});
const useStyles = createStyles((theme, params: { position: 'middle' | 'bottom'; duration: number }) => ({
container: {
position: 'absolute',
top: params.position === 'middle' ? '50%' : undefined,
bottom: params.position === 'bottom' ? '20%' : undefined,
left: '50%',
transform: 'translate(-50%, -50%)',
justifyContent: 'center',
alignItems: 'center',
},
progress: {
// Scuffed way of grabbing the first section and animating it
'> svg > circle:nth-child(2)': {
transition: 'none',
animation: `${progressCircle} linear forwards`,
animationDuration: `${params.duration}ms`,
},
},
value: {
textAlign: 'center',
fontFamily: 'roboto-mono',
textShadow: theme.shadows.sm,
},
label: {
textAlign: 'center',
textShadow: theme.shadows.sm,
},
}));
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('');
const theme = useMantineTheme();
const { classes } = useStyles({ position, duration: progressDuration });
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 = () => {
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;
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 (
<>
2022-05-29 15:26:25 +02:00
{visible && (
<Stack spacing={0} className={classes.container} bottom="0">
<RingProgress
size={90}
thickness={7}
sections={[{ value: 0, color: theme.primaryColor }]}
onAnimationEnd={progressComplete}
className={classes.progress}
label={<Text className={classes.value}>{value}%</Text>}
/>
{label && <Text className={classes.label}>{label}</Text>}
</Stack>
2022-05-29 15:26:25 +02:00
)}
</>
2022-03-15 15:42:54 +01:00
);
};
export default CircleProgressbar;