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

90 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-08-27 15:58:36 +02:00
import React from 'react';
import { Text, Flex, Box } from '@chakra-ui/react';
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 Progressbar: React.FC = () => {
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 (
2022-08-27 15:58:36 +02:00
<Flex h="30%" w="100%" position="absolute" bottom="0" justifyContent="center" alignItems="center">
<Box width={350}>
{visible && (
<Box
height={45}
bg="rgba(0, 0, 0, 0.6)"
textAlign="center"
borderRadius="sm"
boxShadow="lg"
overflow="hidden"
>
<Box
height={45}
onAnimationEnd={progressComplete}
sx={
!cancelled
? {
2022-08-27 15:58:36 +02:00
width: '0%',
backgroundColor: 'green.400',
animation: 'progress-bar linear',
animationDuration: `${duration}ms`,
}
: {
// Currently unused
2022-08-27 15:58:36 +02:00
width: '100%',
animationPlayState: 'paused',
backgroundColor: 'rgb(198, 40, 40)',
}
}
/>
<Text
maxWidth={350}
fontFamily="Inter"
textOverflow="ellipsis"
overflow="hidden"
whiteSpace="nowrap"
fontSize={22}
fontWeight="light"
position="absolute"
top="50%"
left="50%"
transform="translate(-50%, -50%)"
>
{label}
</Text>
</Box>
)}
2022-03-15 20:35:02 +01:00
</Box>
</Flex>
);
};
export default Progressbar;