// @flow import React, { useEffect } from 'react'; import { Route, Redirect, Switch, withRouter } from 'react-router-dom'; import * as PAGES from 'constants/pages'; import { LINKED_COMMENT_QUERY_PARAM } from 'constants/comment'; import { parseURI, isURIValid } from 'lbry-redux'; import { SITE_TITLE, WELCOME_VERSION, SIMPLE_SITE } from 'config'; import HomePage from 'page/home'; // @if TARGET='app' const BackupPage = React.lazy(() => import('page/backup' /* webpackChunkName: "backup" */)); // @endif // @if TARGET='web' const Code2257Page = React.lazy(() => import('web/page/code2257')); // @endif // Chunk: "secondary" const SignInPage = React.lazy(() => import('page/signIn' /* webpackChunkName: "secondary" */)); const SignInWalletPasswordPage = React.lazy(() => import('page/signInWalletPassword' /* webpackChunkName: "secondary" */)); const SignUpPage = React.lazy(() => import('page/signUp' /* webpackChunkName: "secondary" */)); const SignInVerifyPage = React.lazy(() => import('page/signInVerify' /* webpackChunkName: "secondary" */)); // Chunk: "wallet/secondary" const BuyPage = React.lazy(() => import('page/buy' /* webpackChunkName: "secondary" */)); const ReceivePage = React.lazy(() => import('page/receive' /* webpackChunkName: "secondary" */)); const SendPage = React.lazy(() => import('page/send' /* webpackChunkName: "secondary" */)); const SwapPage = React.lazy(() => import('page/swap' /* webpackChunkName: "secondary" */)); const WalletPage = React.lazy(() => import('page/wallet' /* webpackChunkName: "secondary" */)); // Chunk: none const NotificationsPage = React.lazy(() => import('page/notifications' /* webpackChunkName: "secondary" */)); const CollectionPage = React.lazy(() => import('page/collection' /* webpackChunkName: "secondary" */)); const ChannelNew = React.lazy(() => import('page/channelNew' /* webpackChunkName: "secondary" */)); const ChannelsFollowingDiscoverPage = React.lazy(() => import('page/channelsFollowingDiscover' /* webpackChunkName: "secondary" */)); const ChannelsFollowingPage = React.lazy(() => import('page/channelsFollowing' /* webpackChunkName: "secondary" */)); const ChannelsPage = React.lazy(() => import('page/channels' /* webpackChunkName: "secondary" */)); const CheckoutPage = React.lazy(() => import('page/checkoutPage' /* webpackChunkName: "checkoutPage" */)); const CreatorDashboard = React.lazy(() => import('page/creatorDashboard' /* webpackChunkName: "secondary" */)); const DiscoverPage = React.lazy(() => import('page/discover' /* webpackChunkName: "secondary" */)); const EmbedWrapperPage = React.lazy(() => import('page/embedWrapper' /* webpackChunkName: "secondary" */)); const FileListPublished = React.lazy(() => import('page/fileListPublished' /* webpackChunkName: "secondary" */)); const FourOhFourPage = React.lazy(() => import('page/fourOhFour' /* webpackChunkName: "fourOhFour" */)); const HelpPage = React.lazy(() => import('page/help' /* webpackChunkName: "help" */)); const InvitePage = React.lazy(() => import('page/invite' /* webpackChunkName: "secondary" */)); const InvitedPage = React.lazy(() => import('page/invited' /* webpackChunkName: "secondary" */)); const LibraryPage = React.lazy(() => import('page/library' /* webpackChunkName: "secondary" */)); const ListBlockedPage = React.lazy(() => import('page/listBlocked' /* webpackChunkName: "secondary" */)); const ListsPage = React.lazy(() => import('page/lists' /* webpackChunkName: "secondary" */)); const LiveStreamSetupPage = React.lazy(() => import('page/livestreamSetup' /* webpackChunkName: "secondary" */)); const LivestreamCurrentPage = React.lazy(() => import('page/livestreamCurrent' /* webpackChunkName: "secondary" */)); const PasswordResetPage = React.lazy(() => import('page/passwordReset' /* webpackChunkName: "secondary" */)); const PasswordSetPage = React.lazy(() => import('page/passwordSet' /* webpackChunkName: "secondary" */)); const PublishPage = React.lazy(() => import('page/publish' /* webpackChunkName: "secondary" */)); const ReportContentPage = React.lazy(() => import('page/reportContent' /* webpackChunkName: "secondary" */)); const ReportPage = React.lazy(() => import('page/report' /* webpackChunkName: "secondary" */)); const RepostNew = React.lazy(() => import('page/repost' /* webpackChunkName: "secondary" */)); const RewardsPage = React.lazy(() => import('page/rewards' /* webpackChunkName: "secondary" */)); const RewardsVerifyPage = React.lazy(() => import('page/rewardsVerify' /* webpackChunkName: "secondary" */)); const SearchPage = React.lazy(() => import('page/search' /* webpackChunkName: "secondary" */)); const SettingsAdvancedPage = React.lazy(() => import('page/settingsAdvanced' /* webpackChunkName: "secondary" */)); const SettingsCreatorPage = React.lazy(() => import('page/settingsCreator' /* webpackChunkName: "secondary" */)); const SettingsNotificationsPage = React.lazy(() => import('page/settingsNotifications' /* webpackChunkName: "secondary" */)); const SettingsPage = React.lazy(() => import('page/settings' /* webpackChunkName: "secondary" */)); const ShowPage = React.lazy(() => import('page/show' /* webpackChunkName: "secondary" */)); const TagsFollowingManagePage = React.lazy(() => import('page/tagsFollowingManage' /* webpackChunkName: "secondary" */)); const TagsFollowingPage = React.lazy(() => import('page/tagsFollowing' /* webpackChunkName: "secondary" */)); const TopPage = React.lazy(() => import('page/top' /* webpackChunkName: "secondary" */)); const Welcome = React.lazy(() => import('page/welcome' /* webpackChunkName: "secondary" */)); const YoutubeSyncPage = React.lazy(() => import('page/youtubeSync' /* webpackChunkName: "secondary" */)); // Tell the browser we are handling scroll restoration if ('scrollRestoration' in history) { history.scrollRestoration = 'manual'; } type Props = { currentScroll: number, isAuthenticated: boolean, location: { pathname: string, search: string, hash: string }, history: { action: string, entries: { title: string }[], goBack: () => void, goForward: () => void, index: number, length: number, location: { pathname: string }, push: (string) => void, state: {}, replaceState: ({}, string, string) => void, listen: (any) => () => void, }, uri: string, title: string, welcomeVersion: number, hasNavigated: boolean, setHasNavigated: () => void, setReferrer: (string) => void, hasUnclaimedRefereeReward: boolean, homepageData: any, }; type PrivateRouteProps = Props & { component: any, isAuthenticated: boolean, }; function PrivateRoute(props: PrivateRouteProps) { const { component: Component, isAuthenticated, ...rest } = props; const urlSearchParams = new URLSearchParams(props.location.search); const redirectUrl = urlSearchParams.get('redirect'); return ( isAuthenticated || !IS_WEB ? ( ) : ( ) } /> ); } function AppRouter(props: Props) { const { currentScroll, location: { pathname, search, hash }, isAuthenticated, history, uri, title, welcomeVersion, hasNavigated, setHasNavigated, hasUnclaimedRefereeReward, setReferrer, homepageData, } = props; const { entries, listen, action: historyAction } = history; const entryIndex = history.index; const urlParams = new URLSearchParams(search); const resetScroll = urlParams.get('reset_scroll'); const hasLinkedCommentInUrl = urlParams.get(LINKED_COMMENT_QUERY_PARAM); const dynamicRoutes = Object.values(homepageData).filter( (potentialRoute: any) => potentialRoute && potentialRoute.route ); // For people arriving at settings page from deeplinks, know whether they can "go back" useEffect(() => { const unlisten = listen((location, action) => { if (action === 'PUSH') { if (!hasNavigated && setHasNavigated) setHasNavigated(); } }); return unlisten; }, [listen, hasNavigated, setHasNavigated]); useEffect(() => { if (!hasNavigated && hasUnclaimedRefereeReward && !isAuthenticated) { const valid = isURIValid(uri); if (valid) { const { path } = parseURI(uri); if (path !== 'undefined') setReferrer(path); } } }, [hasNavigated, uri, hasUnclaimedRefereeReward, setReferrer, isAuthenticated]); useEffect(() => { if (uri) { const { channelName, streamName } = parseURI(uri); if (title) { document.title = title; } else if (streamName) { document.title = streamName; } else if (channelName) { document.title = channelName; } else { document.title = IS_WEB ? SITE_TITLE : 'LBRY'; } } else { document.title = IS_WEB ? SITE_TITLE : 'LBRY'; } // @if TARGET='app' entries[entryIndex].title = document.title; // @endif return () => { document.title = IS_WEB ? SITE_TITLE : 'LBRY'; }; }, [entries, entryIndex, title, uri]); useEffect(() => { if (!hasLinkedCommentInUrl) { if (hash && historyAction === 'PUSH') { const id = hash.replace('#', ''); const element = document.getElementById(id); if (element) { window.scrollTo(0, element.offsetTop); } } else { window.scrollTo(0, currentScroll); } } }, [currentScroll, pathname, search, hash, resetScroll, hasLinkedCommentInUrl, historyAction]); // react-router doesn't decode pathanmes before doing the route matching check // We have to redirect here because if we redirect on the server, it might get encoded again // in the browser causing a redirect loop const decodedUrl = decodeURIComponent(pathname) + search; if (decodedUrl !== pathname + search) { return ; } return ( TODO.....................}> {/* @if TARGET='app' */} {welcomeVersion < WELCOME_VERSION && } {/* @endif */} {SIMPLE_SITE && } {/* $FlowFixMe */} {dynamicRoutes.map((dynamicRouteProps: RowDataItem) => ( } /> ))} {/* @if TARGET='app' */} {/* @endif */} {/* @if TARGET='web' */} {/* @endif */} {/* Below need to go at the end to make sure we don't match any of our pages first */} ); } export default withRouter(AppRouter);