lbry-desktop/ui/component/autoplayCountdown/view.jsx

97 lines
2.9 KiB
React
Raw Normal View History

2020-01-27 13:52:25 -05:00
// @flow
2020-04-16 17:43:09 -04:00
import React, { useCallback } from 'react';
2020-01-27 13:52:25 -05:00
import Button from 'component/button';
import UriIndicator from 'component/uriIndicator';
2020-01-27 15:02:58 -05:00
import I18nMessage from 'component/i18nMessage';
2020-01-27 13:52:25 -05:00
import { formatLbryUrlForWeb } from 'util/url';
import { withRouter } from 'react-router';
type Props = {
history: { push: string => void },
nextRecommendedClaim: ?StreamClaim,
nextRecommendedUri: string,
2020-04-13 19:48:11 -04:00
isFloating: boolean,
2020-01-27 14:32:20 -05:00
setPlayingUri: (string | null) => void,
2020-01-27 13:52:25 -05:00
};
function AutoplayCountdown(props: Props) {
const {
nextRecommendedUri,
nextRecommendedClaim,
2020-01-27 14:32:20 -05:00
setPlayingUri,
2020-04-13 19:48:11 -04:00
isFloating,
2020-01-27 13:52:25 -05:00
history: { push },
} = props;
const nextTitle = nextRecommendedClaim && nextRecommendedClaim.value && nextRecommendedClaim.value.title;
2020-04-13 19:48:11 -04:00
/* this value is coupled with CSS timing variables on .autoplay-countdown__timer */
2020-04-13 19:50:52 -04:00
const countdownTime = 5;
2020-04-13 19:48:11 -04:00
const [timer, setTimer] = React.useState(countdownTime);
2020-01-27 13:52:25 -05:00
const [timerCanceled, setTimerCanceled] = React.useState(false);
let navigateUrl;
if (nextTitle) {
navigateUrl = formatLbryUrlForWeb(nextRecommendedUri);
}
2020-04-16 17:43:09 -04:00
const doNavigate = useCallback(() => {
2020-04-13 19:48:11 -04:00
if (!isFloating) {
2020-04-16 17:43:09 -04:00
if (navigateUrl) {
push(navigateUrl);
2020-04-29 13:55:47 -04:00
setPlayingUri(nextRecommendedUri);
2020-04-16 17:43:09 -04:00
}
2020-04-13 19:48:11 -04:00
} else {
2020-04-16 17:43:09 -04:00
if (nextRecommendedUri) {
setPlayingUri(nextRecommendedUri);
}
2020-04-13 19:48:11 -04:00
}
2020-04-16 17:43:09 -04:00
}, [navigateUrl, nextRecommendedUri, isFloating, setPlayingUri]);
2020-04-13 19:48:11 -04:00
2020-01-27 13:52:25 -05:00
React.useEffect(() => {
let interval;
2020-04-16 17:43:09 -04:00
if (!timerCanceled && nextRecommendedUri) {
2020-01-27 13:52:25 -05:00
interval = setInterval(() => {
const newTime = timer - 1;
if (newTime === 0) {
2020-04-13 19:48:11 -04:00
doNavigate();
2020-01-27 13:52:25 -05:00
} else {
setTimer(timer - 1);
}
}, 1000);
}
return () => {
clearInterval(interval);
};
2020-04-13 19:48:11 -04:00
}, [timer, doNavigate, navigateUrl, push, timerCanceled, setPlayingUri, nextRecommendedUri]);
2020-01-27 13:52:25 -05:00
2020-04-13 19:48:11 -04:00
if (timerCanceled || !nextRecommendedUri) {
2020-01-27 13:52:25 -05:00
return null;
}
return (
2020-04-13 19:48:11 -04:00
<div className="file-viewer__overlay">
<div className="autoplay-countdown">
<div className="file-viewer__overlay-secondary">
<I18nMessage tokens={{ channel: <UriIndicator link uri={nextRecommendedUri} /> }}>
Up Next by %channel%
</I18nMessage>
2020-01-27 14:32:20 -05:00
</div>
2020-04-13 19:48:11 -04:00
<div className="file-viewer__overlay-title">{nextTitle}</div>
<div className="autoplay-countdown__timer">
<div className={'autoplay-countdown__button autoplay-countdown__button--' + (timer % 5)}>
<Button onClick={doNavigate} iconSize={30} title={__('Play')} className="button--icon button--play" />
</div>
<div className="file-viewer__overlay-secondary autoplay-countdown__counter">
2020-04-16 17:43:09 -04:00
{__('Playing in %seconds_left% seconds...', { seconds_left: timer })}{' '}
<Button label={__('Cancel')} button="link" onClick={() => setTimerCanceled(true)} />
2020-04-13 19:48:11 -04:00
</div>
2020-01-27 13:52:25 -05:00
</div>
</div>
</div>
);
}
export default withRouter(AutoplayCountdown);