You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.6 KiB
120 lines
3.6 KiB
4 years ago
|
import React, { useState, useEffect, useContext, useRef } from 'react';
|
||
|
import { useSelector } from 'react-redux';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import classnames from 'classnames';
|
||
|
import { Duration } from 'luxon';
|
||
|
import { I18nContext } from '../../../contexts/i18n';
|
||
|
import InfoTooltip from '../../../components/ui/info-tooltip';
|
||
|
import { getSwapsQuoteRefreshTime } from '../../../ducks/swaps/swaps';
|
||
4 years ago
|
|
||
|
// Return the mm:ss start time of the countdown timer.
|
||
|
// If time has elapsed between `timeStarted` the time current time,
|
||
|
// then that elapsed time will be subtracted from the timer before
|
||
|
// rendering
|
||
4 years ago
|
function getNewTimer(currentTime, timeStarted, timeBaseStart) {
|
||
4 years ago
|
const timeAlreadyElapsed = currentTime - timeStarted;
|
||
|
return timeBaseStart - timeAlreadyElapsed;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
function decreaseTimerByOne(timer) {
|
||
4 years ago
|
return Math.max(timer - 1000, 0);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
function timeBelowWarningTime(timer, warningTime) {
|
||
4 years ago
|
const [warningTimeMinutes, warningTimeSeconds] = warningTime.split(':');
|
||
4 years ago
|
return (
|
||
|
timer <=
|
||
|
(Number(warningTimeMinutes) * 60 + Number(warningTimeSeconds)) * 1000
|
||
4 years ago
|
);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
export default function CountdownTimer({
|
||
4 years ago
|
timeStarted,
|
||
|
timeOnly,
|
||
4 years ago
|
timerBase,
|
||
4 years ago
|
warningTime,
|
||
|
labelKey,
|
||
|
infoTooltipLabelKey,
|
||
|
}) {
|
||
4 years ago
|
const t = useContext(I18nContext);
|
||
|
const intervalRef = useRef();
|
||
|
const initialTimeStartedRef = useRef();
|
||
4 years ago
|
|
||
4 years ago
|
const swapsQuoteRefreshTime = useSelector(getSwapsQuoteRefreshTime);
|
||
|
const timerStart = Number(timerBase) || swapsQuoteRefreshTime;
|
||
4 years ago
|
|
||
4 years ago
|
const [currentTime, setCurrentTime] = useState(() => Date.now());
|
||
4 years ago
|
const [timer, setTimer] = useState(() =>
|
||
4 years ago
|
getNewTimer(currentTime, timeStarted, timerStart),
|
||
4 years ago
|
);
|
||
4 years ago
|
|
||
|
useEffect(() => {
|
||
|
if (intervalRef.current === undefined) {
|
||
|
intervalRef.current = setInterval(() => {
|
||
4 years ago
|
setTimer(decreaseTimerByOne);
|
||
|
}, 1000);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
return function cleanup() {
|
||
4 years ago
|
clearInterval(intervalRef.current);
|
||
|
};
|
||
|
}, []);
|
||
4 years ago
|
|
||
|
// Reset the timer that timer has hit '0:00' and the timeStarted prop has changed
|
||
|
useEffect(() => {
|
||
|
if (!initialTimeStartedRef.current) {
|
||
4 years ago
|
initialTimeStartedRef.current = timeStarted || Date.now();
|
||
4 years ago
|
}
|
||
|
|
||
|
if (timer === 0 && timeStarted !== initialTimeStartedRef.current) {
|
||
4 years ago
|
initialTimeStartedRef.current = timeStarted;
|
||
|
const newCurrentTime = Date.now();
|
||
|
setCurrentTime(newCurrentTime);
|
||
|
setTimer(getNewTimer(newCurrentTime, timeStarted, timerStart));
|
||
4 years ago
|
|
||
4 years ago
|
clearInterval(intervalRef.current);
|
||
4 years ago
|
intervalRef.current = setInterval(() => {
|
||
4 years ago
|
setTimer(decreaseTimerByOne);
|
||
|
}, 1000);
|
||
4 years ago
|
}
|
||
4 years ago
|
}, [timeStarted, timer, timerStart]);
|
||
4 years ago
|
|
||
4 years ago
|
const formattedTimer = Duration.fromMillis(timer).toFormat('m:ss');
|
||
|
let time;
|
||
4 years ago
|
if (timeOnly) {
|
||
4 years ago
|
time = <div className="countdown-timer__time">{formattedTimer}</div>;
|
||
4 years ago
|
} else if (labelKey) {
|
||
4 years ago
|
time = t(labelKey, [
|
||
|
<div key="countdown-time-1" className="countdown-timer__time">
|
||
|
{formattedTimer}
|
||
|
</div>,
|
||
4 years ago
|
]);
|
||
4 years ago
|
}
|
||
|
|
||
|
return (
|
||
|
<div className="countdown-timer">
|
||
|
<div
|
||
4 years ago
|
data-testid="countdown-timer__timer-container"
|
||
4 years ago
|
className={classnames('countdown-timer__timer-container', {
|
||
4 years ago
|
'countdown-timer__timer-container--warning':
|
||
|
warningTime && timeBelowWarningTime(timer, warningTime),
|
||
4 years ago
|
})}
|
||
|
>
|
||
|
{time}
|
||
|
</div>
|
||
4 years ago
|
{!timeOnly && infoTooltipLabelKey ? (
|
||
|
<InfoTooltip position="bottom" contentText={t(infoTooltipLabelKey)} />
|
||
|
) : null}
|
||
4 years ago
|
</div>
|
||
4 years ago
|
);
|
||
4 years ago
|
}
|
||
|
|
||
|
CountdownTimer.propTypes = {
|
||
|
timeStarted: PropTypes.number,
|
||
|
timeOnly: PropTypes.bool,
|
||
|
timerBase: PropTypes.number,
|
||
|
warningTime: PropTypes.string,
|
||
|
labelKey: PropTypes.string,
|
||
|
infoTooltipLabelKey: PropTypes.string,
|
||
4 years ago
|
};
|