// @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 { useIsLargeScreen } from 'effects/use-screensize'; import { lazyImport } from 'util/lazyImport'; import { LINKED_COMMENT_QUERY_PARAM } from 'constants/comment'; import { parseURI, isURIValid } from 'util/lbryURI'; import { SITE_TITLE } from 'config'; import LoadingBarOneOff from 'component/loadingBarOneOff'; import { GetLinksData } from 'util/buildHomepage'; import * as CS from 'constants/claim_search'; import { buildUnseenCountStr } from 'util/notifications'; import HomePage from 'page/home'; // @if TARGET='app' const BackupPage = lazyImport(() => import('page/backup' /* webpackChunkName: "backup" */)); // @endif // @if TARGET='web' const Code2257Page = lazyImport(() => import('web/page/code2257' /* webpackChunkName: "code2257" */)); const PrivacyPolicyPage = lazyImport(() => import('web/page/privacypolicy' /* webpackChunkName: "privacypolicy" */)); const TOSPage = lazyImport(() => import('web/page/tos' /* webpackChunkName: "tos" */)); const FypPage = lazyImport(() => import('web/page/fyp' /* webpackChunkName: "fyp" */)); const YouTubeTOSPage = lazyImport(() => import('web/page/youtubetos' /* webpackChunkName: "youtubetos" */)); // @endif const SignInPage = lazyImport(() => import('page/signIn' /* webpackChunkName: "signIn" */)); const SignInWalletPasswordPage = lazyImport(() => import('page/signInWalletPassword' /* webpackChunkName: "signInWalletPassword" */) ); const SignUpPage = lazyImport(() => import('page/signUp' /* webpackChunkName: "signUp" */)); const SignInVerifyPage = lazyImport(() => import('page/signInVerify' /* webpackChunkName: "signInVerify" */)); const BuyPage = lazyImport(() => import('page/buy' /* webpackChunkName: "buy" */)); const ReceivePage = lazyImport(() => import('page/receive' /* webpackChunkName: "receive" */)); const SendPage = lazyImport(() => import('page/send' /* webpackChunkName: "send" */)); const SwapPage = lazyImport(() => import('page/swap' /* webpackChunkName: "swap" */)); const WalletPage = lazyImport(() => import('page/wallet' /* webpackChunkName: "wallet" */)); const NotificationsPage = lazyImport(() => import('page/notifications' /* webpackChunkName: "notifications" */)); const CollectionPage = lazyImport(() => import('page/collection' /* webpackChunkName: "collection" */)); const ChannelNew = lazyImport(() => import('page/channelNew' /* webpackChunkName: "channelNew" */)); const ChannelsFollowingDiscoverPage = lazyImport(() => import('page/channelsFollowingDiscover' /* webpackChunkName: "channelsFollowingDiscover" */) ); const ChannelsFollowingPage = lazyImport(() => import('page/channelsFollowing' /* webpackChunkName: "channelsFollowing" */) ); const ChannelsFollowingManage = lazyImport(() => import('page/channelsFollowingManage' /* webpackChunkName: "channelsFollowing" */) ); const ChannelsPage = lazyImport(() => import('page/channels' /* webpackChunkName: "channels" */)); const CheckoutPage = lazyImport(() => import('page/checkoutPage' /* webpackChunkName: "checkoutPage" */)); const CreatorDashboard = lazyImport(() => import('page/creatorDashboard' /* webpackChunkName: "creatorDashboard" */)); const DiscoverPage = lazyImport(() => import('page/discover' /* webpackChunkName: "discover" */)); const EmbedWrapperPage = lazyImport(() => import('page/embedWrapper' /* webpackChunkName: "embedWrapper" */)); const PopoutChatPage = lazyImport(() => import('page/popoutChatWrapper' /* webpackChunkName: "popoutChat" */)); const FileListPublished = lazyImport(() => import('page/fileListPublished' /* webpackChunkName: "fileListPublished" */) ); const FourOhFourPage = lazyImport(() => import('page/fourOhFour' /* webpackChunkName: "fourOhFour" */)); const HelpPage = lazyImport(() => import('page/help' /* webpackChunkName: "help" */)); const InvitePage = lazyImport(() => import('page/invite' /* webpackChunkName: "invite" */)); const InvitedPage = lazyImport(() => import('page/invited' /* webpackChunkName: "invited" */)); const LibraryPage = lazyImport(() => import('page/library' /* webpackChunkName: "library" */)); const ListBlockedPage = lazyImport(() => import('page/listBlocked' /* webpackChunkName: "listBlocked" */)); const ListsPage = lazyImport(() => import('page/lists' /* webpackChunkName: "lists" */)); const PlaylistsPage = lazyImport(() => import('page/playlists' /* webpackChunkName: "lists" */)); const LiveStreamSetupPage = lazyImport(() => import('page/livestreamSetup' /* webpackChunkName: "livestreamSetup" */)); const LivestreamCurrentPage = lazyImport(() => import('page/livestreamCurrent' /* webpackChunkName: "livestreamCurrent" */) ); const OdyseeMembershipPage = lazyImport(() => import('page/odyseeMembership' /* webpackChunkName: "odyseeMembership" */) ); const OwnComments = lazyImport(() => import('page/ownComments' /* webpackChunkName: "ownComments" */)); const PasswordResetPage = lazyImport(() => import('page/passwordReset' /* webpackChunkName: "passwordReset" */)); const PasswordSetPage = lazyImport(() => import('page/passwordSet' /* webpackChunkName: "passwordSet" */)); const PublishPage = lazyImport(() => import('page/publish' /* webpackChunkName: "publish" */)); const ReportContentPage = lazyImport(() => import('page/reportContent' /* webpackChunkName: "reportContent" */)); const ReportPage = lazyImport(() => import('page/report' /* webpackChunkName: "report" */)); const RewardsPage = lazyImport(() => import('page/rewards' /* webpackChunkName: "rewards" */)); const RewardsVerifyPage = lazyImport(() => import('page/rewardsVerify' /* webpackChunkName: "rewardsVerify" */)); const SearchPage = lazyImport(() => import('page/search' /* webpackChunkName: "search" */)); const SettingsStripeCard = lazyImport(() => import('page/settingsStripeCard' /* webpackChunkName: "settingsStripeCard" */) ); const SettingsStripeAccount = lazyImport(() => import('page/settingsStripeAccount' /* webpackChunkName: "settingsStripeAccount" */) ); const SettingsCreatorPage = lazyImport(() => import('page/settingsCreator' /* webpackChunkName: "settingsCreator" */)); const SettingsNotificationsPage = lazyImport(() => import('page/settingsNotifications' /* webpackChunkName: "settingsNotifications" */) ); const SettingsPage = lazyImport(() => import('page/settings' /* webpackChunkName: "settings" */)); const ShowPage = lazyImport(() => import('page/show' /* webpackChunkName: "show" */)); const TagsFollowingManagePage = lazyImport(() => import('page/tagsFollowingManage' /* webpackChunkName: "tagsFollowingManage" */) ); const TagsFollowingPage = lazyImport(() => import('page/tagsFollowing' /* webpackChunkName: "tagsFollowing" */)); const TopPage = lazyImport(() => import('page/top' /* webpackChunkName: "top" */)); const UpdatePasswordPage = lazyImport(() => import('page/passwordUpdate' /* webpackChunkName: "passwordUpdate" */)); const YoutubeSyncPage = lazyImport(() => import('page/youtubeSync' /* webpackChunkName: "youtubeSync" */)); // 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, wildWestDisabled: boolean, unseenCount: number, hideTitleNotificationCount: boolean, }; 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, hasNavigated, setHasNavigated, hasUnclaimedRefereeReward, setReferrer, homepageData, wildWestDisabled, unseenCount, hideTitleNotificationCount, } = 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 tagParams = urlParams.get(CS.TAGS_KEY); const isLargeScreen = useIsLargeScreen(); const categoryPages = React.useMemo(() => { const dynamicRoutes = GetLinksData(homepageData, isLargeScreen).filter( (x: any) => x && x.route && (x.id !== 'WILD_WEST' || !wildWestDisabled) ); return dynamicRoutes.map((dynamicRouteProps: RowDataItem) => ( } /> )); }, [homepageData, isLargeScreen, wildWestDisabled]); // 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)] : ''; return __(title) || (IS_WEB ? SITE_TITLE : 'Odysee'); }; 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 (unseenCount > 0 && !hideTitleNotificationCount) { document.title = `(${buildUnseenCountStr(unseenCount)}) ${document.title}`; } }, [pathname, entries, entryIndex, title, uri, unseenCount]); 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 ( }> {(!wildWestDisabled || tagParams) && } {categoryPages} {/* @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);