2022-08-27 15:58:36 +02:00
|
|
|
import React from 'react';
|
2023-01-02 14:02:07 +01:00
|
|
|
import { Box, Text, createStyles } from '@mantine/core';
|
2022-08-27 15:58:36 +02:00
|
|
|
import { useNuiEvent } from '../../hooks/useNuiEvent';
|
|
|
|
|
import { fetchNui } from '../../utils/fetchNui';
|
2022-12-31 15:52:39 +01:00
|
|
|
import ScaleFade from '../../transitions/ScaleFade';
|
2022-03-15 20:35:02 +01:00
|
|
|
|
2022-08-27 15:40:41 +02:00
|
|
|
export interface ProgressbarProps {
|
2022-03-15 20:35:02 +01:00
|
|
|
label: string;
|
|
|
|
|
duration: number;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 13:17:42 +01:00
|
|
|
const useStyles = createStyles((theme) => ({
|
|
|
|
|
container: {
|
|
|
|
|
width: 350,
|
|
|
|
|
height: 45,
|
|
|
|
|
borderRadius: theme.radius.xs,
|
2022-12-31 15:59:14 +01:00
|
|
|
backgroundColor: theme.colors.dark[5],
|
2022-12-18 13:17:42 +01:00
|
|
|
},
|
2022-12-31 15:52:39 +01:00
|
|
|
wrapper: {
|
|
|
|
|
width: '100%',
|
|
|
|
|
height: '20%',
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
bottom: 0,
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
},
|
2022-12-18 13:17:42 +01:00
|
|
|
bar: {
|
|
|
|
|
height: '100%',
|
|
|
|
|
borderRadius: theme.radius.xs,
|
|
|
|
|
backgroundColor: theme.colors[theme.primaryColor][theme.fn.primaryShade()],
|
|
|
|
|
},
|
2022-12-31 15:52:39 +01:00
|
|
|
labelWrapper: {
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
display: 'flex',
|
|
|
|
|
width: 350,
|
|
|
|
|
height: 45,
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
},
|
2022-12-18 13:17:42 +01:00
|
|
|
label: {
|
|
|
|
|
maxWidth: 350,
|
|
|
|
|
padding: 8,
|
|
|
|
|
textOverflow: 'ellipsis',
|
|
|
|
|
overflow: 'hidden',
|
|
|
|
|
whiteSpace: 'nowrap',
|
|
|
|
|
fontSize: 20,
|
2023-01-02 14:02:07 +01:00
|
|
|
color: theme.colors.gray[3],
|
2022-12-31 15:52:39 +01:00
|
|
|
textShadow: theme.shadows.sm,
|
2022-12-18 13:17:42 +01:00
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2022-03-15 20:35:02 +01:00
|
|
|
const Progressbar: React.FC = () => {
|
2022-12-18 13:17:42 +01:00
|
|
|
const { classes } = useStyles();
|
2022-03-15 20:35:02 +01:00
|
|
|
const [visible, setVisible] = React.useState(false);
|
2022-08-27 15:58:36 +02:00
|
|
|
const [label, setLabel] = React.useState('');
|
2022-03-15 20:35:02 +01:00
|
|
|
const [duration, setDuration] = React.useState(0);
|
2022-03-15 21:08:08 +01:00
|
|
|
const [cancelled, setCancelled] = React.useState(false);
|
2022-03-15 20:35:02 +01:00
|
|
|
|
|
|
|
|
const progressComplete = () => {
|
|
|
|
|
setVisible(false);
|
2022-08-27 15:58:36 +02:00
|
|
|
fetchNui('progressComplete');
|
2022-03-15 20:35:02 +01:00
|
|
|
};
|
|
|
|
|
|
2022-03-15 21:08:08 +01:00
|
|
|
const progressCancel = () => {
|
|
|
|
|
setCancelled(true);
|
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<ProgressbarProps>('progress', (data) => {
|
2022-03-15 21:08:08 +01:00
|
|
|
setCancelled(false);
|
2022-03-15 20:35:02 +01:00
|
|
|
setVisible(true);
|
|
|
|
|
setLabel(data.label);
|
|
|
|
|
setDuration(data.duration);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
2022-12-18 13:17:42 +01:00
|
|
|
<>
|
2022-12-31 15:52:39 +01:00
|
|
|
<Box className={classes.wrapper}>
|
|
|
|
|
<ScaleFade visible={visible}>
|
|
|
|
|
<Box className={classes.container}>
|
|
|
|
|
<Box
|
|
|
|
|
className={classes.bar}
|
|
|
|
|
onAnimationEnd={progressComplete}
|
|
|
|
|
sx={{
|
|
|
|
|
animation: 'progress-bar linear',
|
|
|
|
|
animationDuration: `${duration}ms`,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box className={classes.labelWrapper}>
|
|
|
|
|
<Text className={classes.label}>{label}</Text>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</ScaleFade>
|
|
|
|
|
</Box>
|
2022-12-18 13:17:42 +01:00
|
|
|
</>
|
2022-03-15 20:35:02 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Progressbar;
|