Files
ox_lib/web/src/features/progress/Progressbar.tsx

88 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-08-27 15:58:36 +02:00
import React from 'react';
import { Progress, 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-03-15 20:35:02 +01:00
export interface ProgressbarProps {
2022-03-15 20:35:02 +01:00
label: string;
duration: number;
}
const useStyles = createStyles((theme) => ({
container: {
width: 350,
height: 45,
position: 'absolute',
bottom: '15%',
left: '50%',
transform: 'translate(-50%)',
borderRadius: theme.radius.xs,
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
bar: {
height: '100%',
borderRadius: theme.radius.xs,
backgroundColor: theme.colors[theme.primaryColor][theme.fn.primaryShade()],
},
label: {
maxWidth: 350,
padding: 8,
textOverflow: 'ellipsis',
overflow: 'hidden',
whiteSpace: 'nowrap',
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
fontSize: 20,
},
}));
2022-03-15 20:35:02 +01:00
const Progressbar: React.FC = () => {
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);
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 (
<>
{visible && (
<Box className={classes.container}>
<Box
className={classes.bar}
onAnimationEnd={progressComplete}
sx={{
width: '0%',
animation: 'progress-bar linear',
animationDuration: `${duration}ms`,
}}
/>
<Text className={classes.label}>{label}</Text>
</Box>
)}
</>
2022-03-15 20:35:02 +01:00
);
};
export default Progressbar;