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%);
|
|
|
|
`;
|
|
|
|
|
|
|
|
export default () => {
|
2021-07-20 21:29:22 +00:00
|
|
|
const interval = useRef<NodeJS.Timer>();
|
|
|
|
const timeout = useRef<NodeJS.Timer>();
|
2020-04-10 19:41:08 +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);
|
|
|
|
|
|
|
|
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);
|
2021-07-20 21:29:22 +00:00
|
|
|
const p = progress || 0;
|
|
|
|
if (p >= 90) {
|
2020-04-10 19:41:08 +00:00
|
|
|
setProgress(90);
|
|
|
|
} else {
|
2021-07-20 21:29:22 +00:00
|
|
|
interval.current = setTimeout(() => setProgress(p + randomInt(1, 5)), 500);
|
2020-04-10 19:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [ progress, continuous ]);
|
|
|
|
|
|
|
|
return (
|
2021-07-20 21:29:22 +00:00
|
|
|
<div css={tw`w-full h-[2px] fixed z-10`}>
|
2020-04-10 19:41:08 +00:00
|
|
|
<CSSTransition
|
2020-07-05 20:56:04 +00:00
|
|
|
timeout={150}
|
2020-07-05 01:30:50 +00:00
|
|
|
appear
|
2020-04-10 19:41:08 +00:00
|
|
|
in={visible}
|
2020-07-05 01:30:50 +00:00
|
|
|
unmountOnExit
|
2020-04-10 19:41:08 +00:00
|
|
|
classNames={'fade'}
|
|
|
|
>
|
|
|
|
<BarFill style={{ width: progress === undefined ? '100%' : `${progress}%` }}/>
|
|
|
|
</CSSTransition>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|