// @flow import React, { useCallback } from 'react'; import Button from 'component/button'; import UriIndicator from 'component/uriIndicator'; import I18nMessage from 'component/i18nMessage'; import { formatLbryUrlForWeb } from 'util/url'; import { withRouter } from 'react-router'; type Props = { history: { push: string => void }, nextRecommendedClaim: ?StreamClaim, nextRecommendedUri: string, isFloating: boolean, doSetPlayingUri: (string | null) => void, doPlayUri: string => void, }; function AutoplayCountdown(props: Props) { const { nextRecommendedUri, nextRecommendedClaim, doSetPlayingUri, doPlayUri, isFloating, history: { push }, } = props; const nextTitle = nextRecommendedClaim && nextRecommendedClaim.value && nextRecommendedClaim.value.title; /* this value is coupled with CSS timing variables on .autoplay-countdown__timer */ const countdownTime = 5; const [timer, setTimer] = React.useState(countdownTime); const [timerCanceled, setTimerCanceled] = React.useState(false); let navigateUrl; if (nextTitle) { navigateUrl = formatLbryUrlForWeb(nextRecommendedUri); } const doNavigate = useCallback(() => { if (!isFloating) { if (navigateUrl) { push(navigateUrl); doSetPlayingUri(nextRecommendedUri); doPlayUri(nextRecommendedUri); } } else { if (nextRecommendedUri) { doSetPlayingUri(nextRecommendedUri); doPlayUri(nextRecommendedUri); } } }, [navigateUrl, nextRecommendedUri, isFloating, doSetPlayingUri, doPlayUri]); React.useEffect(() => { let interval; if (!timerCanceled && nextRecommendedUri) { interval = setInterval(() => { const newTime = timer - 1; if (newTime === 0) { doNavigate(); } else { setTimer(timer - 1); } }, 1000); } return () => { clearInterval(interval); }; }, [timer, doNavigate, navigateUrl, push, timerCanceled, nextRecommendedUri]); if (timerCanceled || !nextRecommendedUri) { return null; } return (