Skip to content

Commit e47dc47

Browse files
committed
Fix issues caused by the upgrade
Changes: * Updated imports to be from `react-router-dom` as opposed to `react-router` * `react-router` v6 provides a cleaner way of managing routes and nested routes. Refactored `Routes` and `SettingsPage` to reflect this. * Renamed `Routes` to `AppRoutes` to avoid confict with `react-router`'s `Routes` component. * Using `BrowserRouter` instead of `Router` * Use `useNavigate` and `useLocation` hooks in the component instead of using `routerStore` to avoid `history` dependency and to avoid calling `createBrowserHistory`
1 parent 555ae7c commit e47dc47

File tree

17 files changed

+201
-176
lines changed

17 files changed

+201
-176
lines changed

app/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import AlertContainer from 'components/common/AlertContainer';
55
import FullHeight from 'components/common/FullHeight';
66
import { ThemeProvider } from 'components/theme';
77
import TourHost from 'components/tour/TourHost';
8-
import Routes from './Routes';
8+
import AppRoutes from './AppRoutes';
99

1010
const App = () => {
1111
const store = createStore();
@@ -14,7 +14,7 @@ const App = () => {
1414
<FullHeight>
1515
<StoreProvider store={store}>
1616
<ThemeProvider>
17-
<Routes />
17+
<AppRoutes />
1818
<AlertContainer />
1919
<TourHost />
2020
</ThemeProvider>

app/src/AppRoutes.tsx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React, { Suspense } from 'react';
2+
import { BrowserRouter, Route, Routes } from 'react-router-dom';
3+
import Loading from 'components/common/Loading';
4+
import { Layout } from 'components/layout';
5+
6+
const LazyAuthPage = React.lazy(() => import('components/auth/AuthPage'));
7+
const LazyLoopPage = React.lazy(() => import('components/loop/LoopPage'));
8+
const LazyHistoryPage = React.lazy(() => import('components/history/HistoryPage'));
9+
const LazyPoolPage = React.lazy(() => import('components/pool/PoolPage'));
10+
const LazySettingsPage = React.lazy(() => import('components/settings/SettingsPage'));
11+
const LazyConnectPage = React.lazy(() => import('components/connect/ConnectPage'));
12+
13+
const AppRoutes: React.FC = () => {
14+
return (
15+
<Suspense fallback={<Loading delay={500} />}>
16+
<BrowserRouter>
17+
<Routes>
18+
<Route path="/" element={<LazyAuthPage />} />
19+
<Route>
20+
<Route
21+
path="loop"
22+
element={
23+
<Layout>
24+
<LazyLoopPage />
25+
</Layout>
26+
}
27+
/>
28+
<Route
29+
path="history"
30+
element={
31+
<Layout>
32+
<LazyHistoryPage />
33+
</Layout>
34+
}
35+
/>
36+
<Route
37+
path="pool"
38+
element={
39+
<Layout>
40+
<LazyPoolPage />
41+
</Layout>
42+
}
43+
/>
44+
{/* Only the GeneralSettings component needs hamburger menu
45+
hence not wrapping entire settings page in <Layout>. */}
46+
<Route path="settings/*" element={<LazySettingsPage />} />
47+
<Route
48+
path="connect"
49+
element={
50+
<Layout>
51+
<LazyConnectPage />
52+
</Layout>
53+
}
54+
/>
55+
</Route>
56+
</Routes>
57+
</BrowserRouter>
58+
</Suspense>
59+
);
60+
};
61+
62+
export default AppRoutes;

app/src/Routes.tsx

Lines changed: 0 additions & 41 deletions
This file was deleted.

app/src/__stories__/StoryWrapper.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { CSSProperties, useMemo } from 'react';
2-
import { Router } from 'react-router';
2+
import { BrowserRouter } from 'react-router-dom';
33
import { observer } from 'mobx-react-lite';
44
import { BalanceMode, Unit } from 'util/constants';
55
import { AuthenticationError } from 'util/errors';
@@ -70,13 +70,13 @@ const StoryWrapper: React.FC<{
7070
return (
7171
<StoreProvider store={store}>
7272
<ThemeProvider>
73-
<Router history={store.router.history}>
73+
<BrowserRouter>
7474
{/* modify the bg styles so it isn't too big in docs mode */}
7575
<Background style={{ minHeight: 'inherit', height: '100%' }}>
7676
{/* render the Story after the store has been initialized */}
7777
{store.initialized ? <div style={style}>{children}</div> : null}
7878
</Background>
79-
</Router>
79+
</BrowserRouter>
8080
<AlertContainer />
8181
</ThemeProvider>
8282
</StoreProvider>

app/src/__tests__/Routes.spec.tsx renamed to app/src/__tests__/AppRoutes.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import Routes from 'Routes';
2+
import AppRoutes from 'AppRoutes';
33
import { renderWithProviders } from 'util/tests';
44
import { createStore, Store } from 'store';
55

@@ -12,7 +12,7 @@ describe('Routes Component', () => {
1212
});
1313

1414
const render = () => {
15-
return renderWithProviders(<Routes />, store);
15+
return renderWithProviders(<AppRoutes />, store);
1616
};
1717

1818
it('should display the Auth page by default', async () => {

app/src/components/common/PageHeader.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import styled from '@emotion/styled';
44
import { usePrefixedTranslation } from 'hooks';
55
import { ArrowLeft, Download, HeaderThree, HelpCircle } from '../base';
66
import Tip from './Tip';
7+
import { useNavigate } from 'react-router-dom';
78

89
const Styled = {
910
Wrapper: styled.div`
@@ -43,7 +44,7 @@ const Styled = {
4344

4445
interface Props {
4546
title: ReactNode;
46-
onBackClick?: () => void;
47+
showBackButton?: boolean;
4748
backText?: string;
4849
onHelpClick?: () => void;
4950
exportTip?: string;
@@ -52,19 +53,23 @@ interface Props {
5253

5354
const PageHeader: React.FC<Props> = ({
5455
title,
55-
onBackClick,
56+
showBackButton,
5657
backText,
5758
onHelpClick,
5859
exportTip,
5960
onExportClick,
6061
}) => {
6162
const { l } = usePrefixedTranslation('cmps.common.PageHeader');
63+
const navigate = useNavigate();
64+
const onBackClick = () => {
65+
navigate(-1);
66+
};
6267

6368
const { Wrapper, Left, Center, Right, BackLink } = Styled;
6469
return (
6570
<Wrapper>
6671
<Left>
67-
{onBackClick && (
72+
{showBackButton && (
6873
<BackLink onClick={onBackClick}>
6974
<ArrowLeft />
7075
{backText}

app/src/components/layout/Layout.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import styled from '@emotion/styled';
55
import { useStore } from 'store';
66
import { Background, Menu } from 'components/base';
77
import Sidebar from './Sidebar';
8+
import { useLocation } from 'react-router-dom';
89

910
interface CollapsedProps {
1011
collapsed: boolean;
@@ -110,11 +111,13 @@ const Styled = {
110111

111112
export const Layout: React.FC = ({ children }) => {
112113
const { settingsStore, appView } = useStore();
114+
const location = useLocation();
115+
const fullWidth = location.pathname === `/pool`;
113116

114117
const { Container, Hamburger, Aside, Content, Fluid } = Styled;
115118
return (
116119
<Background>
117-
<Container fullWidth={appView.fullWidth}>
120+
<Container fullWidth={fullWidth}>
118121
<Hamburger
119122
collapsed={!settingsStore.sidebarVisible}
120123
onClick={settingsStore.toggleSidebar}
@@ -124,7 +127,7 @@ export const Layout: React.FC = ({ children }) => {
124127
<Aside collapsed={!settingsStore.sidebarVisible}>
125128
<Sidebar />
126129
</Aside>
127-
<Content collapsed={!settingsStore.sidebarVisible} fullWidth={appView.fullWidth}>
130+
<Content collapsed={!settingsStore.sidebarVisible} fullWidth={fullWidth}>
128131
<Fluid className="container-fluid">{children}</Fluid>
129132
</Content>
130133
</Container>

app/src/components/layout/NavMenu.tsx

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { usePrefixedTranslation } from 'hooks';
55
import { useStore } from 'store';
66
import { Badge, HeaderFour } from 'components/base';
77
import { PUBLIC_URL } from '../../config';
8+
import { Link } from 'react-router-dom';
9+
import { useLocation } from 'react-router-dom';
810

911
const Styled = {
1012
NavHeader: styled(HeaderFour)`
@@ -19,7 +21,7 @@ const Styled = {
1921
font-size: ${props => props.theme.sizes.xs};
2022
margin-right: -17px;
2123
22-
> span {
24+
> a > span {
2325
display: block;
2426
height: 50px;
2527
line-height: 50px;
@@ -35,7 +37,7 @@ const Styled = {
3537
}
3638
}
3739
38-
&.active > span {
40+
&.active > a > span {
3941
border-left: 3px solid ${props => props.theme.colors.offWhite};
4042
background-color: ${props => props.theme.colors.blue};
4143
@@ -49,42 +51,44 @@ const Styled = {
4951
const NavItem: React.FC<{
5052
page: string;
5153
badge?: string;
52-
onClick: () => void;
53-
}> = observer(({ page, badge, onClick }) => {
54+
}> = observer(({ page, badge }) => {
5455
const { l } = usePrefixedTranslation('cmps.layout.NavMenu');
55-
const { router } = useStore();
56-
const className = router.location.pathname.startsWith(`${PUBLIC_URL}/${page}`)
57-
? 'active'
58-
: '';
56+
const { settingsStore, log } = useStore();
57+
const location = useLocation();
58+
const className = location.pathname.startsWith(`/${page}`) ? 'active' : '';
5959

60+
const onClick = () => {
61+
settingsStore.autoCollapseSidebar();
62+
log.info(`Go to the ${page} page`);
63+
};
6064
return (
6165
<Styled.NavItem className={className}>
62-
<span onClick={onClick}>
63-
{l(page)}
64-
{badge && (
65-
<sup>
66-
<Badge muted>{badge}</Badge>
67-
</sup>
68-
)}
69-
</span>
66+
<Link to={`/${page}`} onClick={onClick}>
67+
<span>
68+
{l(page)}
69+
{badge && (
70+
<sup>
71+
<Badge muted>{badge}</Badge>
72+
</sup>
73+
)}
74+
</span>
75+
</Link>
7076
</Styled.NavItem>
7177
);
7278
});
7379

7480
const NavMenu: React.FC = () => {
7581
const { l } = usePrefixedTranslation('cmps.layout.NavMenu');
76-
const { appView } = useStore();
77-
7882
const { NavHeader, Nav } = Styled;
7983
return (
8084
<>
8185
<NavHeader>{l('menu')}</NavHeader>
8286
<Nav>
83-
<NavItem page="loop" onClick={appView.goToLoop} />
84-
<NavItem page="history" onClick={appView.goToHistory} />
85-
<NavItem page="pool" badge={l('common.preview')} onClick={appView.goToPool} />
86-
<NavItem page="settings" onClick={appView.goToSettings} />
87-
<NavItem page="connect" badge={l('common.beta')} onClick={appView.goToConnect} />
87+
<NavItem page="loop" />
88+
<NavItem page="history" />
89+
<NavItem page="pool" badge={l('common.preview')} />
90+
<NavItem page="settings" />
91+
<NavItem page="connect" badge={l('common.beta')} />
8892
</Nav>
8993
</>
9094
);

app/src/components/settings/BalanceSettings.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ const BalanceSettings: React.FC = () => {
4343
const { Wrapper, Content } = Styled;
4444
return (
4545
<Wrapper>
46-
<PageHeader
47-
title={l('pageTitle')}
48-
backText={l('backText')}
49-
onBackClick={handleBack}
50-
/>
46+
<PageHeader title={l('pageTitle')} backText={l('backText')} showBackButton />
5147
<Content>
5248
<HeaderFour>{l('title')}</HeaderFour>
5349
<BalanceModeItem mode={BalanceMode.receive} />

app/src/components/settings/ExplorerSettings.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,7 @@ const ExplorerSettings: React.FC = () => {
9898
const { Wrapper, Content, Actions } = Styled;
9999
return (
100100
<Wrapper>
101-
<PageHeader
102-
title={l('pageTitle')}
103-
backText={l('backText')}
104-
onBackClick={handleBack}
105-
/>
101+
<PageHeader title={l('pageTitle')} backText={l('backText')} showBackButton />
106102
<Content>
107103
<ExplorerField
108104
label={l('bitcoinTxUrl')}

0 commit comments

Comments
 (0)