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

103 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-03-15 20:35:02 +01:00
import React from "react";
import {
Progress,
ProgressLabel,
Flex,
Box,
ScaleFade,
} from "@chakra-ui/react";
import { useNuiEvent } from "../../hooks/useNuiEvent";
import { debugData } from "../../utils/debugData";
import { fetchNui } from "../../utils/fetchNui";
2022-03-15 20:35:02 +01:00
interface Props {
label: string;
duration: number;
}
debugData([
{
action: "progress",
data: {
2022-03-19 12:57:49 +01:00
label: "Using Lockpick",
2022-03-15 20:35:02 +01:00
duration: 8000,
},
},
]);
const Progressbar: React.FC = () => {
const [visible, setVisible] = React.useState(false);
const [label, setLabel] = React.useState("");
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);
fetchNui("progressComplete");
};
2022-03-15 21:08:08 +01:00
const progressCancel = () => {
setCancelled(true);
setTimeout(() => {
setVisible(false);
}, 2500);
};
useNuiEvent("progressCancel", progressCancel);
2022-03-15 20:35:02 +01:00
useNuiEvent<Props>("progress", (data) => {
if (visible) return;
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 (
<Flex
h="20%"
w="100%"
position="absolute"
bottom="0"
justifyContent="center"
alignItems="center"
>
<Box width="22rem">
<ScaleFade in={visible} unmountOnExit>
<Progress
value={100}
2022-03-19 12:57:49 +01:00
height="2.8rem"
borderRadius="sm"
2022-03-19 12:57:49 +01:00
fontFamily="Inter"
2022-03-15 20:35:02 +01:00
flex="1 1 auto"
boxShadow="lg"
2022-03-15 20:35:02 +01:00
onAnimationEnd={progressComplete}
2022-03-15 21:08:08 +01:00
sx={
!cancelled
? {
// really scuffed solution but works, wonder if there's a better way to do this?
"> div:first-of-type": {
animation: `progress-bar linear ${duration}ms`,
borderRadius: "none",
2022-03-15 21:08:08 +01:00
},
}
: {
"> div:first-of-type": {
width: "100%",
backgroundColor: "rgb(198, 40, 40)",
},
}
}
2022-03-15 20:35:02 +01:00
>
<ProgressLabel fontSize={22} fontWeight="light">
{label}
</ProgressLabel>
2022-03-15 20:35:02 +01:00
</Progress>
</ScaleFade>
</Box>
</Flex>
);
};
export default Progressbar;