2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2019-10-22 19:57:32 +02:00
|
|
|
import * as PAGES from 'constants/pages';
|
2019-10-15 06:20:12 +02:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2019-10-09 18:34:18 +02:00
|
|
|
import classnames from 'classnames';
|
2019-07-22 04:28:49 +02:00
|
|
|
import analytics from 'analytics';
|
2019-11-01 20:17:06 +01:00
|
|
|
import { buildURI, parseURI, TX_LIST } from 'lbry-redux';
|
2017-12-21 22:08:54 +01:00
|
|
|
import Router from 'component/router/index';
|
|
|
|
import ModalRouter from 'modal/modalRouter';
|
|
|
|
import ReactModal from 'react-modal';
|
2019-01-23 16:38:40 +01:00
|
|
|
import { openContextMenu } from 'util/context-menu';
|
2019-06-11 20:10:58 +02:00
|
|
|
import useKonamiListener from 'util/enhanced-layout';
|
2019-03-05 05:46:57 +01:00
|
|
|
import Yrbl from 'component/yrbl';
|
2020-01-06 19:32:35 +01:00
|
|
|
import FloatingViewer from 'component/floatingViewer';
|
2019-08-13 07:35:13 +02:00
|
|
|
import { withRouter } from 'react-router';
|
2019-09-27 20:56:15 +02:00
|
|
|
import usePrevious from 'effects/use-previous';
|
2019-12-06 22:12:48 +01:00
|
|
|
import Nag from 'component/common/nag';
|
2020-02-21 22:05:20 +01:00
|
|
|
import Button from 'component/button';
|
|
|
|
import I18nMessage from 'component/i18nMessage';
|
2020-01-14 21:44:07 +01:00
|
|
|
import { rewards as REWARDS } from 'lbryinc';
|
2020-02-21 21:44:58 +01:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2020-02-21 22:05:20 +01:00
|
|
|
import useIsMobile from 'effects/use-is-mobile';
|
2019-12-05 06:24:54 +01:00
|
|
|
// @if TARGET='web'
|
2019-12-06 22:12:48 +01:00
|
|
|
import OpenInAppLink from 'component/openInAppLink';
|
2019-12-05 06:24:54 +01:00
|
|
|
import YoutubeWelcome from 'component/youtubeWelcome';
|
|
|
|
// @endif
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2019-06-27 08:18:45 +02:00
|
|
|
export const MAIN_WRAPPER_CLASS = 'main-wrapper';
|
2019-10-15 18:21:49 +02:00
|
|
|
// @if TARGET='app'
|
|
|
|
export const IS_MAC = process.platform === 'darwin';
|
|
|
|
// @endif
|
2019-10-15 23:23:51 +02:00
|
|
|
const SYNC_INTERVAL = 1000 * 60 * 5; // 5 minutes
|
2019-06-27 08:18:45 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
|
|
|
alertError: (string | {}) => void,
|
|
|
|
pageTitle: ?string,
|
2019-06-07 23:10:47 +02:00
|
|
|
language: string,
|
2019-11-08 21:51:42 +01:00
|
|
|
languages: Array<string>,
|
2018-10-19 17:27:14 +02:00
|
|
|
theme: string,
|
2019-08-14 18:28:13 +02:00
|
|
|
user: ?{ id: string, has_verified_email: boolean, is_reward_approved: boolean },
|
2020-01-14 21:44:07 +01:00
|
|
|
location: { pathname: string, hash: string, search: string },
|
2019-10-22 19:57:32 +02:00
|
|
|
history: { push: string => void },
|
2019-06-11 20:10:58 +02:00
|
|
|
fetchRewards: () => void,
|
2019-11-01 20:17:06 +01:00
|
|
|
fetchTransactions: (number, number) => void,
|
2019-08-06 18:53:59 +02:00
|
|
|
fetchAccessToken: () => void,
|
2019-09-26 18:28:08 +02:00
|
|
|
fetchChannelListMine: () => void,
|
2019-10-01 06:53:33 +02:00
|
|
|
signIn: () => void,
|
2019-09-23 03:56:43 +02:00
|
|
|
requestDownloadUpgrade: () => void,
|
2019-09-26 18:07:11 +02:00
|
|
|
onSignedIn: () => void,
|
2019-11-08 21:51:42 +01:00
|
|
|
setLanguage: string => void,
|
2019-09-26 18:28:08 +02:00
|
|
|
isUpgradeAvailable: boolean,
|
|
|
|
autoUpdateDownloaded: boolean,
|
2019-10-15 23:23:51 +02:00
|
|
|
checkSync: () => void,
|
2019-12-11 21:09:27 +01:00
|
|
|
updatePreferences: () => void,
|
2019-10-15 23:23:51 +02:00
|
|
|
syncEnabled: boolean,
|
2019-10-11 02:37:18 +02:00
|
|
|
uploadCount: number,
|
2019-10-17 17:27:41 +02:00
|
|
|
balance: ?number,
|
2019-10-22 19:57:32 +02:00
|
|
|
syncError: ?string,
|
2020-01-14 21:44:07 +01:00
|
|
|
rewards: Array<Reward>,
|
|
|
|
setReferrer: (string, boolean) => void,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2019-06-11 20:10:58 +02:00
|
|
|
function App(props: Props) {
|
2019-09-23 03:56:43 +02:00
|
|
|
const {
|
|
|
|
theme,
|
|
|
|
fetchRewards,
|
|
|
|
fetchTransactions,
|
|
|
|
user,
|
|
|
|
fetchAccessToken,
|
2019-09-26 18:07:11 +02:00
|
|
|
fetchChannelListMine,
|
2019-10-01 06:53:33 +02:00
|
|
|
signIn,
|
2019-09-26 18:28:08 +02:00
|
|
|
autoUpdateDownloaded,
|
|
|
|
isUpgradeAvailable,
|
|
|
|
requestDownloadUpgrade,
|
2019-10-15 23:23:51 +02:00
|
|
|
syncEnabled,
|
|
|
|
checkSync,
|
2019-10-11 02:37:18 +02:00
|
|
|
uploadCount,
|
2019-10-22 19:57:32 +02:00
|
|
|
history,
|
|
|
|
syncError,
|
2019-11-08 21:51:42 +01:00
|
|
|
language,
|
|
|
|
languages,
|
|
|
|
setLanguage,
|
2019-12-11 21:09:27 +01:00
|
|
|
updatePreferences,
|
2020-01-14 21:44:07 +01:00
|
|
|
rewards,
|
|
|
|
setReferrer,
|
2019-09-23 03:56:43 +02:00
|
|
|
} = props;
|
2019-10-16 19:18:12 +02:00
|
|
|
|
2019-06-11 20:10:58 +02:00
|
|
|
const appRef = useRef();
|
|
|
|
const isEnhancedLayout = useKonamiListener();
|
2019-10-15 06:20:12 +02:00
|
|
|
const [hasSignedIn, setHasSignedIn] = useState(false);
|
2019-07-22 04:28:49 +02:00
|
|
|
const userId = user && user.id;
|
2019-08-06 18:00:31 +02:00
|
|
|
const hasVerifiedEmail = user && user.has_verified_email;
|
2019-08-14 18:28:13 +02:00
|
|
|
const isRewardApproved = user && user.is_reward_approved;
|
|
|
|
const previousUserId = usePrevious(userId);
|
|
|
|
const previousHasVerifiedEmail = usePrevious(hasVerifiedEmail);
|
|
|
|
const previousRewardApproved = usePrevious(isRewardApproved);
|
2020-02-21 22:05:20 +01:00
|
|
|
const isMobile = useIsMobile();
|
2020-02-21 21:44:58 +01:00
|
|
|
// @if TARGET='web'
|
|
|
|
const [showAnalyticsNag, setShowAnalyticsNag] = usePersistedState('analytics-nag', true);
|
|
|
|
// @endif
|
2020-01-14 21:44:07 +01:00
|
|
|
const { pathname, hash, search } = props.location;
|
2019-09-23 03:56:43 +02:00
|
|
|
const showUpgradeButton = autoUpdateDownloaded || (process.platform === 'linux' && isUpgradeAvailable);
|
2020-01-14 21:44:07 +01:00
|
|
|
// referral claiming
|
|
|
|
const referredRewardAvailable = rewards && rewards.some(reward => reward.reward_type === REWARDS.TYPE_REFEREE);
|
|
|
|
const urlParams = new URLSearchParams(search);
|
|
|
|
const rawReferrerParam = urlParams.get('r');
|
|
|
|
const sanitizedReferrerParam = rawReferrerParam && rawReferrerParam.replace(':', '#');
|
2020-02-06 19:49:05 +01:00
|
|
|
const wrapperElement = appRef.current;
|
2019-08-13 07:35:13 +02:00
|
|
|
|
|
|
|
let uri;
|
|
|
|
try {
|
2019-08-30 20:12:52 +02:00
|
|
|
const newpath = buildURI(parseURI(pathname.slice(1).replace(/:/g, '#')));
|
|
|
|
uri = newpath + hash;
|
2019-08-13 07:35:13 +02:00
|
|
|
} catch (e) {}
|
|
|
|
|
2020-02-22 06:42:03 +01:00
|
|
|
const noNagOnPage = pathname.startsWith(`/$/${PAGES.EMBED}`) || pathname.startsWith(`/$/${PAGES.AUTH_VERIFY}`);
|
2020-02-21 21:44:58 +01:00
|
|
|
// @if TARGET='web'
|
|
|
|
function handleAnalyticsDismiss() {
|
|
|
|
setShowAnalyticsNag(false);
|
|
|
|
}
|
|
|
|
// @endif
|
|
|
|
|
2019-10-11 02:37:18 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (!uploadCount) return;
|
|
|
|
const handleBeforeUnload = event => {
|
|
|
|
event.preventDefault();
|
2019-12-03 19:47:03 +01:00
|
|
|
event.returnValue = 'magic'; // without setting this to something it doesn't work
|
2019-10-11 02:37:18 +02:00
|
|
|
};
|
|
|
|
window.addEventListener('beforeunload', handleBeforeUnload);
|
|
|
|
return () => window.removeEventListener('beforeunload', handleBeforeUnload);
|
|
|
|
}, [uploadCount]);
|
|
|
|
|
2020-01-14 21:44:07 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (referredRewardAvailable && sanitizedReferrerParam && isRewardApproved) {
|
|
|
|
setReferrer(sanitizedReferrerParam, true);
|
|
|
|
} else if (referredRewardAvailable && sanitizedReferrerParam) {
|
|
|
|
setReferrer(sanitizedReferrerParam, false);
|
|
|
|
}
|
|
|
|
}, [sanitizedReferrerParam, isRewardApproved, referredRewardAvailable]);
|
|
|
|
|
2019-06-11 20:10:58 +02:00
|
|
|
useEffect(() => {
|
2020-02-06 19:49:05 +01:00
|
|
|
if (wrapperElement) {
|
|
|
|
ReactModal.setAppElement(wrapperElement);
|
|
|
|
}
|
2019-08-06 18:53:59 +02:00
|
|
|
fetchAccessToken();
|
2019-06-27 08:18:45 +02:00
|
|
|
|
|
|
|
// @if TARGET='app'
|
|
|
|
fetchRewards();
|
2019-11-01 20:17:06 +01:00
|
|
|
fetchTransactions(1, TX_LIST.LATEST_PAGE_SIZE);
|
2019-09-26 18:07:11 +02:00
|
|
|
fetchChannelListMine(); // This needs to be done for web too...
|
2019-06-27 08:18:45 +02:00
|
|
|
// @endif
|
2020-02-06 19:49:05 +01:00
|
|
|
}, [fetchRewards, fetchTransactions, fetchAccessToken, fetchChannelListMine, wrapperElement]);
|
2018-10-18 18:45:24 +02:00
|
|
|
|
2019-06-11 20:10:58 +02:00
|
|
|
useEffect(() => {
|
2018-10-18 18:45:24 +02:00
|
|
|
// $FlowFixMe
|
2019-11-22 22:13:00 +01:00
|
|
|
document.documentElement.setAttribute('theme', theme);
|
2019-06-11 20:10:58 +02:00
|
|
|
}, [theme]);
|
2018-11-07 17:03:42 +01:00
|
|
|
|
2019-11-08 21:51:42 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (!languages.includes(language)) {
|
|
|
|
setLanguage(language);
|
|
|
|
}
|
|
|
|
}, [language, languages]);
|
|
|
|
|
2019-07-22 04:28:49 +02:00
|
|
|
useEffect(() => {
|
2019-08-14 18:28:13 +02:00
|
|
|
if (previousUserId === undefined && userId) {
|
2019-07-22 04:28:49 +02:00
|
|
|
analytics.setUser(userId);
|
|
|
|
}
|
2019-08-14 18:28:13 +02:00
|
|
|
}, [previousUserId, userId]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2019-08-15 05:09:34 +02:00
|
|
|
// Check that previousHasVerifiedEmail was not undefined instead of just not truthy
|
|
|
|
// This ensures we don't fire the emailVerified event on the initial user fetch
|
2019-09-26 18:07:11 +02:00
|
|
|
if (previousHasVerifiedEmail === false && hasVerifiedEmail) {
|
2019-08-14 18:28:13 +02:00
|
|
|
analytics.emailVerifiedEvent();
|
|
|
|
}
|
2019-10-01 06:53:33 +02:00
|
|
|
}, [previousHasVerifiedEmail, hasVerifiedEmail, signIn]);
|
2019-08-14 18:28:13 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2019-09-26 18:07:11 +02:00
|
|
|
if (previousRewardApproved === false && isRewardApproved) {
|
2019-08-14 18:28:13 +02:00
|
|
|
analytics.rewardEligibleEvent();
|
|
|
|
}
|
|
|
|
}, [previousRewardApproved, isRewardApproved]);
|
2019-07-22 04:28:49 +02:00
|
|
|
|
2019-09-26 18:07:11 +02:00
|
|
|
// Keep this at the end to ensure initial setup effects are run first
|
2019-08-06 18:00:31 +02:00
|
|
|
useEffect(() => {
|
2019-10-08 20:11:52 +02:00
|
|
|
// Wait for balance to be populated on desktop so we know when we can begin syncing
|
2019-10-15 23:23:51 +02:00
|
|
|
if (!hasSignedIn && hasVerifiedEmail) {
|
2019-10-01 06:53:33 +02:00
|
|
|
signIn();
|
2019-10-15 06:20:12 +02:00
|
|
|
setHasSignedIn(true);
|
2019-08-06 18:00:31 +02:00
|
|
|
}
|
2019-10-15 23:23:51 +02:00
|
|
|
}, [hasVerifiedEmail, signIn, hasSignedIn]);
|
|
|
|
|
2019-12-21 19:43:42 +01:00
|
|
|
// @if TARGET='app'
|
|
|
|
useEffect(() => {
|
|
|
|
updatePreferences();
|
|
|
|
}, []);
|
|
|
|
// @endif
|
|
|
|
|
2019-10-15 23:23:51 +02:00
|
|
|
useEffect(() => {
|
2019-10-24 17:48:58 +02:00
|
|
|
if (hasVerifiedEmail && syncEnabled) {
|
2019-10-15 23:23:51 +02:00
|
|
|
checkSync();
|
|
|
|
|
|
|
|
let syncInterval = setInterval(() => {
|
|
|
|
checkSync();
|
|
|
|
}, SYNC_INTERVAL);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearInterval(syncInterval);
|
|
|
|
};
|
|
|
|
}
|
2019-10-24 17:48:58 +02:00
|
|
|
}, [hasVerifiedEmail, syncEnabled, checkSync]);
|
2019-12-11 21:09:27 +01:00
|
|
|
|
2019-10-22 19:57:32 +02:00
|
|
|
useEffect(() => {
|
2019-10-22 22:43:51 +02:00
|
|
|
if (syncError) {
|
|
|
|
history.push(`/$/${PAGES.AUTH}?redirect=${pathname}`);
|
|
|
|
}
|
2019-10-22 19:57:32 +02:00
|
|
|
}, [syncError, pathname]);
|
|
|
|
|
2019-11-14 16:25:01 +01:00
|
|
|
// @if TARGET='web'
|
|
|
|
// Require an internal-api user on lbry.tv
|
|
|
|
// This also prevents the site from loading in the un-authed state while we wait for internal-apis to return for the first time
|
|
|
|
// It's not needed on desktop since there is no un-authed state
|
2020-01-14 21:01:37 +01:00
|
|
|
if (!user) {
|
2019-12-05 17:30:48 +01:00
|
|
|
return null;
|
|
|
|
}
|
2019-11-14 16:25:01 +01:00
|
|
|
// @endif
|
2019-08-06 18:00:31 +02:00
|
|
|
|
2019-06-11 20:10:58 +02:00
|
|
|
return (
|
2019-10-09 18:34:18 +02:00
|
|
|
<div
|
2019-10-15 18:21:49 +02:00
|
|
|
className={classnames(MAIN_WRAPPER_CLASS, {
|
|
|
|
// @if TARGET='app'
|
|
|
|
[`${MAIN_WRAPPER_CLASS}--mac`]: IS_MAC,
|
|
|
|
// @endif
|
|
|
|
})}
|
2019-10-09 18:34:18 +02:00
|
|
|
ref={appRef}
|
2019-11-01 21:44:26 +01:00
|
|
|
onContextMenu={IS_WEB ? undefined : e => openContextMenu(e)}
|
2019-10-09 18:34:18 +02:00
|
|
|
>
|
2019-08-27 16:43:42 +02:00
|
|
|
<Router />
|
2019-06-11 20:10:58 +02:00
|
|
|
<ModalRouter />
|
2020-01-06 19:32:35 +01:00
|
|
|
<FloatingViewer pageUri={uri} />
|
2019-08-13 07:35:13 +02:00
|
|
|
|
2019-12-05 06:24:54 +01:00
|
|
|
{/* @if TARGET='web' */}
|
|
|
|
<YoutubeWelcome />
|
2019-12-06 22:12:48 +01:00
|
|
|
<OpenInAppLink uri={uri} />
|
2019-12-05 06:24:54 +01:00
|
|
|
{/* @endif */}
|
|
|
|
|
2019-09-23 03:56:43 +02:00
|
|
|
{/* @if TARGET='app' */}
|
|
|
|
{showUpgradeButton && (
|
2019-12-06 22:12:48 +01:00
|
|
|
<Nag message={__('An upgrade is available.')} actionText={__('Install Now')} onClick={requestDownloadUpgrade} />
|
2019-09-23 03:56:43 +02:00
|
|
|
)}
|
|
|
|
{/* @endif */}
|
2020-02-21 21:44:58 +01:00
|
|
|
{/* @if TARGET='web' */}
|
2020-02-22 06:42:03 +01:00
|
|
|
{showAnalyticsNag && !noNagOnPage && (
|
2020-02-21 22:05:20 +01:00
|
|
|
<React.Fragment>
|
2020-02-21 23:24:37 +01:00
|
|
|
{isMobile ? (
|
2020-02-21 22:05:20 +01:00
|
|
|
<Nag
|
2020-02-21 22:12:11 +01:00
|
|
|
message={
|
|
|
|
<I18nMessage
|
|
|
|
tokens={{
|
|
|
|
more_information: (
|
|
|
|
<Button button="link" label={__('more')} href="https://lbry.com/faq/privacy-and-data" />
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
lbry.tv collects usage information for itself and third parties (%more_information%).
|
|
|
|
</I18nMessage>
|
|
|
|
}
|
2020-02-21 22:05:20 +01:00
|
|
|
actionText={__('OK')}
|
2020-02-21 22:08:17 +01:00
|
|
|
onClick={handleAnalyticsDismiss}
|
2020-02-21 22:05:20 +01:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Nag
|
|
|
|
message={
|
|
|
|
<I18nMessage
|
|
|
|
tokens={{
|
|
|
|
more_information: (
|
|
|
|
<Button button="link" label={__('more')} href="https://lbry.com/faq/privacy-and-data" />
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
lbry.tv collects usage information for itself and third parties (%more_information%). Want control
|
|
|
|
over this and more?
|
|
|
|
</I18nMessage>
|
|
|
|
}
|
|
|
|
actionText={__('Get The App')}
|
|
|
|
href="https://lbry.com/get"
|
|
|
|
onClose={handleAnalyticsDismiss}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
2020-02-21 21:44:58 +01:00
|
|
|
)}
|
|
|
|
{/* @endif */}
|
2019-06-11 20:10:58 +02:00
|
|
|
{isEnhancedLayout && <Yrbl className="yrbl--enhanced" />}
|
|
|
|
</div>
|
|
|
|
);
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2019-08-13 07:35:13 +02:00
|
|
|
export default withRouter(App);
|