2018-03-26 14:32:43 -07:00
// @flow
2019-08-27 10:43:42 -04:00
import type { Node } from 'react' ;
import React , { Fragment } from 'react' ;
2018-03-26 14:32:43 -07:00
import classnames from 'classnames' ;
2019-12-18 00:27:08 -05:00
import SideNavigation from 'component/sideNavigation' ;
2019-08-27 10:43:42 -04:00
import Header from 'component/header' ;
2020-05-07 14:44:11 -04:00
import Footer from 'web/component/footer' ;
2020-05-28 18:02:59 +08:00
/* @if TARGET='app' */
import StatusBar from 'component/common/status-bar' ;
/* @endif */
2020-08-10 16:47:39 -04:00
import usePersistedState from 'effects/use-persisted-state' ;
import { useHistory } from 'react-router' ;
import { useIsMobile , useIsMediumScreen } from 'effects/use-screensize' ;
import { parseURI } from 'lbry-redux' ;
2018-03-26 14:32:43 -07:00
2019-10-20 18:41:28 -04:00
export const MAIN _CLASS = 'main' ;
2018-03-26 14:32:43 -07:00
type Props = {
2019-08-27 10:43:42 -04:00
children : Node | Array < Node > ,
2019-01-09 13:39:05 -05:00
className : ? string ,
2019-08-27 10:43:42 -04:00
autoUpdateDownloaded : boolean ,
isUpgradeAvailable : boolean ,
2019-10-28 10:04:37 -04:00
authPage : boolean ,
2020-08-10 16:47:39 -04:00
filePage : boolean ,
2020-08-21 11:49:13 -04:00
homePage : boolean ,
2020-02-19 01:31:40 -05:00
noHeader : boolean ,
2020-05-08 12:48:58 -04:00
noFooter : boolean ,
2020-06-01 13:03:19 -04:00
noSideNavigation : boolean ,
2020-08-21 11:49:13 -04:00
fullWidthPage : boolean ,
2021-01-08 10:21:27 -05:00
videoTheaterMode : boolean ,
2021-03-12 11:18:09 -05:00
isMarkdown ? : boolean ,
2021-03-10 13:34:21 -05:00
livestream ? : boolean ,
2021-06-03 11:55:16 -04:00
chatDisabled : boolean ,
2021-04-23 15:59:48 -04:00
rightSide ? : Node ,
2020-07-10 17:04:36 -04:00
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
} ,
2018-03-26 14:32:43 -07:00
} ;
2019-06-17 16:32:38 -04:00
function Page ( props : Props ) {
2020-06-29 15:54:07 -04:00
const {
children ,
className ,
2020-08-10 16:47:39 -04:00
filePage = false ,
2020-08-21 11:49:13 -04:00
authPage = false ,
fullWidthPage = false ,
2020-06-29 15:54:07 -04:00
noHeader = false ,
noFooter = false ,
noSideNavigation = false ,
backout ,
2021-01-08 10:21:27 -05:00
videoTheaterMode ,
2021-03-12 11:18:09 -05:00
isMarkdown = false ,
2021-03-10 13:34:21 -05:00
livestream ,
2021-04-23 15:59:48 -04:00
rightSide ,
2021-06-03 11:55:16 -04:00
chatDisabled ,
2020-06-29 15:54:07 -04:00
} = props ;
2021-01-08 10:21:27 -05:00
2020-08-10 16:47:39 -04:00
const {
location : { pathname } ,
} = useHistory ( ) ;
2021-05-31 14:35:37 +08:00
const [ sidebarOpen , setSidebarOpen ] = usePersistedState ( 'sidebar' , false ) ;
2020-08-10 16:47:39 -04:00
const isMediumScreen = useIsMediumScreen ( ) ;
const isMobile = useIsMobile ( ) ;
2020-08-21 11:49:13 -04:00
2020-08-10 16:47:39 -04:00
let isOnFilePage = false ;
try {
const url = pathname . slice ( 1 ) . replace ( /:/g , '#' ) ;
const { isChannel } = parseURI ( url ) ;
if ( ! isChannel ) {
isOnFilePage = true ;
}
} catch ( e ) { }
const isAbsoluteSideNavHidden = ( isOnFilePage || isMobile ) && ! sidebarOpen ;
React . useEffect ( ( ) => {
if ( isOnFilePage || isMediumScreen ) {
setSidebarOpen ( false ) ;
}
2020-12-17 13:43:47 -05:00
// TODO: make sure setState callback for usePersistedState uses useCallback to it doesn't cause effect to re-run
} , [ isOnFilePage , isMediumScreen ] ) ;
2019-06-17 16:32:38 -04:00
2019-08-27 10:43:42 -04:00
return (
< Fragment >
2020-08-10 16:47:39 -04:00
{ ! noHeader && (
< Header
authHeader = { authPage }
backout = { backout }
sidebarOpen = { sidebarOpen }
isAbsoluteSideNavHidden = { isAbsoluteSideNavHidden }
setSidebarOpen = { setSidebarOpen }
/ >
) }
2021-01-08 10:21:27 -05:00
< div
className = { classnames ( 'main-wrapper__inner' , {
'main-wrapper__inner--filepage' : isOnFilePage ,
'main-wrapper__inner--theater-mode' : isOnFilePage && videoTheaterMode ,
} ) }
>
2020-08-10 16:47:39 -04:00
{ ! authPage && ! noSideNavigation && (
< SideNavigation
sidebarOpen = { sidebarOpen }
setSidebarOpen = { setSidebarOpen }
isMediumScreen = { isMediumScreen }
isOnFilePage = { isOnFilePage }
/ >
) }
< main
2020-08-21 11:49:13 -04:00
className = { classnames ( MAIN _CLASS , className , {
'main--full-width' : fullWidthPage ,
'main--auth-page' : authPage ,
'main--file-page' : filePage ,
2021-03-12 11:18:09 -05:00
'main--markdown' : isMarkdown ,
2021-03-10 13:34:21 -05:00
'main--theater-mode' : isOnFilePage && videoTheaterMode && ! livestream ,
2021-06-03 11:55:16 -04:00
'main--livestream' : livestream && ! chatDisabled ,
2020-08-21 11:49:13 -04:00
} ) }
2020-08-10 16:47:39 -04:00
>
{ children }
2021-04-23 15:59:48 -04:00
{ ! isMobile && rightSide && < div className = "main__right-side" > { rightSide } < / div > }
2020-08-10 16:47:39 -04:00
< / main >
2020-05-28 18:02:59 +08:00
{ /* @if TARGET='app' */ }
< StatusBar / >
{ /* @endif */ }
2019-08-27 10:43:42 -04:00
< / div >
2020-05-08 12:48:58 -04:00
{ /* @if TARGET='web' */ }
{ ! noFooter && < Footer / > }
{ /* @endif */ }
2019-08-27 10:43:42 -04:00
< / Fragment >
) ;
2018-05-07 00:50:55 -04:00
}
2018-03-26 14:32:43 -07:00
export default Page ;