2020-04-10 19:41:08 +00:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2020-07-03 21:19:05 +00:00
|
|
|
import styled from 'styled-components/macro';
|
2020-04-10 19:41:08 +00:00
|
|
|
import { useStoreActions, useStoreState } from 'easy-peasy';
|
|
|
|
import { randomInt } from '@/helpers';
|
|
|
|
import { CSSTransition } from 'react-transition-group';
|
2020-07-03 21:19:05 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-04-10 19:41:08 +00:00
|
|
|
|
|
|
|
const BarFill = styled.div`
|
|
|
|
${tw`h-full bg-cyan-400`};
|
|
|
|
transition: 250ms ease-in-out;
|
|
|
|
box-shadow: 0 -2px 10px 2px hsl(178, 78%, 57%);
|
|
|
|
`;
|
|
|
|
|
2022-06-26 19:30:05 +00:00
|
|
|
type Timer = ReturnType<typeof setTimeout>;
|
|
|
|
|
2020-04-10 19:41:08 +00:00
|
|
|
export default () => {
|
2022-06-26 19:30:05 +00:00
|
|
|
const interval = useRef<Timer>(null) as React.MutableRefObject<Timer>;
|
|
|
|
const timeout = useRef<Timer>(null) as React.MutableRefObject<Timer>;
|
2022-06-26 19:13:52 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
const progress = useStoreState((state) => state.progress.progress);
|
|
|
|
const continuous = useStoreState((state) => state.progress.continuous);
|
|
|
|
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 (
|
2020-12-16 16:34:47 +00:00
|
|
|
<div css={tw`w-full fixed`} style={{ height: '2px' }}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<CSSTransition timeout={150} appear in={visible} unmountOnExit classNames={'fade'}>
|
|
|
|
<BarFill style={{ width: progress === undefined ? '100%' : `${progress}%` }} />
|
2020-04-10 19:41:08 +00:00
|
|
|
</CSSTransition>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|