Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 4a6a169

Browse files
committed
React-router and Staking page with tabbed navigation
1 parent 6d37a92 commit 4a6a169

File tree

13 files changed

+144
-62
lines changed

13 files changed

+144
-62
lines changed

content/LoansPage.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { useEffect, FC } from 'react';
1+
import { FC } from 'react';
22
import Head from 'next/head';
33
import styled from 'styled-components';
44
import { useTranslation } from 'react-i18next';
5-
import { useRouter } from 'next/router';
65

76
import StatBox from 'components/StatBox';
87
import { LineSpacer } from 'styles/common';
@@ -23,20 +22,13 @@ type LoansPageProps = {};
2322

2423
const LoansPage: FC<LoansPageProps> = () => {
2524
const { t } = useTranslation();
26-
const router = useRouter();
2725

2826
const walletAddress = useRecoilValue(walletAddressState);
2927

3028
const { stakedCollateralValue, debtBalance } = useStakingCalculations();
3129
const { selectedPriceCurrency, getPriceAtCurrentRate } = useSelectedPriceCurrency();
3230
const { stakingAPR } = useUserStakingData(walletAddress);
3331

34-
useEffect(() => {
35-
if (router.asPath === '/loans') {
36-
router.push('/loans/new');
37-
}
38-
}, [router, router.asPath, router.push]);
39-
4032
return (
4133
<>
4234
<Head>

no-next/App.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { ThemeProvider } from 'styled-components';
88
import WithAppContainers from 'containers';
99
import theme from 'styles/theme';
1010
import Layout from 'sections/shared/Layout';
11-
import AppLayout from 'sections/shared/Layout/AppLayout';
1211
import { MediaContextProvider } from 'styles/media';
1312
import { QueryClient, QueryClientProvider } from 'react-query';
1413
import { ReactQueryDevtools } from 'react-query/devtools';
@@ -21,7 +20,7 @@ import SystemStatus from 'sections/shared/SystemStatus';
2120
import '../i18n';
2221
import Connector from 'containers/Connector';
2322

24-
import DashboardPage from 'content/DashboardPage';
23+
import Routes from './Routes';
2524

2625
const queryClient = new QueryClient({
2726
defaultOptions: {
@@ -59,9 +58,7 @@ function InnerApp() {
5958
>
6059
<Layout>
6160
<SystemStatus>
62-
<AppLayout>
63-
<DashboardPage />
64-
</AppLayout>
61+
<Routes />
6562
</SystemStatus>
6663
</Layout>
6764
<ReactQueryDevtools />

no-next/Routes.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom';
2+
3+
import { safeLazy } from '@synthetixio/safe-import';
4+
import AppLayout from '../sections/shared/Layout/AppLayout';
5+
6+
const DashboardPage = safeLazy(
7+
() => import(/* webpackChunkName: "dashboard" */ 'content/DashboardPage')
8+
);
9+
const SynthsPage = safeLazy(() => import(/* webpackChunkName: "synths" */ 'content/SynthsPage'));
10+
11+
const StakingPage = safeLazy(() => import(/* webpackChunkName: "staking" */ 'content/StakingPage'));
12+
13+
const LoansPage = safeLazy(() => import(/* webpackChunkName: "loans" */ 'content/LoansPage'));
14+
const NotFound = safeLazy(() => import(/* webpackChunkName: "404" */ '../pages/404'));
15+
16+
function Page(props) {
17+
return <pre style={{ padding: '50px' }}>{JSON.stringify(props, null, 2)}</pre>;
18+
}
19+
20+
export default function AppRoutes() {
21+
return (
22+
<BrowserRouter>
23+
<AppLayout>
24+
<Routes>
25+
<Route path="/loans" element={<Navigate to="/loans/new" replace={true} />} />
26+
<Route path="/escrow" element={<Navigate to="/loans/rewards" replace={true} />} />
27+
28+
<Route path="/" element={<DashboardPage />} />
29+
<Route path="/staking" element={<StakingPage />}>
30+
<Route path="/staking/:action" element={<StakingPage />} />
31+
</Route>
32+
<Route path="/loans" element={<LoansPage />} />
33+
<Route path="/synths" element={<SynthsPage />} />
34+
35+
<Route path="/gov" element={<Page />} />
36+
<Route path="/earn" element={<Page />} />
37+
<Route path="/debt" element={<Page />} />
38+
<Route path="/l2" element={<Page />} />
39+
<Route path="/escrow" element={<Page />} />
40+
<Route path="/escrow/rewards" element={<Page />} />
41+
<Route path="/escrow/ico" element={<Page />} />
42+
<Route path="/history" element={<Page />} />
43+
<Route path="/delegate" element={<Page />} />
44+
<Route path="/merge-accounts" element={<Page />} />
45+
46+
<Route path="*" element={<NotFound />} />
47+
</Routes>
48+
</AppLayout>
49+
</BrowserRouter>
50+
);
51+
}

no-next/next-link.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export default function Link({ ...props }) {
2-
// eslint-disable-next-line jsx-a11y/anchor-has-content
3-
return <a {...props} />;
1+
import { Link } from 'react-router-dom';
2+
3+
export default function NoNextLink({ href, ...props }) {
4+
return <Link to={href} {...props} />;
45
}

no-next/next-router.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
const router = {
2-
push: function push() {},
3-
asPath: '',
4-
};
1+
import { useNavigate, useLocation } from 'react-router-dom';
52

63
export function useRouter() {
7-
return router;
8-
}
4+
const navigate = useNavigate();
5+
const location = useLocation();
96

10-
export default router;
7+
return {
8+
query: {},
9+
push: navigate,
10+
pathname: '',
11+
asPath: location.pathname,
12+
};
13+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"react-outside-click-handler": "^1.3.0",
6666
"react-query": "3.16.0",
6767
"react-responsive": "^8.2.0",
68+
"react-router-dom": "^6.3.0",
6869
"react-select": "^4.3.1",
6970
"react-slick": "^0.27.13",
7071
"react-table": "^7.7.0",

sections/layer2/migrate/MigrateTab/TabContent.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FC } from 'react';
22
import styled from 'styled-components';
33
import { useTranslation } from 'react-i18next';
4-
import router from 'next/router';
4+
import { useRouter } from 'next/router';
55

66
import { formatCurrency } from 'utils/formatters/number';
77
import { CryptoCurrency } from 'constants/currency';
@@ -59,6 +59,8 @@ const TabContent: FC<TabContentProps> = ({
5959
const { t } = useTranslation();
6060
const vestingCurrencyKey = CryptoCurrency['SNX'];
6161

62+
const router = useRouter();
63+
6264
const renderButton = () => {
6365
if (isVestNeeded) {
6466
return (

sections/loans/components/ActionBox/ActionBox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const ActionBox: React.FC<ActionBoxProps> = () => {
5656
<StructuredTab
5757
boxPadding={20}
5858
tabData={tabData}
59-
setActiveTab={(key: string) => router.push(`/loans/${key}`)}
59+
setActiveTab={(key) => router.push(`/loans/${key}`)}
6060
activeTab={currentTab}
6161
/>
6262
);

sections/shared/Layout/AppLayout.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FC, ReactNode, useEffect } from 'react';
2-
import router from 'next/router';
2+
// import { useRouter } from 'next/router';
33
import { useRecoilValue } from 'recoil';
44
import styled from 'styled-components';
55

@@ -18,6 +18,7 @@ type AppLayoutProps = {
1818
};
1919

2020
const AppLayout: FC<AppLayoutProps> = ({ children }) => {
21+
// const router = useRouter();
2122
const isL2 = useRecoilValue(isL2State);
2223
const isMainnet = useRecoilValue(isMainnetState);
2324

@@ -26,11 +27,11 @@ const AppLayout: FC<AppLayoutProps> = ({ children }) => {
2627
const depositsInactive = !(useIsBridgeActiveQuery().data ?? true); // Deposits are active by default to prevent redirects when status unknown
2728
const delegateWallet = useRecoilValue(delegateWalletState);
2829

29-
useEffect(() => {
30-
if (delegateWallet && router.pathname !== ROUTES.Home) {
31-
router.push(ROUTES.Home);
32-
}
33-
}, [isL2, isMainnet, depositsInactive, delegateWallet]);
30+
// useEffect(() => {
31+
// if (delegateWallet && router.pathname !== ROUTES.Home) {
32+
// router.push(ROUTES.Home);
33+
// }
34+
// }, [isL2, isMainnet, depositsInactive, delegateWallet, router]);
3435

3536
return (
3637
<>

sections/shared/Layout/SideNav/Desktop/DesktopMenu.tsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FC, useState, MouseEvent } from 'react';
22
import styled, { css } from 'styled-components';
3+
import { Link } from 'react-router-dom';
34
import { useRouter } from 'next/router';
45
import { useTranslation } from 'react-i18next';
56
import { useRecoilValue } from 'recoil';
@@ -36,10 +37,11 @@ const DesktopMenu: FC = () => {
3637
if (!subMenu) return;
3738
setOpenMenu(link);
3839
};
40+
3941
return (
4042
<div key={link}>
4143
<MenuLinkItem
42-
onClick={() => router.push(link)}
44+
to={link}
4345
onMouseEnter={() => onMouseEnter(link)}
4446
data-testid={`sidenav-${link}`}
4547
isActive={
@@ -55,21 +57,16 @@ const DesktopMenu: FC = () => {
5557
</MenuLinkItem>
5658
{subMenu && (
5759
<DesktopSubMenu i={i} isActive={openMenu === link} key={link}>
58-
{subMenu.map(({ i18nLabel, subLink }, j) => {
59-
const onClick = () => {
60-
router.push(subLink);
61-
};
62-
return (
63-
<SubMenuLinkItem
64-
key={`subMenuLinkItem-${j}`}
65-
isActive={router.asPath === subLink}
66-
data-testid={`sidenav-submenu-${subLink}`}
67-
onClick={onClick}
68-
>
69-
{t(i18nLabel)}
70-
</SubMenuLinkItem>
71-
);
72-
})}
60+
{subMenu.map(({ i18nLabel, subLink }) => (
61+
<SubMenuLinkItem
62+
to={subLink}
63+
key={subLink}
64+
isActive={router.asPath === subLink}
65+
data-testid={`sidenav-submenu-${subLink}`}
66+
>
67+
{t(i18nLabel)}
68+
</SubMenuLinkItem>
69+
))}
7370
</DesktopSubMenu>
7471
)}
7572
</div>
@@ -88,7 +85,7 @@ const MenuLinks = styled.div`
8885
position: relative;
8986
`;
9087

91-
export const MenuLinkItem = styled.div<{ isActive?: boolean; isL2Switcher?: boolean }>`
88+
export const MenuLinkItem = styled(Link)<{ isCurrent?: boolean; isL2Switcher?: boolean }>`
9289
line-height: 40px;
9390
padding-bottom: 10px;
9491
position: relative;

sections/shared/Layout/SideNav/Desktop/DesktopSubMenu.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createPortal } from 'react-dom';
55
import styled, { css } from 'styled-components';
66
import { FlexDivColCentered } from 'styles/common';
77
import media from 'styles/media';
8+
import { Link } from 'react-router-dom';
89

910
type SubMenuProps = {
1011
children: ReactNode;
@@ -28,9 +29,8 @@ const DesktopSubMenu: FC<SubMenuProps> = ({ children, i, isActive }) => {
2829
export const SubContainer = styled(FlexDivColCentered)<{ i: number; isActive: boolean }>`
2930
padding-top: ${({ i }) => 111 + i * 50}px; // (Logo + container) + index offset * link height;
3031
background: ${(props) => props.theme.colors.darkGradient1};
31-
top: 0px;
32-
bottom: 0px;
33-
transform: translateX(0px);
32+
top: 0;
33+
bottom: 0;
3434
border-right: 1px solid ${(props) => props.theme.colors.grayBlue};
3535
height: 100%;
3636
width: 128px;
@@ -47,8 +47,8 @@ export const SubContainer = styled(FlexDivColCentered)<{ i: number; isActive: bo
4747
`}
4848
`;
4949

50-
export const SubMenuLinkItem = styled.div<{ isActive: boolean }>`
51-
zindex: ${zIndex.BASE};
50+
export const SubMenuLinkItem = styled(Link)<{ isCurrent: boolean }>`
51+
z-index: ${zIndex.BASE};
5252
padding-left: 26px;
5353
line-height: 40px;
5454
padding-bottom: 10px;
@@ -84,11 +84,10 @@ export const SubMenuLinkItem = styled.div<{ isActive: boolean }>`
8484
`;
8585

8686
const Inner = styled.div`
87-
position: relative;
8887
height: 100%;
8988
width: 100%;
9089
position: fixed;
91-
zindex: -100;
90+
z-index: -100;
9291
`;
9392

9493
export default DesktopSubMenu;

sections/staking/index.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FC } from 'react';
22
import styled from 'styled-components';
3-
import { useRouter } from 'next/router';
3+
import { useParams } from 'react-router-dom';
44

55
import { StakingPanelType } from 'store/staking';
66
import { FlexDivCol } from 'styles/common';
@@ -10,16 +10,20 @@ import InfoBox from './components/InfoBox';
1010
import ActionBox from './components/ActionBox';
1111

1212
const Index: FC = () => {
13-
const router = useRouter();
14-
const defaultTab = (router.query.action && router.query.action[0]) || StakingPanelType.MINT;
13+
const params = useParams();
14+
const action = params.action || '';
15+
// @ts-ignore
16+
const currentTab = Object.values(StakingPanelType).includes(action)
17+
? action
18+
: StakingPanelType.MINT;
1519

1620
return (
1721
<Container>
1822
<Col>
19-
<ActionBox currentTab={defaultTab} />
23+
<ActionBox currentTab={currentTab} />
2024
</Col>
2125
<Col>
22-
<InfoBox currentTab={defaultTab} />
26+
<InfoBox currentTab={currentTab} />
2327
</Col>
2428
</Container>
2529
);

0 commit comments

Comments
 (0)