// @flow import React, { useEffect } from 'react'; import { Route, Redirect, Switch, withRouter } from 'react-router-dom'; import * as PAGES from 'constants/pages'; import { PAGE_TITLE } from 'constants/pageTitles'; import { LINKED_COMMENT_QUERY_PARAM } from 'constants/comment'; import { parseURI, isURIValid } from 'util/lbryURI'; import { WELCOME_VERSION } from 'config'; import { GetLinksData } from 'util/buildHomepage'; import HomePage from 'page/home'; import BackupPage from 'page/backup'; // Chunk: "secondary" import SignInPage from 'page/signIn'; import SignInWalletPasswordPage from 'page/signInWalletPassword'; import SignUpPage from 'page/signUp'; import SignInVerifyPage from 'page/signInVerify'; // Chunk: "wallet/secondary" import BuyPage from 'page/buy'; import ReceivePage from 'page/receive'; import SendPage from 'page/send'; import WalletPage from 'page/wallet'; // Chunk: none import NotificationsPage from 'page/notifications'; import CollectionPage from 'page/collection'; import ChannelNew from 'page/channelNew'; import ChannelsFollowingDiscoverPage from 'page/channelsFollowingDiscover'; import ChannelsFollowingPage from 'page/channelsFollowing'; import ChannelsPage from 'page/channels'; import CreatorDashboard from 'page/creatorDashboard'; import DiscoverPage from 'page/discover'; import FileListPublished from 'page/fileListPublished'; import FourOhFourPage from 'page/fourOhFour'; import HelpPage from 'page/help'; import LibraryPage from 'page/library'; import ListBlockedPage from 'page/listBlocked'; import ListsPage from 'page/lists'; import PlaylistsPage from 'page/playlists'; import OwnComments from 'page/ownComments'; import PasswordResetPage from 'page/passwordReset'; import PasswordSetPage from 'page/passwordSet'; import PublishPage from 'page/publish'; import ReportContentPage from 'page/reportContent'; import ReportPage from 'page/report'; import RepostNew from 'page/repost'; import SearchPage from 'page/search'; import SettingsCreatorPage from 'page/settingsCreator'; import SettingsNotificationsPage from 'page/settingsNotifications'; import SettingsPage from 'page/settings'; import ShowPage from 'page/show'; import TagsFollowingManagePage from 'page/tagsFollowingManage'; import TagsFollowingPage from 'page/tagsFollowing'; import TopPage from 'page/top'; import UpdatePasswordPage from 'page/passwordUpdate'; import Welcome from 'page/welcome'; // 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 = GetLinksData(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(() => { const getDefaultTitle = (pathname: string) => { const title = pathname.startsWith('/$/') ? PAGE_TITLE[pathname.substring(3)] : ''; if (process.env.NODE_ENV !== 'production') { return uri || pathname || title; } return __(title) || 'LBRY'; }; 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 = getDefaultTitle(pathname); } } else { document.title = getDefaultTitle(pathname); } // @if TARGET='app' entries[entryIndex].title = document.title; // @endif }, [pathname, 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 ( {welcomeVersion < WELCOME_VERSION && } {/* $FlowFixMe */} {dynamicRoutes.map((dynamicRouteProps: RowDataItem) => ( } /> ))} {/* @if TARGET='app' */} {/* @endif */} {/* Below need to go at the end to make sure we don't match any of our pages first */} ); } export default withRouter(AppRouter);