2018-03-26 23:32:43 +02:00
// @flow
2022-01-27 03:14:36 +01:00
// import 'scss/component/_header.scss'; // ody codesplits this. no.
2022-01-26 20:54:58 +01:00
import { useIsMobile } from 'effects/use-screensize' ;
import { withRouter } from 'react-router' ;
2018-11-26 02:21:25 +01:00
import * as ICONS from 'constants/icons' ;
2019-08-21 22:54:44 +02:00
import * as PAGES from 'constants/pages' ;
2018-03-26 23:32:43 +02:00
import Button from 'component/button' ;
2022-01-26 20:54:58 +01:00
import classnames from 'classnames' ;
import HeaderMenuButtons from 'component/headerMenuButtons' ;
import HeaderProfileMenuButton from 'component/headerProfileMenuButton' ;
import Logo from 'component/logo' ;
2020-08-10 22:47:39 +02:00
import NotificationBubble from 'component/notificationBubble' ;
2022-01-26 20:54:58 +01:00
import React from 'react' ;
2021-07-21 02:31:26 +02:00
import SkipNavigationButton from 'component/skipNavigationButton' ;
2022-01-27 03:14:36 +01:00
import Tooltip from 'component/common/tooltip' ;
2022-01-26 20:54:58 +01:00
import WunderBar from 'component/wunderbar' ;
2022-01-02 21:33:11 +01:00
import * as remote from '@electron/remote' ;
2019-10-15 18:21:49 +02:00
import { IS _MAC } from 'component/app/view' ;
2022-01-26 20:54:58 +01:00
import NavigationButton from 'component/navigationButton' ;
2019-10-09 18:34:18 +02:00
2018-03-26 23:32:43 +02:00
type Props = {
2019-10-28 15:04:37 +01:00
authHeader : boolean ,
2020-06-30 07:51:15 +02:00
backout : {
2020-07-10 23:04:36 +02:00
backLabel ? : string ,
backNavDefault ? : string ,
2020-06-30 07:51:15 +02:00
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
} ,
2022-01-26 20:54:58 +01:00
balance : number ,
2020-04-13 21:16:07 +02:00
emailToVerify ? : string ,
2020-07-10 23:04:36 +02:00
hasNavigated : boolean ,
2022-01-26 20:54:58 +01:00
hideBalance : boolean ,
hideCancel : boolean ,
history : {
goBack : ( ) => void ,
location : { pathname : string } ,
push : ( string ) => void ,
replace : ( string ) => void ,
} ,
isAbsoluteSideNavHidden : boolean ,
roundedBalance : string ,
roundedSpendableBalance : string ,
2020-08-10 22:47:39 +02:00
sidebarOpen : boolean ,
2022-01-26 20:54:58 +01:00
syncError : ? string ,
clearEmailEntry : ( ) => void ,
clearPasswordEntry : ( ) => void ,
2021-03-03 19:50:16 +01:00
setSidebarOpen : ( boolean ) => void ,
2022-01-26 20:54:58 +01:00
signOut : ( ) => void ,
2018-03-26 23:32:43 +02:00
} ;
const Header = ( props : Props ) => {
2019-08-21 22:54:44 +02:00
const {
2022-01-26 20:54:58 +01:00
authHeader ,
backout ,
2021-01-25 19:34:21 +01:00
balance ,
2022-01-26 20:54:58 +01:00
emailToVerify ,
hideBalance ,
hideCancel ,
history ,
isAbsoluteSideNavHidden ,
2019-08-21 22:54:44 +02:00
roundedBalance ,
2021-01-25 19:34:21 +01:00
roundedSpendableBalance ,
2022-01-26 20:54:58 +01:00
sidebarOpen ,
2019-11-01 17:19:28 +01:00
syncError ,
2020-04-13 21:16:07 +02:00
clearEmailEntry ,
clearPasswordEntry ,
2020-08-10 22:47:39 +02:00
setSidebarOpen ,
2022-01-26 20:54:58 +01:00
signOut ,
2019-08-21 22:54:44 +02:00
} = props ;
2022-01-26 20:54:58 +01:00
const {
location : { pathname } ,
goBack ,
push ,
} = history ;
2020-06-30 07:51:15 +02:00
const isMobile = useIsMobile ( ) ;
2022-01-26 20:54:58 +01:00
2019-11-22 22:13:00 +01:00
// on the verify page don't let anyone escape other than by closing the tab to keep session data consistent
2022-01-26 20:54:58 +01:00
const isVerifyPage = pathname . includes ( PAGES . AUTH _VERIFY ) ;
const isSignUpPage = pathname . includes ( PAGES . AUTH ) ;
const isSignInPage = pathname . includes ( PAGES . AUTH _SIGNIN ) ;
const isPwdResetPage = pathname . includes ( PAGES . AUTH _PASSWORD _RESET ) ;
// For pages that allow for "backing out", shows a backout option instead of the Home logo
const canBackout = Boolean ( backout ) ;
2020-07-30 21:14:22 +02:00
const { backLabel , backNavDefault , title : backTitle , simpleTitle : simpleBackTitle } = backout || { } ;
2021-02-09 17:05:56 +01:00
2019-11-01 17:19:28 +01:00
// Sign out if they click the "x" when they are on the password prompt
2022-01-26 20:54:58 +01:00
const authHeaderAction = syncError && { onClick : signOut } ;
const homeButtonNavigationProps = ( isVerifyPage && { } ) || ( authHeader && authHeaderAction ) || { navigate : '/' } ;
const sidebarLabel = sidebarOpen
? _ _ ( 'Close sidebar - hide channels you are following.' )
: _ _ ( 'Expand sidebar - view channels you are following.' ) ;
const onBackout = React . useCallback (
( e : any ) => {
const { hasNavigated } = props ;
const { replace } = history ;
window . removeEventListener ( 'popstate' , onBackout ) ;
if ( e . type !== 'popstate' ) {
// if not initiated by pop (back button)
if ( hasNavigated && ! backNavDefault ) {
goBack ( ) ;
} else {
replace ( backNavDefault || ` / ` ) ;
}
2020-07-07 11:47:52 +02:00
}
2020-04-13 21:16:07 +02:00
} ,
2022-01-26 20:54:58 +01:00
[ backNavDefault , goBack , history , props ]
) ;
2020-07-10 23:04:36 +02:00
2020-07-27 16:43:55 +02:00
React . useEffect ( ( ) => {
2022-01-26 20:54:58 +01:00
if ( canBackout ) {
2020-07-27 16:43:55 +02:00
window . addEventListener ( 'popstate' , onBackout ) ;
return ( ) => window . removeEventListener ( 'popstate' , onBackout ) ;
}
2022-01-26 20:54:58 +01:00
} , [ canBackout , onBackout ] ) ;
2022-01-27 03:14:36 +01:00
const userButtons = ( hideWallet ? : boolean , hideProfile ? : boolean ) => (
< div className = "header__menu--right" >
< Tooltip
2022-01-26 20:54:58 +01:00
title = {
balance > 0
? _ _ ( 'Immediately spendable: %spendable_balance%' , { spendable _balance : roundedSpendableBalance } )
: _ _ ( 'Your Wallet' )
}
2022-01-27 03:14:36 +01:00
>
< div >
< Button
// title={
// balance > 0
// ? __('Immediately spendable: %spendable_balance%', { spendable_balance: roundedSpendableBalance })
// : __('Your Wallet')
// }
navigate = { ` / $ / ${ PAGES . WALLET } ` }
className = "button--file-action header__navigationItem--balance"
label = { hideBalance || Number ( roundedBalance ) === 0 ? _ _ ( 'Your Wallet' ) : roundedBalance }
icon = { ICONS . LBC }
onDoubleClick = { ( e ) => {
e . stopPropagation ( ) ;
} }
/ >
< / div >
< / Tooltip >
2018-03-26 23:32:43 +02:00
2022-01-26 20:54:58 +01:00
< HeaderProfileMenuButton / >
< / div >
2021-01-25 19:34:21 +01:00
) ;
2017-06-06 23:19:12 +02:00
return (
2019-10-15 18:21:49 +02:00
< header
className = { classnames ( 'header' , {
2019-10-28 15:04:37 +01:00
'header--minimal' : authHeader ,
2019-10-15 18:21:49 +02:00
'header--mac' : IS _MAC ,
} ) }
2020-05-18 06:26:30 +02:00
// @if TARGET='app'
2021-03-03 19:50:16 +01:00
onDoubleClick = { ( e ) => {
2020-05-18 06:26:30 +02:00
remote . getCurrentWindow ( ) . maximize ( ) ;
} }
// @endif
2019-10-15 18:21:49 +02:00
>
2022-01-27 03:14:36 +01:00
< div className = "card__actions--between header__contents" >
2022-01-26 20:54:58 +01:00
{ ! authHeader && canBackout ? (
2022-01-27 03:14:36 +01:00
< >
< div className = "header__menu--left" >
< Button onClick = { onBackout } button = "link" label = { backLabel || _ _ ( 'Cancel' ) } icon = { ICONS . ARROW _LEFT } / >
< / div >
{ backTitle && < h1 className = "header__authTitle" > { ( isMobile && simpleBackTitle ) || backTitle } < / h1 > }
{ userButtons ( false , isMobile ) }
< / >
2020-06-29 21:54:07 +02:00
) : (
< >
< div className = "header__navigation" >
2022-01-27 03:14:36 +01:00
< div className = "header__menu--left" >
< SkipNavigationButton / >
{ ! authHeader && (
< span style = { { position : 'relative' } } >
< Button
aria - label = { sidebarLabel }
className = "header__navigationItem--icon"
icon = { ICONS . MENU }
aria - expanded = { sidebarOpen }
onClick = { ( ) => setSidebarOpen ( ! sidebarOpen ) }
>
{ isAbsoluteSideNavHidden && isMobile && < NotificationBubble / > }
< / Button >
< / span >
) }
< Button
aria - label = { _ _ ( 'Home' ) }
className = "header__navigationItem--logo"
onClick = { ( ) => {
// here use state.router.location.pathname
if ( history . location . pathname === '/' ) window . location . reload ( ) ;
} }
onDoubleClick = { ( e ) => {
e . stopPropagation ( ) ;
} }
{ ... homeButtonNavigationProps }
>
< Logo / >
< / Button >
< / div >
2022-01-26 20:54:58 +01:00
2020-06-29 21:54:07 +02:00
{ ! authHeader && (
2020-08-10 22:47:39 +02:00
< div className = "header__center" >
{ ! authHeader && (
< div className = "header__buttons" >
< NavigationButton isBackward history = { history } / >
< NavigationButton isBackward = { false } history = { history } / >
< / div >
) }
2022-01-26 20:54:58 +01:00
< WunderBar / >
< HeaderMenuButtons / >
2020-08-10 22:47:39 +02:00
< / div >
) }
2022-01-27 03:14:36 +01:00
{ ! authHeader && ! canBackout
? userButtons ( isMobile )
: ! isVerifyPage &&
! hideCancel && (
< div className = "header__menu--right" >
< Button
title = { _ _ ( 'Go Back' ) }
button = "alt"
// className="button--header-close"
icon = { ICONS . REMOVE }
onClick = { ( ) => {
clearEmailEntry ( ) ;
clearPasswordEntry ( ) ;
2022-01-26 20:54:58 +01:00
2022-01-27 03:14:36 +01:00
if ( syncError ) signOut ( ) ;
if ( ( isSignInPage && ! emailToVerify ) || isSignUpPage || isPwdResetPage ) {
goBack ( ) ;
} else {
push ( '/' ) ;
}
} }
/ >
< / div >
) }
< / div >
2020-06-29 21:54:07 +02:00
< / >
2019-08-27 16:43:42 +02:00
) }
2017-06-06 23:19:12 +02:00
< / div >
< / header >
) ;
2017-06-06 06:21:55 +02:00
} ;
2016-08-27 16:12:56 +02:00
2019-06-17 22:32:38 +02:00
export default withRouter ( Header ) ;