2021-04-14 05:01:58 -07:00
|
|
|
import { useEffect, useState } from 'react'
|
2021-04-14 18:54:23 -07:00
|
|
|
import { Menu } from '@headlessui/react'
|
2021-04-05 07:32:11 -07:00
|
|
|
import styled from '@emotion/styled'
|
2021-04-14 05:01:58 -07:00
|
|
|
import {
|
2021-04-14 18:54:23 -07:00
|
|
|
ChevronUpIcon,
|
|
|
|
ChevronDownIcon,
|
2021-04-14 05:01:58 -07:00
|
|
|
DuplicateIcon,
|
|
|
|
LogoutIcon,
|
|
|
|
MenuIcon,
|
|
|
|
XIcon,
|
|
|
|
} from '@heroicons/react/outline'
|
2021-04-19 09:15:49 -07:00
|
|
|
import UiLock from './UiLock'
|
|
|
|
import { useRouter } from 'next/router'
|
2021-03-30 15:47:08 -07:00
|
|
|
import MenuItem from './MenuItem'
|
2021-04-12 06:32:01 -07:00
|
|
|
import ThemeSwitch from './ThemeSwitch'
|
2021-04-15 06:22:39 -07:00
|
|
|
import { WalletIcon } from './icons'
|
2021-04-14 15:46:36 -07:00
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
2021-04-18 18:58:16 -07:00
|
|
|
import ConnectWalletButton from './ConnectWalletButton'
|
2021-05-01 07:02:21 -07:00
|
|
|
import { copyToClipboard } from '../utils'
|
2021-05-07 12:41:26 -07:00
|
|
|
import AlertsList from './AlertsList'
|
2021-03-30 15:47:08 -07:00
|
|
|
|
2021-04-05 07:32:11 -07:00
|
|
|
const Code = styled.code`
|
|
|
|
border: 1px solid hsla(0, 0%, 39.2%, 0.2);
|
|
|
|
border-radius: 3px;
|
|
|
|
background: hsla(0, 0%, 58.8%, 0.1);
|
2021-04-14 18:54:23 -07:00
|
|
|
font-size: 13px;
|
2021-04-05 07:32:11 -07:00
|
|
|
`
|
2021-04-02 11:26:21 -07:00
|
|
|
|
2021-04-22 08:00:13 -07:00
|
|
|
const WALLET_OPTIONS = [
|
|
|
|
{ name: 'Copy address', icon: <DuplicateIcon /> },
|
|
|
|
{ name: 'Disconnect', icon: <LogoutIcon /> },
|
|
|
|
]
|
|
|
|
|
2021-04-05 07:32:11 -07:00
|
|
|
const TopBar = () => {
|
2021-04-19 09:15:49 -07:00
|
|
|
const { asPath } = useRouter()
|
2021-04-14 15:46:36 -07:00
|
|
|
const connected = useMangoStore((s) => s.wallet.connected)
|
|
|
|
const wallet = useMangoStore((s) => s.wallet.current)
|
2021-03-30 15:47:08 -07:00
|
|
|
const [showMenu, setShowMenu] = useState(false)
|
2021-04-14 05:01:58 -07:00
|
|
|
const [isCopied, setIsCopied] = useState(false)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isCopied) {
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
setIsCopied(false)
|
|
|
|
}, 2000)
|
|
|
|
return () => clearTimeout(timer)
|
|
|
|
}
|
|
|
|
}, [isCopied])
|
|
|
|
|
|
|
|
const handleWalletMenu = (option) => {
|
|
|
|
if (option === 'Copy address') {
|
2021-05-01 07:02:21 -07:00
|
|
|
copyToClipboard(wallet.publicKey)
|
2021-04-14 05:01:58 -07:00
|
|
|
setIsCopied(true)
|
|
|
|
} else {
|
|
|
|
wallet.disconnect()
|
|
|
|
}
|
|
|
|
}
|
2021-03-30 15:47:08 -07:00
|
|
|
|
|
|
|
return (
|
2021-04-14 22:18:59 -07:00
|
|
|
<nav className={`bg-th-bkg-2`}>
|
2021-05-07 12:41:26 -07:00
|
|
|
<div className={`px-4 md:px-6 lg:px-8`}>
|
2021-04-12 09:49:02 -07:00
|
|
|
<div className={`flex justify-between h-16`}>
|
|
|
|
<div className={`flex`}>
|
2021-04-19 06:45:59 -07:00
|
|
|
<div className={`flex-shrink-0 flex items-center`}>
|
2021-03-30 15:47:08 -07:00
|
|
|
<img
|
2021-04-12 09:49:02 -07:00
|
|
|
className={`h-8 w-auto`}
|
2021-03-30 15:47:08 -07:00
|
|
|
src="/assets/icons/logo.svg"
|
|
|
|
alt="next"
|
|
|
|
/>
|
|
|
|
</div>
|
2021-05-07 12:41:26 -07:00
|
|
|
<div className={`hidden md:flex md:space-x-6 md:ml-4 py-2`}>
|
2021-03-30 15:47:08 -07:00
|
|
|
<MenuItem href="/">Trade</MenuItem>
|
|
|
|
<MenuItem href="/stats">Stats</MenuItem>
|
2021-04-24 19:10:28 -07:00
|
|
|
<MenuItem href="/alerts">Alerts</MenuItem>
|
2021-03-30 15:47:08 -07:00
|
|
|
<MenuItem href="https://docs.mango.markets/">Learn</MenuItem>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-04-13 16:41:04 -07:00
|
|
|
<div className="flex">
|
2021-04-19 06:45:59 -07:00
|
|
|
<div className="flex items-center">
|
2021-05-07 12:41:26 -07:00
|
|
|
<div className="pl-3">{asPath === '/' ? <UiLock /> : null}</div>
|
|
|
|
<div className="pl-3">
|
2021-04-19 06:45:59 -07:00
|
|
|
<ThemeSwitch />
|
|
|
|
</div>
|
2021-05-07 12:41:26 -07:00
|
|
|
{connected ? (
|
|
|
|
<div className="pl-3">
|
|
|
|
<AlertsList />
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
<div className="hidden md:ml-4 md:block">
|
2021-04-14 15:46:36 -07:00
|
|
|
{connected && wallet?.publicKey ? (
|
2021-04-14 18:54:23 -07:00
|
|
|
<Menu>
|
|
|
|
{({ open }) => (
|
|
|
|
<div className="relative">
|
2021-04-14 19:18:30 -07:00
|
|
|
<Menu.Button className="w-48 h-11 pl-2 pr-2.5 border border-th-green hover:border-th-fgd-1 focus:outline-none rounded-md text-th-fgd-4 hover:text-th-fgd-1">
|
2021-04-14 18:54:23 -07:00
|
|
|
<div className="flex flex-row items-center justify-between">
|
|
|
|
<div className="flex items-center">
|
2021-04-14 19:18:30 -07:00
|
|
|
<WalletIcon className="w-5 h-5 mr-2 fill-current text-th-green" />
|
2021-04-14 18:54:23 -07:00
|
|
|
<Code className="p-1 text-th-fgd-3 font-light">
|
|
|
|
{isCopied
|
|
|
|
? 'Copied!'
|
|
|
|
: wallet.publicKey.toString().substr(0, 5) +
|
|
|
|
'...' +
|
|
|
|
wallet.publicKey.toString().substr(-5)}
|
|
|
|
</Code>
|
|
|
|
</div>
|
|
|
|
{open ? (
|
|
|
|
<ChevronUpIcon className="h-5 w-5" />
|
|
|
|
) : (
|
|
|
|
<ChevronDownIcon className="h-5 w-5" />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Menu.Button>
|
|
|
|
<Menu.Items className="z-20 p-1 absolute right-0 top-11 bg-th-bkg-1 divide-y divide-th-bkg-3 shadow-lg outline-none rounded-md w-48">
|
|
|
|
{WALLET_OPTIONS.map(({ name, icon }) => (
|
|
|
|
<Menu.Item key={name}>
|
|
|
|
<button
|
2021-04-16 04:50:56 -07:00
|
|
|
className="flex flex-row items-center w-full p-2 hover:bg-th-bkg-2 hover:cursor-pointer font-normal"
|
2021-04-14 18:54:23 -07:00
|
|
|
onClick={() => handleWalletMenu(name)}
|
|
|
|
>
|
2021-04-16 04:50:56 -07:00
|
|
|
<div className="w-5 h-5 mr-2">{icon}</div>
|
|
|
|
{name}
|
2021-04-14 18:54:23 -07:00
|
|
|
</button>
|
|
|
|
</Menu.Item>
|
|
|
|
))}
|
|
|
|
</Menu.Items>
|
2021-04-11 14:25:01 -07:00
|
|
|
</div>
|
2021-04-14 18:54:23 -07:00
|
|
|
)}
|
|
|
|
</Menu>
|
2021-04-14 05:01:58 -07:00
|
|
|
) : (
|
2021-04-18 18:58:16 -07:00
|
|
|
<ConnectWalletButton />
|
2021-04-14 05:01:58 -07:00
|
|
|
)}
|
2021-04-11 14:25:01 -07:00
|
|
|
</div>
|
2021-03-30 15:47:08 -07:00
|
|
|
</div>
|
2021-05-07 12:41:26 -07:00
|
|
|
<div className={`-mr-2 ml-2 flex items-center md:hidden`}>
|
2021-04-11 14:25:01 -07:00
|
|
|
<button
|
|
|
|
type="button"
|
2021-04-12 09:49:02 -07:00
|
|
|
className={`inline-flex items-center justify-center p-2 rounded-md text-black dark:text-white hover:text-gray-400 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-mango-orange`}
|
2021-04-11 14:25:01 -07:00
|
|
|
aria-controls="mobile-menu"
|
|
|
|
aria-expanded="false"
|
|
|
|
onClick={() => setShowMenu((showMenu) => !showMenu)}
|
2021-03-30 15:47:08 -07:00
|
|
|
>
|
2021-04-13 22:23:50 -07:00
|
|
|
<span className="sr-only">Open main menu</span>
|
2021-04-11 14:25:01 -07:00
|
|
|
{showMenu ? (
|
2021-04-13 22:23:50 -07:00
|
|
|
<XIcon className="h-5 w-5 text-th-primary" />
|
2021-04-11 14:25:01 -07:00
|
|
|
) : (
|
2021-04-13 22:23:50 -07:00
|
|
|
<MenuIcon className="h-5 w-5 text-th-primary" />
|
2021-04-11 14:25:01 -07:00
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
</div>
|
2021-03-30 15:47:08 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div
|
2021-05-07 12:41:26 -07:00
|
|
|
className={`${showMenu ? `visible` : `hidden`} md:hidden`}
|
2021-03-30 15:47:08 -07:00
|
|
|
id="mobile-menu"
|
|
|
|
>
|
2021-04-12 09:49:02 -07:00
|
|
|
<div
|
|
|
|
className={`bg-th-bkg-3 pt-2 pb-3 space-y-1 border-b border-th-fgd-4`}
|
|
|
|
>
|
2021-03-30 15:47:08 -07:00
|
|
|
<MenuItem href="/">Trade</MenuItem>
|
|
|
|
<MenuItem href="/stats">Stats</MenuItem>
|
2021-04-24 19:10:28 -07:00
|
|
|
<MenuItem href="/alerts">Alerts</MenuItem>
|
2021-03-30 15:47:08 -07:00
|
|
|
<MenuItem href="https://docs.mango.markets/">Learn</MenuItem>
|
2021-04-22 08:00:13 -07:00
|
|
|
|
|
|
|
{connected && wallet?.publicKey ? (
|
|
|
|
<button
|
2021-04-22 08:21:16 -07:00
|
|
|
className="block text-th-fgd-1 text-base items-center pl-3 pr-4 py-2 font-normal
|
2021-05-07 12:41:26 -07:00
|
|
|
md:inline-flex md:ml-4 md:px-1 md:py-0 border-l-4 md:border-l-0 md:border-b-2 hover:text-th-primary
|
2021-04-22 08:00:13 -07:00
|
|
|
border-transparent hover:border-th-primary rounded-none outline-none focus:outline-none"
|
|
|
|
onClick={() => wallet.disconnect()}
|
|
|
|
>
|
|
|
|
Disconnect
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button
|
2021-04-22 08:21:16 -07:00
|
|
|
className="block text-th-fgd-1 text-base items-center pl-3 pr-4 py-2 font-normal
|
2021-05-07 12:41:26 -07:00
|
|
|
md:inline-flex md:ml-4 md:px-1 md:py-0 border-l-4 md:border-l-0 md:border-b-2 hover:text-th-primary
|
2021-04-22 08:00:13 -07:00
|
|
|
border-transparent hover:border-th-primary rounded-none outline-none focus:outline-none"
|
|
|
|
onClick={() => wallet.connect()}
|
|
|
|
>
|
|
|
|
Connect
|
|
|
|
</button>
|
|
|
|
)}
|
2021-03-30 15:47:08 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</nav>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TopBar
|