feat(nui): Progress bar cancel

This commit is contained in:
Luke
2022-03-15 21:08:08 +01:00
parent ae073881c3
commit be1048271a
2 changed files with 56 additions and 14 deletions

View File

@@ -26,14 +26,26 @@ const CircleProgressbar: React.FC = () => {
const [visible, setVisible] = React.useState(false); const [visible, setVisible] = React.useState(false);
const [progressDuration, setProgressDuration] = React.useState(0); const [progressDuration, setProgressDuration] = React.useState(0);
const [value, setValue] = React.useState(0); const [value, setValue] = React.useState(0);
const [cancelled, setCancelled] = React.useState(false);
const progressComplete = () => { const progressComplete = () => {
setVisible(false); setVisible(false);
fetchNui("progressComplete"); fetchNui("progressComplete");
}; };
const progressCancel = () => {
setCancelled(true);
setValue(99); // Sets the final value to 100% kek
setTimeout(() => {
setVisible(false);
}, 2500);
};
useNuiEvent("progressCancel", progressCancel);
useNuiEvent<Props>("circleProgress", (data) => { useNuiEvent<Props>("circleProgress", (data) => {
if (visible) return; if (visible) return;
setCancelled(false);
setVisible(true); setVisible(true);
setValue(0); setValue(0);
setProgressDuration(data.duration); setProgressDuration(data.duration);
@@ -61,14 +73,24 @@ const CircleProgressbar: React.FC = () => {
value={value} value={value}
size="7rem" size="7rem"
onAnimationEnd={progressComplete} onAnimationEnd={progressComplete}
sx={{ color={cancelled ? "rgb(198, 40, 40)" : "blue.300"}
".chakra-progress__indicator": { sx={
transition: "none !important", !cancelled
animation: "progress linear forwards !important", ? {
animationDuration: `${progressDuration}ms !important`, ".chakra-progress__indicator": {
opacity: "1 !important", 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
},
}
}
> >
<CircularProgressLabel color="black">{value}%</CircularProgressLabel> <CircularProgressLabel color="black">{value}%</CircularProgressLabel>
</CircularProgress> </CircularProgress>

View File

@@ -29,14 +29,25 @@ const Progressbar: React.FC = () => {
const [visible, setVisible] = React.useState(false); const [visible, setVisible] = React.useState(false);
const [label, setLabel] = React.useState(""); const [label, setLabel] = React.useState("");
const [duration, setDuration] = React.useState(0); const [duration, setDuration] = React.useState(0);
const [cancelled, setCancelled] = React.useState(false);
const progressComplete = () => { const progressComplete = () => {
setVisible(false); setVisible(false);
fetchNui("progressComplete"); fetchNui("progressComplete");
}; };
const progressCancel = () => {
setCancelled(true);
setTimeout(() => {
setVisible(false);
}, 2500);
};
useNuiEvent("progressCancel", progressCancel);
useNuiEvent<Props>("progress", (data) => { useNuiEvent<Props>("progress", (data) => {
if (visible) return; if (visible) return;
setCancelled(false);
setVisible(true); setVisible(true);
setLabel(data.label); setLabel(data.label);
setDuration(data.duration); setDuration(data.duration);
@@ -59,12 +70,21 @@ const Progressbar: React.FC = () => {
height="3rem" height="3rem"
flex="1 1 auto" flex="1 1 auto"
onAnimationEnd={progressComplete} onAnimationEnd={progressComplete}
sx={{ sx={
// really scuffed solution but works, wonder if there's a better way to do this? !cancelled
"> div:first-of-type": { ? {
animation: `progress-bar linear ${duration}ms`, // 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`,
},
}
: {
"> div:first-of-type": {
width: "100%",
backgroundColor: "rgb(198, 40, 40)",
},
}
}
> >
<ProgressLabel fontSize={22}>{label}</ProgressLabel> <ProgressLabel fontSize={22}>{label}</ProgressLabel>
</Progress> </Progress>