Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: new UI nav menu #2513

Merged
merged 3 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/neuron-ui/src/components/WalletWizard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ const Mnemonic = ({ state = initState, rootPath = '/wizard/', dispatch, isSettin
} else {
navigate(-1)
}
navigate(-1)
}, [changeStep, type])

return (
Expand Down
261 changes: 139 additions & 122 deletions packages/neuron-ui/src/containers/Navbar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import React, { useCallback } from 'react'
import React, { useCallback, useState } from 'react'
import { createPortal } from 'react-dom'
import { useNavigate, useLocation } from 'react-router-dom'
import { useTranslation } from 'react-i18next'
import { useState as useGlobalState } from 'states'

import { openExternal, showSettings } from 'services/remote'

import NetworkStatus from 'components/NetworkStatus'
import SyncStatus from 'components/SyncStatus'
import RingProgresBar from 'widgets/RingProgressBar'
import { ReactComponent as ExperimentalIcon } from 'widgets/Icons/Flask.svg'

import Logo from 'widgets/Icons/Logo.png'
import {
RoutePath,
getSyncLeftTime,
localNumberFormatter,
useOnLocaleChange,
SyncStatus as SyncStatusEnum,
ConnectionStatus,
getExplorerUrl,
isMainnet,
} from 'utils'
Overview,
Send,
Receive,
History,
NervosDAO,
Settings,
Experimental,
ArrowOpenRight,
MenuExpand,
} from 'widgets/Icons/icon'
import { showSettings } from 'services/remote'
import { RoutePath, useOnLocaleChange } from 'utils'
import Tooltip from 'widgets/Tooltip'

import styles from './navbar.module.scss'

Expand All @@ -39,141 +36,161 @@ const throttledShowSettings = (() => {
})()

const menuItems = [
{ name: 'navbar.overview', key: RoutePath.Overview.slice(1), url: RoutePath.Overview, experimental: false },
{ name: 'navbar.send', key: RoutePath.Send.slice(1), url: RoutePath.Send, experimental: false },
{ name: 'navbar.receive', key: RoutePath.Receive.slice(1), url: RoutePath.Receive, experimental: false },
{ name: 'navbar.history', key: RoutePath.History.slice(1), url: RoutePath.History, experimental: false },
{ name: 'navbar.nervos-dao', key: RoutePath.NervosDAO.slice(1), url: RoutePath.NervosDAO, experimental: false },
{ name: 'navbar.overview', key: RoutePath.Overview, url: RoutePath.Overview, icon: <Overview /> },
{ name: 'navbar.send', key: RoutePath.Send, url: RoutePath.Send, icon: <Send /> },
{ name: 'navbar.receive', key: RoutePath.Receive, url: RoutePath.Receive, icon: <Receive /> },
{ name: 'navbar.history', key: RoutePath.History, url: RoutePath.History, icon: <History /> },
{ name: 'navbar.nervos-dao', key: RoutePath.NervosDAO, url: RoutePath.NervosDAO, icon: <NervosDAO /> },
{ name: 'navbar.settings', key: RoutePath.Settings, url: RoutePath.Settings, icon: <Settings /> },
{
name: 'navbar.special-assets',
key: RoutePath.SpecialAssets.slice(1),
name: 'navbar.experimental-functions',
key: 'experimental-functions',
icon: <Experimental />,
url: RoutePath.SpecialAssets,
experimental: true,
children: [
{ name: 'navbar.special-assets', key: RoutePath.SpecialAssets, url: RoutePath.SpecialAssets },
{ name: 'navbar.s-udt', key: RoutePath.SUDTAccountList, url: RoutePath.SUDTAccountList },
],
},
{ name: 'navbar.s-udt', key: RoutePath.SUDTAccountList.slice(1), url: RoutePath.SUDTAccountList, experimental: true },
]

const MenuButton = ({
menu,
onClick,
children,
selectedKey,
className,
}: React.PropsWithChildren<{
menu: { key: string; name: string; url: string }
onClick: React.MouseEventHandler<HTMLButtonElement>
selectedKey?: string
className?: string
}>) => {
const [t] = useTranslation()
return (
<button
type="button"
title={t(menu.name)}
name={t(menu.name)}
aria-label={t(menu.name)}
data-link={menu.url}
data-active={menu.key === selectedKey}
onClick={onClick}
className={className}
>
{children}
</button>
)
}

const Navbar = () => {
const navigate = useNavigate()
const { pathname } = useLocation()
const neuronWallet = useGlobalState()
const {
wallet: { name },
chain: {
connectionStatus,
networkID,
syncState: { cacheTipBlockNumber, bestKnownBlockNumber, estimate, status, isLookingValidTarget, validTarget },
},
settings: { wallets = [], networks = [] },
settings: { wallets = [] },
} = neuronWallet
const [t, i18n] = useTranslation()
useOnLocaleChange(i18n)

const network = networks.find(n => n.id === networkID)

const selectedKey = menuItems.find(item => item.key === pathname.substr(1))?.key ?? null

const syncStatus = status

const onOpenValidTarget = useCallback(
(e: React.SyntheticEvent) => {
e.stopPropagation()
const explorerUrl = getExplorerUrl(isMainnet(networks, networkID))
openExternal(`${explorerUrl}/block/${validTarget}`)
const selectedKey = menuItems.find(item => item.key === pathname || item.children?.some(v => v.key === pathname))?.key
const [menuExpanded, setMenuExpanded] = useState(true)
const onClickExpand = useCallback(() => {
setMenuExpanded(v => !v)
}, [setMenuExpanded])
const onClickNavItem = useCallback(
(e: React.SyntheticEvent<HTMLButtonElement>) => {
const {
dataset: { link },
} = e.currentTarget
if (link) {
navigate(link)
}
},
[networks, networkID, validTarget]
[navigate]
)

if (!wallets.length || FULL_SCREENS.find(url => pathname.startsWith(url))) {
return null
}

const normalMenus = menuItems
.filter(item => !item.experimental)
.map(item => (
<button
type="button"
key={item.key}
title={t(item.name)}
name={t(item.name)}
aria-label={t(item.name)}
data-link={item.url}
data-active={item.key === selectedKey}
onClick={() => navigate(item.url)}
>
{t(item.name)}
</button>
))

const experimentalMenus = menuItems
.filter(item => item.experimental)
.map(item => (
<button
type="button"
key={item.key}
title={t(item.name)}
name={t(item.name)}
aria-label={t(item.name)}
data-link={item.url}
data-active={item.key === selectedKey}
onClick={() => navigate(item.url)}
>
{t(item.name)}
</button>
))

const bestBlockNumber = Math.max(cacheTipBlockNumber, bestKnownBlockNumber)
const syncPercents =
bestBlockNumber > 0 && cacheTipBlockNumber > 0 ? +((cacheTipBlockNumber * 100) / bestBlockNumber).toFixed(2) : 0
let ringBarColor = '#3cc68a'
if (ConnectionStatus.Offline === connectionStatus || SyncStatusEnum.SyncNotStart === syncStatus) {
ringBarColor = '#ff0000'
} else if (SyncStatusEnum.SyncPending === syncStatus) {
ringBarColor = '#f7ae4d'
}

const syncBlockNumbers = `${cacheTipBlockNumber >= 0 ? localNumberFormatter(cacheTipBlockNumber) : '-'}/${
bestBlockNumber >= 0 ? localNumberFormatter(bestBlockNumber) : '-'
}`

return (
<aside className={styles.sidebar}>
<aside className={styles.sidebar} data-expanded={menuExpanded}>
<button
type="button"
className={styles.name}
title={name}
aria-label={name}
onClick={() => throttledShowSettings({ tab: 'wallets' })}
>
{name}
{menuExpanded ? (
<img src={Logo} alt="logo" />
) : (
<Tooltip tip={name} placement="right">
<img src={Logo} alt="logo" />
</Tooltip>
)}
{menuExpanded && (
<Tooltip tip={name} className={styles.nameText} placement="right">
<span>{name}</span>
</Tooltip>
)}
</button>
<nav role="navigation" className={styles.navs}>
{normalMenus}
<div className={styles.experimentalDivider}>
<ExperimentalIcon />
{t('navbar.experimental-functions')}
</div>
{experimentalMenus}
{menuExpanded
? menuItems.map(item => (
<React.Fragment key={item.key}>
<MenuButton menu={item} selectedKey={selectedKey} onClick={onClickNavItem}>
{item.icon}
<span>{t(item.name)}</span>
{item.children?.length && <ArrowOpenRight className={styles.arrow} />}
</MenuButton>
{item.children?.length && item.key === selectedKey && (
<div className={styles.child}>
<div className={styles.leftLine} />
<div>
{item.children.map(child => (
<MenuButton key={child.key} menu={child} selectedKey={pathname} onClick={onClickNavItem}>
{t(child.name)}
</MenuButton>
))}
</div>
</div>
)}
</React.Fragment>
))
: menuItems.map(item => (
<React.Fragment key={item.key}>
<Tooltip
tip={
item.children?.length ? (
<>
{item.children.map(child => (
<MenuButton
key={child.key}
menu={child}
selectedKey={pathname}
onClick={onClickNavItem}
className={styles.buttonInTip}
>
{t(child.name)}
</MenuButton>
))}
</>
) : (
t(item.name)
)
}
placement={item.children?.length ? 'right-bottom' : 'right'}
>
<MenuButton menu={item} selectedKey={selectedKey} onClick={onClickNavItem}>
{item.icon}
</MenuButton>
</Tooltip>
</React.Fragment>
))}
</nav>
<div className={styles.network}>
<NetworkStatus
isLookingValidTarget={isLookingValidTarget}
onOpenValidTarget={onOpenValidTarget}
syncPercents={syncPercents}
syncBlockNumbers={syncBlockNumbers}
network={network}
onAction={() => throttledShowSettings({ tab: 'networks' })}
/>
</div>
<div className={styles.sync}>
<RingProgresBar
percents={syncPercents}
color={ringBarColor}
backgroundColor="#666"
size="16px"
strokeWidth="4px"
/>
<SyncStatus syncStatus={syncStatus} connectionStatus={connectionStatus} leftTime={getSyncLeftTime(estimate)} />
<div className={styles.showExpand}>
<MenuExpand className={styles.expand} onClick={onClickExpand} data-expanded={menuExpanded} />
</div>
</aside>
)
Expand Down
Loading