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

95 lines
2.8 KiB
React
Raw Normal View History

2020-01-27 19:52:25 +01:00
// @flow
import React from 'react';
import Button from 'component/button';
import UriIndicator from 'component/uriIndicator';
2020-01-27 21:02:58 +01:00
import I18nMessage from 'component/i18nMessage';
2020-01-27 19:52:25 +01:00
import { formatLbryUrlForWeb } from 'util/url';
import { withRouter } from 'react-router';
type Props = {
history: { push: string => void },
nextRecommendedClaim: ?StreamClaim,
nextRecommendedUri: string,
2020-04-14 01:48:11 +02:00
isFloating: boolean,
2020-01-27 20:32:20 +01:00
setPlayingUri: (string | null) => void,
2020-01-27 19:52:25 +01:00
};
function AutoplayCountdown(props: Props) {
const {
nextRecommendedUri,
nextRecommendedClaim,
2020-01-27 20:32:20 +01:00
setPlayingUri,
2020-04-14 01:48:11 +02:00
isFloating,
2020-01-27 19:52:25 +01:00
history: { push },
} = props;
const nextTitle = nextRecommendedClaim && nextRecommendedClaim.value && nextRecommendedClaim.value.title;
2020-04-14 01:48:11 +02:00
/* this value is coupled with CSS timing variables on .autoplay-countdown__timer */
2020-04-14 01:50:52 +02:00
const countdownTime = 5;
2020-04-14 01:48:11 +02:00
const [timer, setTimer] = React.useState(countdownTime);
2020-01-27 19:52:25 +01:00
const [timerCanceled, setTimerCanceled] = React.useState(false);
let navigateUrl;
if (nextTitle) {
navigateUrl = formatLbryUrlForWeb(nextRecommendedUri);
}
2020-04-14 01:48:11 +02:00
function doNavigate() {
// FIXME: make autoplay continue in floating player
if (!isFloating) {
// if not floating
setPlayingUri(null);
push(navigateUrl);
} else {
setPlayingUri(nextRecommendedUri);
}
}
2020-01-27 19:52:25 +01:00
React.useEffect(() => {
let interval;
if (!timerCanceled) {
interval = setInterval(() => {
const newTime = timer - 1;
if (newTime === 0) {
2020-04-14 01:48:11 +02:00
doNavigate();
2020-01-27 19:52:25 +01:00
} else {
setTimer(timer - 1);
}
}, 1000);
}
return () => {
clearInterval(interval);
};
2020-04-14 01:48:11 +02:00
}, [timer, doNavigate, navigateUrl, push, timerCanceled, setPlayingUri, nextRecommendedUri]);
2020-01-27 19:52:25 +01:00
2020-04-14 01:48:11 +02:00
if (timerCanceled || !nextRecommendedUri) {
2020-01-27 19:52:25 +01:00
return null;
}
return (
2020-04-14 01:48:11 +02: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 20:32:20 +01:00
</div>
2020-04-14 01:48:11 +02: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">
{__('Playing in %seconds_left% seconds', { seconds_left: timer })}
</div>
2020-01-27 19:52:25 +01:00
</div>
2020-04-14 01:48:11 +02:00
<Button label={__('Cancel')} button="link" onClick={() => setTimerCanceled(true)} />
2020-01-27 19:52:25 +01:00
</div>
</div>
);
}
export default withRouter(AutoplayCountdown);