misc_pterodactyl-panel/resources/scripts/components/elements/ProgressBar.tsx

79 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

2022-11-25 20:25:03 +00:00
import { Transition } from '@headlessui/react';
import { useStoreActions, useStoreState } from 'easy-peasy';
2022-11-25 20:25:03 +00:00
import { Fragment, useEffect, useRef, useState } from 'react';
2022-11-25 20:25:03 +00:00
import { randomInt } from '@/helpers';
2022-06-26 19:30:05 +00:00
type Timer = ReturnType<typeof setTimeout>;
2022-11-25 20:25:03 +00:00
function ProgressBar() {
const interval = useRef<Timer>();
const timeout = useRef<Timer>();
const [visible, setVisible] = useState(false);
2022-11-25 20:25:03 +00:00
const continuous = useStoreState(state => state.progress.continuous);
const progress = useStoreState(state => state.progress.progress);
const setProgress = useStoreActions(actions => actions.progress.setProgress);
useEffect(() => {
return () => {
timeout.current && clearTimeout(timeout.current);
interval.current && clearInterval(interval.current);
};
}, []);
useEffect(() => {
setVisible((progress || 0) > 0);
if (progress === 100) {
timeout.current = setTimeout(() => setProgress(undefined), 500);
}
}, [progress]);
useEffect(() => {
if (!continuous) {
interval.current && clearInterval(interval.current);
return;
}
if (!progress || progress === 0) {
setProgress(randomInt(20, 30));
}
}, [continuous]);
useEffect(() => {
if (continuous) {
interval.current && clearInterval(interval.current);
if ((progress || 0) >= 90) {
setProgress(90);
} else {
2022-06-26 19:30:05 +00:00
interval.current = setTimeout(() => setProgress((progress || 0) + randomInt(1, 5)), 500);
}
}
}, [progress, continuous]);
return (
2022-11-25 20:25:03 +00:00
<div className="fixed h-[2px] w-full">
<Transition
as={Fragment}
show={visible}
enter="transition-opacity duration-150"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="transition-opacity duration-150"
leaveFrom="opacity-100"
leaveTo="opacity-0"
appear
unmount
>
<div
className="h-full bg-cyan-400 shadow-[0_-2px_10px_2px] shadow-[#3CE7E1] transition-all duration-[250ms] ease-in-out"
style={{ width: progress === undefined ? '100%' : `${progress}%` }}
/>
</Transition>
</div>
);
2022-11-25 20:25:03 +00:00
}
export default ProgressBar;