fix: 1569 navigation link active on market selected refresh (#1580)

* chore: ignore apollo errors - to be reverted after API will be fixed

* fix: navigtaion link market - no global local storage init

* fix: update active link check

* fix: fix any hydration errors and default fallback to markets

Co-authored-by: Bartłomiej Głownia <bglownia@gmail.com>
This commit is contained in:
m.ray 2022-10-03 09:29:07 +01:00 committed by GitHub
parent 1d87eab47c
commit 361f8fa870
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 17 deletions

View File

@ -7,6 +7,7 @@ import { useGlobalStore } from '../../stores/global';
import { VegaWalletConnectButton } from '../vega-wallet-connect-button'; import { VegaWalletConnectButton } from '../vega-wallet-connect-button';
import { ThemeSwitcher } from '@vegaprotocol/ui-toolkit'; import { ThemeSwitcher } from '@vegaprotocol/ui-toolkit';
import { Vega } from '../icons/vega'; import { Vega } from '../icons/vega';
import { useEffect, useState } from 'react';
interface NavbarProps { interface NavbarProps {
theme: 'light' | 'dark'; theme: 'light' | 'dark';
@ -18,7 +19,12 @@ export const Navbar = ({ theme, toggleTheme }: NavbarProps) => {
marketId: store.marketId, marketId: store.marketId,
update: store.update, update: store.update,
})); }));
const tradingPath = marketId ? `/markets/${marketId}` : '/'; const [tradingPath, setTradingPath] = useState('/markets');
useEffect(() => {
if (marketId) {
setTradingPath(`/markets/${marketId}`);
}
}, [marketId]);
return ( return (
<div className="dark px-4 flex items-stretch border-b border-default bg-black text-white"> <div className="dark px-4 flex items-stretch border-b border-default bg-black text-white">
<div className="flex gap-4 mr-4 items-center h-full"> <div className="flex gap-4 mr-4 items-center h-full">
@ -31,15 +37,8 @@ export const Navbar = ({ theme, toggleTheme }: NavbarProps) => {
<NetworkSwitcher /> <NetworkSwitcher />
</div> </div>
<nav className="flex items-center"> <nav className="flex items-center">
{[ <NavLink key={'trading'} name={t('Trading')} path={tradingPath} />
{ <NavLink key={'portfolio'} name={t('Portfolio')} path={'/portfolio'} />
name: t('Trading'),
path: tradingPath,
},
{ name: t('Portfolio'), path: '/portfolio' },
].map((route) => (
<NavLink key={route.path} {...route} />
))}
</nav> </nav>
<div className="flex items-center gap-2 ml-auto"> <div className="flex items-center gap-2 ml-auto">
<ThemeSwitcher theme={theme} onToggle={toggleTheme} /> <ThemeSwitcher theme={theme} onToggle={toggleTheme} />
@ -59,7 +58,11 @@ interface NavLinkProps {
const NavLink = ({ name, path, testId = name }: NavLinkProps) => { const NavLink = ({ name, path, testId = name }: NavLinkProps) => {
const router = useRouter(); const router = useRouter();
const isActive = router.asPath === path; const [isActive, setIsActive] = useState(false);
useEffect(
() => setIsActive(router.asPath.includes(path)),
[path, router.asPath]
);
const linkClasses = classNames('mx-2 py-2 self-end border-b-4', { const linkClasses = classNames('mx-2 py-2 self-end border-b-4', {
'border-vega-yellow text-white cursor-default': isActive, 'border-vega-yellow text-white cursor-default': isActive,
'border-transparent text-neutral-400 hover:text-neutral-300': !isActive, 'border-transparent text-neutral-400 hover:text-neutral-300': !isActive,

View File

@ -1,9 +1,5 @@
import { useMarketList } from '@vegaprotocol/market-list'; import { useMarketList } from '@vegaprotocol/market-list';
import { import { addDecimalsFormatNumber, titlefy } from '@vegaprotocol/react-helpers';
addDecimalsFormatNumber,
LocalStorage,
titlefy,
} from '@vegaprotocol/react-helpers';
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit'; import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { useEffect } from 'react'; import { useEffect } from 'react';
@ -23,7 +19,7 @@ export function Index() {
update({ landingDialog: true }); update({ landingDialog: true });
if (data) { if (data) {
const marketId = LocalStorage.getItem('marketId') || data.markets[0]?.id; const marketId = data.markets[0]?.id;
const marketName = data.markets[0]?.tradableInstrument.instrument.name; const marketName = data.markets[0]?.tradableInstrument.instrument.name;
const marketPrice = data.marketsData[0]?.markPrice const marketPrice = data.marketsData[0]?.markPrice
? addDecimalsFormatNumber( ? addDecimalsFormatNumber(