// @flow import * as PAGES from 'constants/pages'; import React, { useEffect } from 'react'; import { Route, Redirect, Switch, withRouter } from 'react-router-dom'; import SettingsPage from 'page/settings'; import SettingsNotificationsPage from 'page/settingsNotifications'; import SettingsAdvancedPage from 'page/settingsAdvanced'; import HelpPage from 'page/help'; // @if TARGET='app' import BackupPage from 'page/backup'; // @endif // @if TARGET='web' import Code2257Page from 'web/page/code2257'; // @endif import ReportPage from 'page/report'; import ShowPage from 'page/show'; import PublishPage from 'page/publish'; import DiscoverPage from 'page/discover'; import HomePage from 'page/home'; import InvitedPage from 'page/invited'; import RewardsPage from 'page/rewards'; import FileListPublished from 'page/fileListPublished'; import InvitePage from 'page/invite'; import SearchPage from 'page/search'; import LibraryPage from 'page/library'; import WalletPage from 'page/wallet'; import TagsFollowingPage from 'page/tagsFollowing'; import ChannelsFollowingPage from 'page/channelsFollowing'; import ChannelsFollowingDiscoverPage from 'page/channelsFollowingDiscover'; import TagsFollowingManagePage from 'page/tagsFollowingManage'; import ListBlockedPage from 'page/listBlocked'; import FourOhFourPage from 'page/fourOhFour'; import SignInPage from 'page/signIn'; import SignUpPage from 'page/signUp'; import PasswordResetPage from 'page/passwordReset'; import PasswordSetPage from 'page/passwordSet'; import SignInVerifyPage from 'page/signInVerify'; import ChannelsPage from 'page/channels'; import EmbedWrapperPage from 'page/embedWrapper'; import TopPage from 'page/top'; import Welcome from 'page/welcome'; import CreatorDashboard from 'page/creatorDashboard'; import RewardsVerifyPage from 'page/rewardsVerify'; import CheckoutPage from 'page/checkoutPage'; import ChannelNew from 'page/channelNew'; import BuyPage from 'page/buy'; import NotificationsPage from 'page/notifications'; import { parseURI } from 'lbry-redux'; import { SITE_TITLE, WELCOME_VERSION } from 'config'; // Tell the browser we are handling scroll restoration if ('scrollRestoration' in history) { history.scrollRestoration = 'manual'; } type PrivateRouteProps = { component: any, isAuthenticated: boolean, location: { pathname: string }, }; function PrivateRoute(props: PrivateRouteProps) { const { component: Component, isAuthenticated, ...rest } = props; return ( isAuthenticated || !IS_WEB ? ( ) : ( ) } /> ); } type Props = { currentScroll: number, isAuthenticated: boolean, location: { pathname: string, search: string }, history: { 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, syncSettings: () => void, checkSync: () => void, }; function AppRouter(props: Props) { const { currentScroll, location: { pathname, search }, isAuthenticated, history, uri, title, welcomeVersion, hasNavigated, setHasNavigated, syncSettings, checkSync, } = props; const { entries } = history; const entryIndex = history.index; const urlParams = new URLSearchParams(search); const resetScroll = urlParams.get('reset_scroll'); const [prevPath, setPrevPath] = React.useState(pathname); // For people arriving at settings page from deeplinks, know whether they can "go back" useEffect(() => { const unlisten = history.listen((location, action) => { if (action === 'PUSH') { if (!hasNavigated && setHasNavigated) setHasNavigated(); } }); return unlisten; }, [hasNavigated, setHasNavigated]); // Sync when no longer on a settings page, or when entering settings pages useEffect(() => { const unlisten = history.listen(location => { if (!location.pathname.includes(PAGES.SETTINGS) && prevPath.includes(PAGES.SETTINGS)) { syncSettings(); } else if (location.pathname.includes(PAGES.SETTINGS) && !prevPath.includes(PAGES.SETTINGS)) { checkSync(); } }); return unlisten; }, [prevPath, syncSettings, checkSync]); useEffect(() => { const unlisten = history.listen(location => { if (prevPath !== location.pathname && setPrevPath) { setPrevPath(location.pathname); } }); return unlisten; }, [prevPath, setPrevPath]); useEffect(() => { if (uri) { const { channelName, streamName } = parseURI(uri); if (typeof title !== 'undefined' && 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(() => { window.scrollTo(0, currentScroll); }, [currentScroll, pathname, resetScroll]); // 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 ( {/* @if TARGET='app' */} {welcomeVersion < WELCOME_VERSION && } {/* @endif */} {/* @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);