2022-11-25 20:25:03 +00:00
|
|
|
import { Transition } from '@headlessui/react';
|
2020-04-10 19:41:08 +00:00
|
|
|
import { useStoreActions, useStoreState } from 'easy-peasy';
|
2022-11-25 20:25:03 +00:00
|
|
|
import { Fragment, useEffect, useRef, useState } from 'react';
|
2020-04-10 19:41:08 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
import { randomInt } from '@/helpers';
|
2020-04-10 19:41:08 +00:00
|
|
|
|
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>();
|
2022-06-26 19:13:52 +00:00
|
|
|
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);
|
2020-04-10 19:41:08 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [progress]);
|
2020-04-10 19:41:08 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!continuous) {
|
|
|
|
interval.current && clearInterval(interval.current);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!progress || progress === 0) {
|
|
|
|
setProgress(randomInt(20, 30));
|
|
|
|
}
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [continuous]);
|
2020-04-10 19:41:08 +00:00
|
|
|
|
|
|
|
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);
|
2020-04-10 19:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [progress, continuous]);
|
2020-04-10 19:41:08 +00:00
|
|
|
|
|
|
|
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>
|
2020-04-10 19:41:08 +00:00
|
|
|
</div>
|
|
|
|
);
|
2022-11-25 20:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ProgressBar;
|