Skip to content

Commit 57e5cc3

Browse files
author
Brendan Mulholland
committed
Migrate app to react-router v6 syntax; TODO tests
1 parent e8863fd commit 57e5cc3

File tree

6 files changed

+51
-50
lines changed

6 files changed

+51
-50
lines changed

src/app.tsx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, { useContext } from 'react';
22
import {
3-
Redirect,
3+
Navigate,
44
HashRouter as Router,
55
Route,
6-
Switch,
6+
Routes,
77
useLocation,
88
} from 'react-router-dom';
99

@@ -23,7 +23,7 @@ function RequireAuth({ children }) {
2323
return isLoggedIn ? (
2424
children
2525
) : (
26-
<Redirect to={{ pathname: '/login', state: { from: location } }} />
26+
<Navigate to="/login" replace state={{ from: location }} />
2727
);
2828
}
2929

@@ -35,30 +35,31 @@ export const App = () => {
3535
<Loading />
3636
<Sidebar />
3737

38-
<Switch>
39-
<Route path="/" exact>
40-
<RequireAuth>
41-
<NotificationsRoute />
42-
</RequireAuth>
43-
</Route>
44-
<Route path="/settings" exact>
45-
<RequireAuth>
46-
<SettingsRoute />
47-
</RequireAuth>
48-
</Route>
49-
<Route path="/login">
50-
<LoginRoute />
51-
</Route>
52-
<Route path="/login">
53-
<LoginRoute />
54-
</Route>
55-
<Route path="/login-enterprise">
56-
<LoginEnterpriseRoute />
57-
</Route>
58-
<Route path="/login-token">
59-
<LoginWithToken />
60-
</Route>
61-
</Switch>
38+
<Routes>
39+
<Route
40+
path="/"
41+
element={
42+
<RequireAuth>
43+
<NotificationsRoute />
44+
</RequireAuth>
45+
}
46+
/>
47+
<Route
48+
path="/settings"
49+
element={
50+
<RequireAuth>
51+
<SettingsRoute />
52+
</RequireAuth>
53+
}
54+
/>
55+
<Route path="/login" element={<LoginRoute />} />
56+
<Route path="/login" element={<LoginRoute />} />
57+
<Route
58+
path="/login-enterprise"
59+
element={<LoginEnterpriseRoute />}
60+
/>
61+
<Route path="/login-token" element={<LoginWithToken />} />
62+
</Routes>
6263
</div>
6364
</Router>
6465
</AppProvider>

src/components/Sidebar.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BellIcon } from '@primer/octicons-react';
22
import { ipcRenderer, shell } from 'electron';
33
import React, { useCallback, useContext, useMemo } from 'react';
4-
import { useHistory, useLocation } from 'react-router-dom';
4+
import { useNavigate, useLocation } from 'react-router-dom';
55

66
import { Logo } from '../components/Logo';
77
import { AppContext } from '../context/App';
@@ -11,7 +11,7 @@ import { IconRefresh } from '../icons/Refresh';
1111
import { Constants } from '../utils/constants';
1212

1313
export const Sidebar: React.FC = () => {
14-
const history = useHistory();
14+
const navigate = useNavigate();
1515
const location = useLocation();
1616

1717
const { isLoggedIn } = useContext(AppContext);
@@ -66,7 +66,7 @@ export const Sidebar: React.FC = () => {
6666
<button
6767
className={footerButtonClasses}
6868
onClick={() => {
69-
history.replace('/');
69+
navigate('/', { replace: true });
7070
fetchNotifications();
7171
}}
7272
aria-label="Refresh Notifications"
@@ -78,9 +78,9 @@ export const Sidebar: React.FC = () => {
7878
className={footerButtonClasses}
7979
onClick={() => {
8080
if (location.pathname.startsWith('/settings')) {
81-
history.replace('/');
81+
navigate('/', { replace: true });
8282
} else {
83-
history.push('/settings');
83+
navigate('/settings');
8484
}
8585
}}
8686
aria-label="Settings"

src/routes/Login.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
const { ipcRenderer } = require('electron');
22

33
import React, { useCallback, useContext, useEffect } from 'react';
4-
import { useHistory } from 'react-router-dom';
4+
import { useNavigate } from 'react-router-dom';
55

66
import { AppContext } from '../context/App';
77
import { Logo } from '../components/Logo';
88

99
export const LoginRoute: React.FC = () => {
10-
const history = useHistory();
10+
const navigate = useNavigate();
1111
const { isLoggedIn, login } = useContext(AppContext);
1212

1313
useEffect(() => {
1414
if (isLoggedIn) {
1515
ipcRenderer.send('reopen-window');
16-
history.replace('/');
16+
navigate('/', { replace: true });
1717
}
1818
}, [isLoggedIn]);
1919

@@ -46,15 +46,15 @@ export const LoginRoute: React.FC = () => {
4646

4747
<button
4848
className={loginButtonClass}
49-
onClick={() => history.push('/login-enterprise')}
49+
onClick={() => navigate('/login-enterprise')}
5050
aria-label="Login with GitHub Enterprise"
5151
>
5252
Login to GitHub Enterprise
5353
</button>
5454

5555
<button
5656
className="bg-none hover:text-gray-800 dark:text-gray-100 dark:hover:text-gray-300 mt-4 focus:outline-none"
57-
onClick={() => history.push('/login-token')}
57+
onClick={() => navigate('/login-token')}
5858
aria-label="Login with Personal Token"
5959
>
6060
<small>or login with a personal token</small>

src/routes/LoginEnterprise.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const ipcRenderer = require('electron').ipcRenderer;
33
import React, { useCallback, useContext, useEffect } from 'react';
44
import { Form, FormRenderProps } from 'react-final-form';
55
import { ArrowLeftIcon } from '@primer/octicons-react';
6-
import { useHistory } from 'react-router-dom';
6+
import { useNavigate } from 'react-router-dom';
77

88
import { AppContext } from '../context/App';
99
import { FieldInput } from '../components/fields/FieldInput';
@@ -55,12 +55,12 @@ export const LoginEnterpriseRoute: React.FC = () => {
5555
accounts: { enterpriseAccounts },
5656
loginEnterprise,
5757
} = useContext(AppContext);
58-
const history = useHistory();
58+
const navigate = useNavigate();
5959

6060
useEffect(() => {
6161
if (enterpriseAccounts.length) {
6262
ipcRenderer.send('reopen-window');
63-
history.goBack();
63+
navigate(-1);
6464
}
6565
}, [enterpriseAccounts]);
6666

@@ -109,7 +109,7 @@ export const LoginEnterpriseRoute: React.FC = () => {
109109
<button
110110
className="focus:outline-none"
111111
aria-label="Go Back"
112-
onClick={() => history.goBack()}
112+
onClick={() => navigate(-1)}
113113
>
114114
<ArrowLeftIcon size={20} className="hover:text-gray-400" />
115115
</button>

src/routes/LoginWithToken.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useCallback, useContext, useState } from 'react';
22
import { Form, FormRenderProps } from 'react-final-form';
33
import { ArrowLeftIcon } from '@primer/octicons-react';
4-
import { useHistory } from 'react-router-dom';
4+
import { useNavigate } from 'react-router-dom';
55
import { shell } from 'electron';
66

77
import { AppContext } from '../context/App';
@@ -42,7 +42,7 @@ export const validate = (values: IValues): IFormErrors => {
4242

4343
export const LoginWithToken: React.FC = () => {
4444
const { validateToken } = useContext(AppContext);
45-
const history = useHistory();
45+
const navigate = useNavigate();
4646
const [isValidToken, setIsValidToken] = useState<boolean>(true);
4747

4848
const openLink = useCallback((url: string) => {
@@ -108,8 +108,8 @@ export const LoginWithToken: React.FC = () => {
108108
const submit = useCallback(async (data: IValues) => {
109109
setIsValidToken(true);
110110
try {
111+
navigate(-1);
111112
await validateToken(data as AuthTokenOptions);
112-
history.goBack();
113113
} catch (err) {
114114
setIsValidToken(false);
115115
}
@@ -121,7 +121,7 @@ export const LoginWithToken: React.FC = () => {
121121
<button
122122
className="focus:outline-none"
123123
aria-label="Go Back"
124-
onClick={() => history.goBack()}
124+
onClick={() => navigate(-1)}
125125
>
126126
<ArrowLeftIcon size={20} className="hover:text-gray-400" />
127127
</button>

src/routes/Settings.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useCallback, useContext } from 'react';
22
import { ipcRenderer, remote } from 'electron';
3-
import { useHistory } from 'react-router-dom';
3+
import { useNavigate } from 'react-router-dom';
44
import { ArrowLeftIcon } from '@primer/octicons-react';
55

66
import { AppContext } from '../context/App';
@@ -17,7 +17,7 @@ const isLinux = remote.process.platform === 'linux';
1717

1818
export const SettingsRoute: React.FC = () => {
1919
const { settings, updateSetting, logout } = useContext(AppContext);
20-
const history = useHistory();
20+
const navigate = useNavigate();
2121

2222
ipcRenderer.on('update-native-theme', (_, updatedAppearance: Appearance) => {
2323
if (settings.appearance === Appearance.SYSTEM) {
@@ -27,7 +27,7 @@ export const SettingsRoute: React.FC = () => {
2727

2828
const logoutUser = useCallback(() => {
2929
logout();
30-
history.goBack();
30+
navigate(-1);
3131
updateTrayIcon();
3232
}, []);
3333

@@ -36,7 +36,7 @@ export const SettingsRoute: React.FC = () => {
3636
}, []);
3737

3838
const goToEnterprise = useCallback(() => {
39-
return history.replace('/login-enterprise');
39+
return navigate('/login-enterprise', { replace: true });
4040
}, []);
4141

4242
const footerButtonClass =
@@ -48,7 +48,7 @@ export const SettingsRoute: React.FC = () => {
4848
<button
4949
className="focus:outline-none"
5050
aria-label="Go Back"
51-
onClick={() => history.goBack()}
51+
onClick={() => navigate(-1)}
5252
>
5353
<ArrowLeftIcon size={20} className="hover:text-gray-400" />
5454
</button>

0 commit comments

Comments
 (0)