2022-03-14 13:18:11 +00:00
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import { Vega } from '../icons/vega';
|
|
|
|
import Link from 'next/link';
|
|
|
|
import { AnchorButton } from '@vegaprotocol/ui-toolkit';
|
2022-06-06 16:19:56 +00:00
|
|
|
import { LocalStorage, t } from '@vegaprotocol/react-helpers';
|
|
|
|
import { useEffect, useState } from 'react';
|
2022-03-14 13:18:11 +00:00
|
|
|
|
|
|
|
export const Navbar = () => {
|
2022-06-06 16:19:56 +00:00
|
|
|
const initNavItemsState = [
|
|
|
|
{
|
|
|
|
name: t('Portfolio'),
|
|
|
|
path: '/portfolio',
|
|
|
|
testId: 'portfolio-link',
|
|
|
|
slug: '',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const [navItems, setNavItems] = useState(initNavItemsState);
|
|
|
|
const marketId = LocalStorage.getItem('marketId') ?? '';
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setNavItems([
|
|
|
|
{
|
|
|
|
name: t('Trading'),
|
|
|
|
path: '/markets',
|
|
|
|
testId: 'markets-link',
|
|
|
|
slug: marketId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: t('Portfolio'),
|
|
|
|
path: '/portfolio',
|
|
|
|
testId: 'portfolio-link',
|
|
|
|
slug: '',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
}, [marketId]);
|
|
|
|
|
2022-03-14 13:18:11 +00:00
|
|
|
return (
|
|
|
|
<nav className="flex items-center">
|
|
|
|
<Link href="/" passHref={true}>
|
|
|
|
<a className="px-[26px]">
|
|
|
|
<Vega className="fill-black dark:fill-white" />
|
|
|
|
</a>
|
|
|
|
</Link>
|
2022-06-06 16:19:56 +00:00
|
|
|
{navItems.map((route) => (
|
2022-03-14 13:18:11 +00:00
|
|
|
<NavLink key={route.path} {...route} />
|
|
|
|
))}
|
|
|
|
</nav>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
interface NavLinkProps {
|
|
|
|
name: string;
|
|
|
|
path: string;
|
|
|
|
exact?: boolean;
|
2022-06-06 16:19:56 +00:00
|
|
|
testId?: string;
|
|
|
|
slug?: string;
|
2022-03-14 13:18:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 16:19:56 +00:00
|
|
|
const NavLink = ({
|
|
|
|
name,
|
|
|
|
path,
|
|
|
|
exact,
|
|
|
|
testId = name,
|
|
|
|
slug = '',
|
|
|
|
}: NavLinkProps) => {
|
2022-03-14 13:18:11 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const isActive =
|
|
|
|
router.asPath === path || (!exact && router.asPath.startsWith(path));
|
2022-06-06 16:19:56 +00:00
|
|
|
const href = slug !== '' ? `${path}/${slug}` : path;
|
2022-03-14 13:18:11 +00:00
|
|
|
return (
|
|
|
|
<AnchorButton
|
|
|
|
variant={isActive ? 'accent' : 'inline'}
|
2022-03-24 18:08:57 +00:00
|
|
|
className="px-16 py-6 h-[38px] uppercase border-0 self-end xs:text-ui sm:text-body-large md:text-h5 lg:text-h4"
|
2022-06-06 16:19:56 +00:00
|
|
|
data-testid={testId}
|
|
|
|
href={href}
|
2022-03-14 13:18:11 +00:00
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
2022-06-06 16:19:56 +00:00
|
|
|
router.push(href);
|
2022-03-14 13:18:11 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{name}
|
|
|
|
</AnchorButton>
|
|
|
|
);
|
|
|
|
};
|