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

111 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-03-15 15:42:54 +01:00
import React from "react";
import {
CircularProgress,
CircularProgressLabel,
Flex,
ScaleFade,
} from "@chakra-ui/react";
import { useNuiEvent } from "../../hooks/useNuiEvent";
import { debugData } from "../../utils/debugData";
import { fetchNui } from "../../utils/fetchNui";
2022-03-15 15:42:54 +01:00
interface Props {
duration: number;
position?: "middle" | "bottom";
percent?: boolean;
2022-03-15 15:42:54 +01:00
}
2022-03-19 12:57:49 +01:00
debugData([
{
action: "circleProgress",
data: {
duration: 8000,
},
},
]);
2022-03-15 15:42:54 +01:00
const CircleProgressbar: React.FC = () => {
const [visible, setVisible] = React.useState(false);
const [progressDuration, setProgressDuration] = React.useState(0);
const [position, setPosition] = React.useState<"middle" | "bottom">("middle");
2022-03-15 15:42:54 +01:00
const [value, setValue] = React.useState(0);
2022-03-15 21:08:08 +01:00
const [cancelled, setCancelled] = React.useState(false);
2022-03-15 15:42:54 +01:00
const progressComplete = () => {
setVisible(false);
fetchNui("progressComplete");
};
2022-03-15 21:08:08 +01:00
const progressCancel = () => {
setCancelled(true);
setValue(99); // Sets the final value to 100% kek
setTimeout(() => {
setVisible(false);
}, 2500);
};
useNuiEvent("progressCancel", progressCancel);
2022-03-15 15:42:54 +01:00
useNuiEvent<Props>("circleProgress", (data) => {
if (visible) return;
2022-03-15 21:08:08 +01:00
setCancelled(false);
2022-03-15 15:42:54 +01:00
setVisible(true);
setValue(0);
setProgressDuration(data.duration);
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 (
<Flex
h={position === "middle" ? "100%" : "20%"}
2022-03-15 15:42:54 +01:00
w="100%"
position="absolute"
bottom="0"
justifyContent="center"
alignItems="center"
>
<ScaleFade in={visible} unmountOnExit>
<CircularProgress
value={value}
size="5rem"
trackColor="rgba(0, 0, 0, 0.6)"
2022-03-15 15:42:54 +01:00
onAnimationEnd={progressComplete}
thickness={6}
color={cancelled ? "rgb(198, 40, 40)" : "white"}
2022-03-15 21:08:08 +01:00
sx={
!cancelled
? {
".chakra-progress__indicator": {
transition: "none !important",
animation: "progress linear forwards !important",
animationDuration: `${progressDuration}ms !important`,
opacity: "1 !important",
},
}
: {
".chakra-progress__indicator": {
transition: "none !important",
strokeDasharray: "264, 0 !important", // sets circle to full
},
}
}
2022-03-15 15:42:54 +01:00
>
<CircularProgressLabel fontFamily="Fira Mono">
{value}%
</CircularProgressLabel>
2022-03-15 15:42:54 +01:00
</CircularProgress>
</ScaleFade>
</Flex>
);
};
export default CircleProgressbar;