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

369 lines
10 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
2020-08-10 22:47:39 +02:00
import type { Node } from 'react';
2019-03-28 17:53:13 +01:00
import * as PAGES from 'constants/pages';
import * as ICONS from 'constants/icons';
2019-09-26 18:07:11 +02:00
import React from 'react';
2018-03-26 23:32:43 +02:00
import Button from 'component/button';
2020-05-21 17:38:28 +02:00
import classnames from 'classnames';
2020-08-10 22:47:39 +02:00
import NotificationBubble from 'component/notificationBubble';
// @if TARGET='app'
import { IS_MAC } from 'component/app/view';
// @endif
2020-01-03 20:11:47 +01:00
2020-08-10 22:47:39 +02:00
const ESCAPE_KEY_CODE = 27;
const BACKSLASH_KEY_CODE = 220;
2020-08-11 22:32:03 +02:00
2020-08-21 17:18:47 +02:00
const HOME = {
label: 'Home',
navigate: `/`,
icon: ICONS.HOME,
};
const RECENT_FROM_FOLLOWING = {
label: 'Following',
navigate: `/$/${PAGES.CHANNELS_FOLLOWING}`,
icon: ICONS.SUBSCRIBE,
};
2018-03-26 23:32:43 +02:00
type Props = {
2019-06-11 20:10:58 +02:00
subscriptions: Array<Subscription>,
2019-08-21 22:54:44 +02:00
email: ?string,
uploadCount: number,
2019-12-18 06:27:08 +01:00
doSignOut: () => void,
2020-08-10 22:47:39 +02:00
sidebarOpen: boolean,
setSidebarOpen: boolean => void,
isMediumScreen: boolean,
isOnFilePage: boolean,
unreadCount: number,
2020-05-21 17:38:28 +02:00
purchaseSuccess: boolean,
doClearPurchasedUriSuccess: () => void,
2020-08-11 22:32:03 +02:00
user: ?User,
2018-03-26 23:32:43 +02:00
};
2020-01-02 17:30:27 +01:00
function SideNavigation(props: Props) {
2019-12-18 06:27:08 +01:00
const {
subscriptions,
2020-08-20 17:10:29 +02:00
doSignOut,
2019-12-18 06:27:08 +01:00
email,
2020-05-21 17:38:28 +02:00
purchaseSuccess,
doClearPurchasedUriSuccess,
2020-08-10 22:47:39 +02:00
sidebarOpen,
setSidebarOpen,
isMediumScreen,
isOnFilePage,
unreadCount,
2020-08-11 22:32:03 +02:00
user,
2019-12-18 06:27:08 +01:00
} = props;
2020-08-20 17:10:29 +02:00
const TOP_LEVEL_LINKS: Array<{
label: string,
navigate: string,
icon: string,
extra?: Node,
hideForUnauth?: boolean,
}> = [
2020-08-21 17:18:47 +02:00
HOME,
RECENT_FROM_FOLLOWING,
2020-08-20 17:10:29 +02:00
{
label: 'Your Tags',
navigate: `/$/${PAGES.TAGS_FOLLOWING}`,
icon: ICONS.TAG,
hideForUnauth: true,
},
{
label: 'Discover',
navigate: `/$/${PAGES.DISCOVER}`,
icon: ICONS.DISCOVER,
},
{
label: IS_WEB ? 'Purchased' : 'Library',
2020-08-20 17:10:29 +02:00
navigate: `/$/${PAGES.LIBRARY}`,
icon: ICONS.PURCHASED,
hideForUnauth: true,
},
];
const ABSOLUTE_LINKS: Array<{
label: string,
navigate?: string,
onClick?: () => any,
icon: string,
extra?: Node,
hideForUnauth?: boolean,
}> = [
{
label: 'Upload',
navigate: `/$/${PAGES.UPLOAD}`,
icon: ICONS.PUBLISH,
},
{
label: 'New Channel',
navigate: `/$/${PAGES.CHANNEL_NEW}`,
icon: ICONS.CHANNEL,
hideForUnauth: true,
},
{
label: 'Uploads',
navigate: `/$/${PAGES.UPLOADS}`,
icon: ICONS.PUBLISH,
hideForUnauth: true,
},
{
label: 'Channels',
navigate: `/$/${PAGES.CHANNELS}`,
icon: ICONS.CHANNEL,
hideForUnauth: true,
},
{
label: 'Creator Analytics',
navigate: `/$/${PAGES.CREATOR_DASHBOARD}`,
icon: ICONS.ANALYTICS,
hideForUnauth: true,
},
{
label: 'Wallet',
navigate: `/$/${PAGES.WALLET}`,
icon: ICONS.WALLET,
hideForUnauth: true,
},
{
label: 'Notifications',
navigate: `/$/${PAGES.NOTIFICATIONS}`,
icon: ICONS.NOTIFICATION,
extra: <NotificationBubble inline />,
hideForUnauth: true,
},
{
label: 'Rewards',
navigate: `/$/${PAGES.REWARDS}`,
icon: ICONS.REWARDS,
hideForUnauth: true,
},
{
label: 'Invites',
navigate: `/$/${PAGES.INVITE}`,
icon: ICONS.INVITE,
hideForUnauth: true,
},
{
label: 'Settings',
navigate: `/$/${PAGES.SETTINGS}`,
icon: ICONS.SETTINGS,
hideForUnauth: true,
},
{
label: 'Help',
navigate: `/$/${PAGES.HELP}`,
icon: ICONS.HELP,
hideForUnauth: true,
},
{
label: 'Sign Out',
onClick: doSignOut,
icon: ICONS.SIGN_OUT,
hideForUnauth: true,
},
];
const UNAUTH_LINKS: Array<{
label: string,
navigate: string,
icon: string,
extra?: Node,
}> = [
{
label: 'Sign In',
navigate: `/$/${PAGES.AUTH_SIGNIN}`,
icon: ICONS.SIGN_IN,
},
{
2020-08-21 06:24:02 +02:00
label: 'Sign Up',
2020-08-20 17:10:29 +02:00
navigate: `/$/${PAGES.AUTH}`,
icon: ICONS.SIGN_UP,
},
{
label: 'Settings',
navigate: `/$/${PAGES.SETTINGS}`,
icon: ICONS.SETTINGS,
},
{
label: 'Help',
navigate: `/$/${PAGES.HELP}`,
icon: ICONS.HELP,
},
];
2020-08-11 22:32:03 +02:00
const notificationsEnabled = user && user.experimental_ui;
2019-12-18 06:27:08 +01:00
const isAuthenticated = Boolean(email);
2020-05-21 17:38:28 +02:00
const [pulseLibrary, setPulseLibrary] = React.useState(false);
const isPersonalized = !IS_WEB || isAuthenticated;
2020-08-10 22:47:39 +02:00
const isAbsolute = isOnFilePage || isMediumScreen;
2020-08-11 22:32:03 +02:00
const microNavigation = !sidebarOpen || isMediumScreen;
const subLinks = email
? ABSOLUTE_LINKS.filter(link => {
if (!notificationsEnabled && link.icon === ICONS.NOTIFICATION) {
return false;
}
return true;
})
: UNAUTH_LINKS;
2019-12-18 06:27:08 +01:00
2020-05-21 17:38:28 +02:00
React.useEffect(() => {
if (purchaseSuccess) {
setPulseLibrary(true);
let timeout = setTimeout(() => {
setPulseLibrary(false);
doClearPurchasedUriSuccess();
}, 2500);
return () => clearTimeout(timeout);
}
}, [setPulseLibrary, purchaseSuccess, doClearPurchasedUriSuccess]);
2020-08-10 22:47:39 +02:00
React.useEffect(() => {
function handleKeydown(e: SyntheticKeyboardEvent<*>) {
if (e.keyCode === ESCAPE_KEY_CODE && isAbsolute) {
setSidebarOpen(false);
} else if (e.keyCode === BACKSLASH_KEY_CODE) {
const hasActiveInput = document.querySelector('input:focus');
if (!hasActiveInput) {
setSidebarOpen(!sidebarOpen);
}
}
}
window.addEventListener('keydown', handleKeydown);
return () => window.removeEventListener('keydown', handleKeydown);
}, [sidebarOpen, setSidebarOpen, isAbsolute]);
2019-04-19 23:15:41 +02:00
2020-01-24 19:31:49 +01:00
return (
2020-08-11 22:32:03 +02:00
<div
className={classnames('navigation__wrapper', {
'navigation__wrapper--micro': microNavigation && !isOnFilePage,
'navigation__wrapper--absolute': isAbsolute,
})}
>
2020-08-10 22:47:39 +02:00
{!isOnFilePage && (
<nav
className={classnames('navigation', {
'navigation--micro': microNavigation,
// @if TARGET='app'
'navigation--mac': IS_MAC,
// @endif
})}
>
2020-08-11 22:32:03 +02:00
<ul className={classnames('navigation-links', { 'navigation-links--micro': !sidebarOpen })}>
2020-08-10 22:47:39 +02:00
{TOP_LEVEL_LINKS.map(linkProps =>
!email && linkProps.hideForUnauth && IS_WEB ? null : (
2020-03-11 20:57:21 +01:00
<li key={linkProps.navigate}>
2020-05-21 17:38:28 +02:00
<Button
{...linkProps}
label={__(linkProps.label)}
2020-05-21 17:38:28 +02:00
icon={pulseLibrary && linkProps.icon === ICONS.LIBRARY ? ICONS.PURCHASED : linkProps.icon}
className={classnames('navigation-link', {
'navigation-link--pulse': linkProps.icon === ICONS.LIBRARY && pulseLibrary,
2020-08-11 22:32:03 +02:00
'navigation-link--highlighted': linkProps.icon === ICONS.NOTIFICATION && unreadCount > 0,
2020-05-21 17:38:28 +02:00
})}
activeClass="navigation-link--active"
/>
2020-08-10 22:47:39 +02:00
{linkProps.extra}
2020-03-11 20:57:21 +01:00
</li>
)
2020-08-10 22:47:39 +02:00
)}
</ul>
{sidebarOpen && isPersonalized && subscriptions && subscriptions.length > 0 && (
2020-08-11 22:32:03 +02:00
<ul className="navigation__secondary navigation-links navigation-links--small">
2020-08-10 22:47:39 +02:00
{subscriptions.map(({ uri, channelName }, index) => (
<li key={uri} className="navigation-link__wrapper">
<Button
navigate={uri}
label={channelName}
className="navigation-link"
activeClass="navigation-link--active"
/>
</li>
))}
</ul>
2020-03-11 20:57:21 +01:00
)}
2020-08-10 22:47:39 +02:00
</nav>
)}
2019-12-18 06:27:08 +01:00
2020-08-10 22:47:39 +02:00
{(isOnFilePage || isMediumScreen) && sidebarOpen && (
<>
<nav
className={classnames('navigation--absolute', {
// @if TARGET='app'
'navigation--mac': IS_MAC,
// @endif
})}
>
2020-08-10 22:47:39 +02:00
<ul className="navigation-links--absolute">
2020-08-11 22:32:03 +02:00
{TOP_LEVEL_LINKS.map(linkProps =>
!email && linkProps.hideForUnauth && IS_WEB ? null : (
<li key={linkProps.navigate}>
<Button
{...linkProps}
label={__(linkProps.label)}
2020-08-11 22:32:03 +02:00
icon={pulseLibrary && linkProps.icon === ICONS.LIBRARY ? ICONS.PURCHASED : linkProps.icon}
className={classnames('navigation-link', {
'navigation-link--pulse': linkProps.icon === ICONS.LIBRARY && pulseLibrary,
'navigation-link--highlighted': linkProps.icon === ICONS.NOTIFICATION && unreadCount > 0,
})}
activeClass="navigation-link--active"
/>
{linkProps.extra}
</li>
)
)}
2020-08-10 22:47:39 +02:00
</ul>
<ul className="navigation-links--absolute">
2020-08-11 22:32:03 +02:00
{subLinks.map(linkProps =>
!email && linkProps.hideForUnauth && IS_WEB ? null : (
<li key={linkProps.navigate} className="mobile-only">
<Button
{...linkProps}
label={__(linkProps.label)}
className="navigation-link"
activeClass="navigation-link--active"
/>
2020-08-11 22:32:03 +02:00
{linkProps.extra}
</li>
)
)}
2020-08-10 22:47:39 +02:00
</ul>
{isPersonalized && subscriptions && subscriptions.length > 0 && (
<ul className="navigation__secondary navigation-links--small">
{subscriptions.map(({ uri, channelName }, index) => (
<li key={uri} className="navigation-link__wrapper">
<Button
navigate={uri}
label={channelName}
className="navigation-link"
activeClass="navigation-link--active"
/>
2019-12-19 21:43:49 +01:00
</li>
2020-08-10 22:47:39 +02:00
))}
</ul>
2019-12-19 21:43:49 +01:00
)}
2020-08-10 22:47:39 +02:00
</nav>
<div
className={classnames('navigation__overlay', {
// @if TARGET='app'
'navigation__overlay--mac': IS_MAC,
// @endif
})}
onClick={() => setSidebarOpen(false)}
/>
2020-08-10 22:47:39 +02:00
</>
)}
2020-08-11 22:32:03 +02:00
</div>
2019-06-11 20:10:58 +02:00
);
}
2018-03-26 23:32:43 +02:00
2020-08-10 22:47:39 +02:00
export default SideNavigation;