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

416 lines
15 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
2018-11-26 02:21:25 +01:00
import * as ICONS from 'constants/icons';
import { SETTINGS } from 'lbry-redux';
2019-08-21 22:54:44 +02:00
import * as PAGES from 'constants/pages';
2020-08-11 22:32:03 +02:00
import React from 'react';
2019-06-17 22:32:38 +02:00
import { withRouter } from 'react-router';
2019-08-27 16:43:42 +02:00
import classnames from 'classnames';
2018-03-26 23:32:43 +02:00
import Button from 'component/button';
import LbcSymbol from 'component/common/lbc-symbol';
import WunderBar from 'component/wunderbar';
2019-06-17 22:32:38 +02:00
import Icon from 'component/common/icon';
import { Menu, MenuList, MenuButton, MenuItem } from '@reach/menu-button';
2019-08-27 16:43:42 +02:00
import Tooltip from 'component/common/tooltip';
import NavigationButton from 'component/navigationButton';
import { LOGO_TITLE } from 'config';
2020-08-10 22:47:39 +02:00
import { useIsMobile } from 'effects/use-screensize';
import NotificationBubble from 'component/notificationBubble';
import NotificationHeaderButton from 'component/notificationHeaderButton';
// @if TARGET='app'
import { remote } from 'electron';
import { IS_MAC } from 'component/app/view';
// @endif
2019-10-09 18:34:18 +02:00
2018-03-26 23:32:43 +02:00
type Props = {
balance: string,
2019-06-11 20:10:58 +02:00
roundedBalance: number,
history: {
entities: {}[],
goBack: () => void,
goForward: () => void,
index: number,
length: number,
location: { pathname: string },
push: string => void,
replace: string => void,
},
2019-06-17 22:32:38 +02:00
currentTheme: string,
automaticDarkModeEnabled: boolean,
setClientSetting: (string, boolean | string) => void,
hideBalance: boolean,
2019-08-21 22:54:44 +02:00
email: ?string,
authenticated: boolean,
2019-10-28 15:04:37 +01:00
authHeader: 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
},
syncError: ?string,
emailToVerify?: string,
2019-09-26 18:07:11 +02:00
signOut: () => void,
2020-01-03 20:11:47 +01:00
openChannelCreate: () => void,
openSignOutModal: () => void,
clearEmailEntry: () => void,
clearPasswordEntry: () => void,
hasNavigated: boolean,
syncSettings: () => void,
2020-08-10 22:47:39 +02:00
sidebarOpen: boolean,
setSidebarOpen: boolean => void,
isAbsoluteSideNavHidden: boolean,
2018-03-26 23:32:43 +02:00
};
const Header = (props: Props) => {
2019-08-21 22:54:44 +02:00
const {
roundedBalance,
history,
setClientSetting,
currentTheme,
automaticDarkModeEnabled,
hideBalance,
email,
authenticated,
2019-10-28 15:04:37 +01:00
authHeader,
2019-09-26 18:07:11 +02:00
signOut,
syncError,
openSignOutModal,
clearEmailEntry,
clearPasswordEntry,
emailToVerify,
2020-06-29 21:54:07 +02:00
backout,
syncSettings,
2020-08-10 22:47:39 +02:00
sidebarOpen,
setSidebarOpen,
isAbsoluteSideNavHidden,
2019-08-21 22:54:44 +02:00
} = props;
const isMobile = useIsMobile();
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
2019-11-14 01:33:36 +01:00
const isVerifyPage = history.location.pathname.includes(PAGES.AUTH_VERIFY);
const isSignUpPage = history.location.pathname.includes(PAGES.AUTH);
const isSignInPage = history.location.pathname.includes(PAGES.AUTH_SIGNIN);
const isPwdResetPage = history.location.pathname.includes(PAGES.AUTH_PASSWORD_RESET);
const hasBackout = Boolean(backout);
const { backLabel, backNavDefault, title: backTitle, simpleTitle: simpleBackTitle } = backout || {};
2019-11-14 01:33:36 +01:00
// Sign out if they click the "x" when they are on the password prompt
2020-01-20 17:47:03 +01:00
const authHeaderAction = syncError ? { onClick: signOut } : { navigate: '/' };
const homeButtonNavigationProps = isVerifyPage ? {} : authHeader ? authHeaderAction : { navigate: '/' };
const closeButtonNavigationProps = {
onClick: () => {
clearEmailEntry();
clearPasswordEntry();
if (isSignInPage && !emailToVerify) {
history.goBack();
}
if (isSignUpPage) {
history.goBack();
}
if (isPwdResetPage) {
history.goBack();
}
if (syncError) {
signOut();
}
},
};
2018-03-26 23:32:43 +02:00
function onBackout(e) {
const { history, hasNavigated } = props;
const { goBack, 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-27 16:43:55 +02:00
React.useEffect(() => {
if (hasBackout) {
window.addEventListener('popstate', onBackout);
return () => window.removeEventListener('popstate', onBackout);
}
}, [hasBackout]);
2019-06-17 22:32:38 +02:00
function handleThemeToggle() {
if (automaticDarkModeEnabled) {
setClientSetting(SETTINGS.AUTOMATIC_DARK_MODE_ENABLED, false);
}
if (currentTheme === 'dark') {
setClientSetting(SETTINGS.THEME, 'light');
} else {
setClientSetting(SETTINGS.THEME, 'dark');
}
syncSettings();
2019-06-17 22:32:38 +02:00
}
2018-03-26 23:32:43 +02:00
2019-09-26 18:07:11 +02:00
function getWalletTitle() {
return hideBalance || Number(roundedBalance) === 0 ? (
__('Your Wallet')
2019-09-26 18:07:11 +02:00
) : (
<React.Fragment>
{roundedBalance} <LbcSymbol />
</React.Fragment>
);
2019-08-21 22:54:44 +02:00
}
2017-06-06 23:19:12 +02:00
return (
<header
className={classnames('header', {
2019-10-28 15:04:37 +01:00
'header--minimal': authHeader,
// @if TARGET='app'
'header--mac': IS_MAC,
// @endif
})}
// @if TARGET='app'
onDoubleClick={e => {
remote.getCurrentWindow().maximize();
}}
// @endif
>
2019-06-11 20:10:58 +02:00
<div className="header__contents">
2020-06-29 21:54:07 +02:00
{!authHeader && backout ? (
<div className="card__actions--between">
<Button
onClick={onBackout}
button="link"
label={(backLabel && backLabel) || __('Cancel')}
icon={ICONS.ARROW_LEFT}
/>
{backTitle && <h1 className={'card__title'}>{isMobile ? simpleBackTitle || backTitle : backTitle}</h1>}
2020-06-29 21:54:07 +02:00
<Button
aria-label={__('Your wallet')}
navigate={`/$/${PAGES.WALLET}`}
className="header__navigation-item menu__title header__navigation-item--balance"
label={getWalletTitle()}
// @if TARGET='app'
onDoubleClick={e => {
e.stopPropagation();
}}
// @endif
/>
</div>
) : (
<>
<div className="header__navigation">
2020-08-11 22:32:03 +02:00
{!authHeader && (
<span style={{ position: 'relative' }}>
<Button
className="header__navigation-item menu__title header__navigation-item--icon"
icon={ICONS.MENU}
onClick={() => setSidebarOpen(!sidebarOpen)}
>
{isAbsoluteSideNavHidden && isMobile && <NotificationBubble />}
2020-08-11 22:32:03 +02:00
</Button>
</span>
)}
2020-06-29 21:54:07 +02:00
<Button
className="header__navigation-item header__navigation-item--lbry header__navigation-item--button-mobile"
2020-07-09 00:31:23 +02:00
// @if TARGET='app'
label={'LBRY'}
// @endif
// @if TARGET='web'
label={LOGO_TITLE} // eslint-disable-line
// @endif
2020-06-29 21:54:07 +02:00
icon={ICONS.LBRY}
onClick={() => {
if (history.location.pathname === '/') window.location.reload();
}}
// @if TARGET='app'
onDoubleClick={e => {
e.stopPropagation();
}}
// @endif
{...homeButtonNavigationProps}
/>
{!authHeader && (
2020-08-10 22:47:39 +02:00
<div className="header__center">
{/* @if TARGET='app' */}
{!authHeader && (
<div className="header__buttons">
<NavigationButton isBackward history={history} />
<NavigationButton isBackward={false} history={history} />
</div>
)}
{/* @endif */}
2019-09-30 23:48:30 +02:00
2020-08-10 22:47:39 +02:00
{!authHeader && <WunderBar />}
2019-08-27 16:43:42 +02:00
2020-08-10 22:47:39 +02:00
<div className="header__buttons mobile-hidden">
2020-06-29 21:54:07 +02:00
<Menu>
<MenuButton
aria-label={__('Publish a file, or create a channel')}
title={__('Publish a file, or create a channel')}
className="header__navigation-item menu__title header__navigation-item--icon"
// @if TARGET='app'
onDoubleClick={e => {
e.stopPropagation();
}}
// @endif
>
<Icon size={18} icon={ICONS.PUBLISH} aria-hidden />
</MenuButton>
2020-08-10 22:47:39 +02:00
<NotificationHeaderButton />
2020-06-29 21:54:07 +02:00
<MenuList className="menu__list--header">
2020-07-23 19:02:07 +02:00
<MenuItem className="menu__link" onSelect={() => history.push(`/$/${PAGES.UPLOAD}`)}>
2020-06-29 21:54:07 +02:00
<Icon aria-hidden icon={ICONS.PUBLISH} />
2020-07-23 19:02:07 +02:00
{__('Upload')}
2020-06-29 21:54:07 +02:00
</MenuItem>
2020-07-02 18:18:59 +02:00
<MenuItem className="menu__link" onSelect={() => history.push(`/$/${PAGES.CHANNEL_NEW}`)}>
2020-06-29 21:54:07 +02:00
<Icon aria-hidden icon={ICONS.CHANNEL} />
{__('New Channel')}
</MenuItem>
</MenuList>
</Menu>
2019-08-27 16:43:42 +02:00
2020-06-29 21:54:07 +02:00
<Menu>
<MenuButton
aria-label={__('Your account')}
title={__('Your account')}
className="header__navigation-item menu__title header__navigation-item--icon"
// @if TARGET='app'
onDoubleClick={e => {
e.stopPropagation();
}}
// @endif
>
<Icon size={18} icon={ICONS.ACCOUNT} aria-hidden />
</MenuButton>
<MenuList className="menu__list--header">
2020-07-23 19:02:07 +02:00
<MenuItem className="menu__link" onSelect={() => history.push(`/$/${PAGES.UPLOADS}`)}>
2020-06-29 21:54:07 +02:00
<Icon aria-hidden icon={ICONS.PUBLISH} />
2020-07-23 19:02:07 +02:00
{__('Uploads')}
2020-06-29 21:54:07 +02:00
</MenuItem>
<MenuItem className="menu__link" onSelect={() => history.push(`/$/${PAGES.CHANNELS}`)}>
<Icon aria-hidden icon={ICONS.CHANNEL} />
{__('Channels')}
</MenuItem>
<MenuItem className="menu__link" onSelect={() => history.push(`/$/${PAGES.CREATOR_DASHBOARD}`)}>
<Icon aria-hidden icon={ICONS.ANALYTICS} />
{__('Creator Analytics')}
</MenuItem>
<MenuItem className="menu__link" onSelect={() => history.push(`/$/${PAGES.REWARDS}`)}>
<Icon aria-hidden icon={ICONS.REWARDS} />
{__('Rewards')}
</MenuItem>
<MenuItem className="menu__link" onSelect={() => history.push(`/$/${PAGES.INVITE}`)}>
<Icon aria-hidden icon={ICONS.INVITE} />
{__('Invites')}
</MenuItem>
2020-01-03 17:43:51 +01:00
2020-06-29 21:54:07 +02:00
{authenticated ? (
<MenuItem onSelect={IS_WEB ? signOut : openSignOutModal}>
<div className="menu__link">
<Icon aria-hidden icon={ICONS.SIGN_OUT} />
{__('Sign Out')}
</div>
<span className="menu__link-help">{email}</span>
</MenuItem>
2020-08-20 17:02:56 +02:00
) : !IS_WEB ? (
<MenuItem className="menu__link" onSelect={() => history.push(`/$/${PAGES.AUTH}`)}>
<Icon aria-hidden icon={ICONS.INVITE} />
{__('Sign In')}
</MenuItem>
2020-08-11 22:32:03 +02:00
) : null}
2020-06-29 21:54:07 +02:00
</MenuList>
</Menu>
2020-08-10 22:47:39 +02:00
<Menu>
<MenuButton
aria-label={__('Settings')}
title={__('Settings')}
className="header__navigation-item menu__title header__navigation-item--icon"
// @if TARGET='app'
onDoubleClick={e => {
e.stopPropagation();
}}
// @endif
>
<Icon size={18} icon={ICONS.SETTINGS} aria-hidden />
</MenuButton>
<MenuList className="menu__list--header">
<MenuItem className="menu__link" onSelect={() => history.push(`/$/${PAGES.SETTINGS}`)}>
<Icon aria-hidden tootlip icon={ICONS.SETTINGS} />
{__('Settings')}
</MenuItem>
<MenuItem className="menu__link" onSelect={() => history.push(`/$/${PAGES.HELP}`)}>
<Icon aria-hidden icon={ICONS.HELP} />
{__('Help')}
</MenuItem>
<MenuItem className="menu__link" onSelect={handleThemeToggle}>
<Icon icon={currentTheme === 'light' ? ICONS.DARK : ICONS.LIGHT} />
{currentTheme === 'light' ? __('Dark') : __('Light')}
</MenuItem>
</MenuList>
</Menu>
</div>
</div>
)}
</div>
2020-08-11 22:32:03 +02:00
{!authHeader && !backout ? (
2020-08-10 22:47:39 +02:00
<div className={classnames('header__menu', { 'header__menu--with-balance': !IS_WEB || authenticated })}>
{(!IS_WEB || authenticated) && (
2020-08-11 22:32:03 +02:00
<Button
aria-label={__('Your wallet')}
navigate={`/$/${PAGES.WALLET}`}
className="header__navigation-item menu__title header__navigation-item--balance"
label={getWalletTitle()}
// @if TARGET='app'
onDoubleClick={e => {
e.stopPropagation();
}}
// @endif
/>
2020-06-29 21:54:07 +02:00
)}
2020-07-23 16:22:57 +02:00
2020-06-29 21:54:07 +02:00
{IS_WEB && !authenticated && (
<div className="header__auth-buttons">
<Button navigate={`/$/${PAGES.AUTH_SIGNIN}`} button="link" label={__('Sign In')} />
<Button navigate={`/$/${PAGES.AUTH}`} button="primary" label={__('Register')} />
</div>
)}
</div>
2020-06-29 21:54:07 +02:00
) : (
!isVerifyPage && (
<div className="header__menu">
{/* Add an empty span here so we can use the same style as above */}
{/* This pushes the close button to the right side */}
<span />
<Tooltip label={__('Go Back')}>
<Button
button="alt"
// className="button--header-close"
icon={ICONS.REMOVE}
{...closeButtonNavigationProps}
// @if TARGET='app'
onDoubleClick={e => {
e.stopPropagation();
}}
// @endif
/>
</Tooltip>
</div>
)
2019-08-27 16:43:42 +02:00
)}
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
};
2019-06-17 22:32:38 +02:00
export default withRouter(Header);