lbry-desktop/ui/component/page/view.jsx

142 lines
4.2 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import { MAIN_CLASS } from 'constants/classnames';
import { parseURI } from 'util/lbryURI';
import { useHistory } from 'react-router';
import { useIsMobile, useIsMediumScreen } from 'effects/use-screensize';
2018-03-26 23:32:43 +02:00
import classnames from 'classnames';
2019-08-27 16:43:42 +02:00
import Header from 'component/header';
import React from 'react';
import Wallpaper from 'component/wallpaper';
import SettingsSideNavigation from 'component/settingsSideNavigation';
import SideNavigation from 'component/sideNavigation';
import StatusBar from 'component/common/status-bar';
2020-08-10 22:47:39 +02:00
import usePersistedState from 'effects/use-persisted-state';
import type { Element, Node } from 'react';
2018-03-26 23:32:43 +02:00
type Props = {
2019-01-09 19:39:05 +01:00
className: ?string,
2019-08-27 16:43:42 +02:00
autoUpdateDownloaded: boolean,
isUpgradeAvailable: boolean,
2019-10-28 15:04:37 +01:00
authPage: boolean,
backout: {
backLabel?: string,
backNavDefault?: string,
title: string,
simpleTitle: string, // Just use the same value as `title` if `title` is already short (~< 10 chars), unless you have a better idea for title overlfow on mobile
},
children: Node | Array<Node>,
className: ?string,
filePage: boolean,
fullWidthPage: boolean,
isMarkdown?: boolean,
noHeader: boolean,
noSideNavigation: boolean,
rightSide?: Element<any>,
settingsPage?: boolean,
videoTheaterMode: boolean,
2018-03-26 23:32:43 +02:00
};
2019-06-17 22:32:38 +02:00
function Page(props: Props) {
2020-06-29 21:54:07 +02:00
const {
authPage = false,
backout,
2020-06-29 21:54:07 +02:00
children,
className,
2020-08-10 22:47:39 +02:00
filePage = false,
2020-08-21 17:49:13 +02:00
fullWidthPage = false,
isMarkdown = false,
2020-06-29 21:54:07 +02:00
noHeader = false,
noSideNavigation = false,
2021-04-23 21:59:48 +02:00
rightSide,
settingsPage,
videoTheaterMode,
2020-06-29 21:54:07 +02:00
} = props;
2021-01-08 16:21:27 +01:00
2020-08-10 22:47:39 +02:00
const {
location: { pathname },
} = useHistory();
2020-08-10 22:47:39 +02:00
const isMediumScreen = useIsMediumScreen();
const isMobile = useIsMobile();
const [sidebarOpen, setSidebarOpen] = usePersistedState('sidebar', false);
2020-08-21 17:49:13 +02:00
const url = pathname.slice(1).replace(/:/g, '#');
2020-08-10 22:47:39 +02:00
let isOnFilePage = false;
try {
const { isChannel } = parseURI(url);
if (!isChannel) isOnFilePage = true;
2020-08-10 22:47:39 +02:00
} catch (e) {}
const isAbsoluteSideNavHidden = (isOnFilePage || isMobile) && !sidebarOpen;
React.useEffect(() => {
if (isOnFilePage || isMediumScreen) setSidebarOpen(false);
2020-12-17 19:43:47 +01:00
// TODO: make sure setState callback for usePersistedState uses useCallback to it doesn't cause effect to re-run
// eslint-disable-next-line react-hooks/exhaustive-deps
2020-12-17 19:43:47 +01:00
}, [isOnFilePage, isMediumScreen]);
2019-06-17 22:32:38 +02:00
2019-08-27 16:43:42 +02:00
return (
<>
<Wallpaper uri={url} />
2020-08-10 22:47:39 +02:00
{!noHeader && (
<Header
authHeader={authPage}
backout={backout}
sidebarOpen={sidebarOpen}
isAbsoluteSideNavHidden={isAbsoluteSideNavHidden}
setSidebarOpen={setSidebarOpen}
/>
)}
2021-01-08 16:21:27 +01:00
<div
className={classnames('main-wrapper__inner', {
'main-wrapper__inner--filepage': isOnFilePage,
'main-wrapper__inner--theater-mode': isOnFilePage && videoTheaterMode && !isMediumScreen,
2021-01-08 16:21:27 +01:00
})}
>
{!authPage &&
(settingsPage ? (
<SettingsSideNavigation />
) : (
!noSideNavigation && (
<SideNavigation
sidebarOpen={sidebarOpen}
setSidebarOpen={setSidebarOpen}
isMediumScreen={isMediumScreen}
isOnFilePage={isOnFilePage}
/>
)
))}
<div
className={classnames({
'sidebar--pusher': fullWidthPage,
'sidebar--pusher--open': sidebarOpen && fullWidthPage,
'sidebar--pusher--filepage': !fullWidthPage,
2020-08-21 17:49:13 +02:00
})}
2020-08-10 22:47:39 +02:00
>
<main
id={'main-content'}
className={classnames(MAIN_CLASS, className, {
'main--full-width': fullWidthPage,
'main--auth-page': authPage,
'main--file-page': filePage,
'main--settings-page': settingsPage,
'main--markdown': isMarkdown,
'main--theater-mode': isOnFilePage && videoTheaterMode && !isMediumScreen && !isMarkdown,
})}
>
{children}
2021-04-23 21:59:48 +02:00
{!isMobile && rightSide && <div className="main__right-side">{rightSide}</div>}
</main>
<StatusBar />
</div>
2019-08-27 16:43:42 +02:00
</div>
</>
2019-08-27 16:43:42 +02:00
);
}
2018-03-26 23:32:43 +02:00
export default Page;