Skip to content

Commit 650bcd5

Browse files
authored
refactor: upgrade React Router to v6 (#1954)
1 parent f3dc017 commit 650bcd5

31 files changed

+337
-378
lines changed

jsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"compilerOptions": {
3+
"experimentalDecorators": true
4+
},
25
"typeAcquisition": {
36
"include": [
47
"jest"

package-lock.json

Lines changed: 48 additions & 115 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"express": "4.18.1",
4848
"graphiql": "2.0.7",
4949
"graphql": "16.6.0",
50-
"history": "4.10.1",
5150
"immutable": "4.1.0",
5251
"immutable-devtools": "0.1.5",
5352
"inquirer": "8.2.4",
@@ -68,8 +67,7 @@
6867
"react-helmet": "6.1.0",
6968
"react-json-view": "1.21.3",
7069
"react-popper-tooltip": "4.4.2",
71-
"react-router": "5.2.1",
72-
"react-router-dom": "5.3.0",
70+
"react-router-dom": "6.4.1",
7371
"regenerator-runtime": "0.13.9",
7472
"semver": "7.3.7",
7573
"typescript": "4.8.2"
@@ -103,7 +101,6 @@
103101
"madge": "5.0.1",
104102
"marked": "4.0.10",
105103
"null-loader": "4.0.1",
106-
"path-to-regexp": "3.2.0",
107104
"puppeteer": "18.0.5",
108105
"react-test-renderer": "16.13.1",
109106
"request": "2.88.2",

src/components/Autocomplete/Autocomplete.react.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* the root directory of this source tree.
77
*/
88
import Position from 'lib/Position';
9-
import PropTypes from 'prop-types'
9+
import PropTypes from 'lib/PropTypes'
1010
import React, { Component } from 'react';
1111
import styles from 'components/Autocomplete/Autocomplete.scss';
1212
import SuggestionsList from 'components/SuggestionsList/SuggestionsList.react';

src/components/Chip/Chip.react.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import React from 'react';
99
import styles from 'components/Chip/Chip.scss';
10-
import PropTypes from 'prop-types'
10+
import PropTypes from 'lib/PropTypes'
1111
import Icon from 'components/Icon/Icon.react'
1212

1313
let Chip = ({ value, onClose }) => (

src/components/FourOhFour/FourOhFour.react.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
* This source code is licensed under the license found in the LICENSE file in
66
* the root directory of this source tree.
77
*/
8-
import history from 'dashboard/history';
98
import React from 'react';
109
import styles from 'components/FourOhFour/FourOhFour.scss';
10+
import { withRouter } from 'lib/withRouter';
1111

1212
const EMOJI_COUNT = 30;
1313

14-
export default class FourOhFour extends React.Component {
14+
@withRouter
15+
class FourOhFour extends React.Component {
1516
constructor() {
1617
super();
1718

@@ -51,10 +52,12 @@ export default class FourOhFour extends React.Component {
5152
<div className={styles.message}>Oh no, we can't find that page!</div>
5253

5354
<div className={styles.back}>
54-
<button type='button' onClick={() => history.goBack()}>Go back</button>
55+
<button type='button' onClick={() => this.props.navigate(-1)}>Go back</button>
5556
</div>
5657
</div>
5758
</div>
5859
);
5960
}
6061
}
62+
63+
export default FourOhFour;

src/components/Sidebar/AppsMenu.react.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const AppsMenu = ({ apps, current, height, onSelect, onPinClick }) => (
2323
return null;
2424
}
2525
return (
26-
<Link to={{ pathname: html`/apps/${app.slug}/browser` }} key={app.slug} className={styles.menuRow} onClick={onSelect.bind(null, current.slug)}>
26+
<Link to={html`/apps/${app.slug}/browser`} key={app.slug} className={styles.menuRow} onClick={onSelect.bind(null, current.slug)}>
2727
<span>{app.name}</span>
2828
<AppBadge production={app.production} />
2929
</Link>

src/components/Toolbar/Toolbar.react.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import PropTypes from 'lib/PropTypes';
99
import React from 'react';
1010
import Icon from 'components/Icon/Icon.react';
1111
import styles from 'components/Toolbar/Toolbar.scss';
12-
import history from 'dashboard/history';
13-
14-
const goBack = () => history.goBack();
12+
import { useNavigate, useNavigationType, NavigationType } from 'react-router-dom';
1513

1614
let Toolbar = (props) => {
15+
const action = useNavigationType();
16+
const navigate = useNavigate();
1717
let backButton;
18-
if ((props.relation || (props.filters && props.filters.size)) && history.action !== 'POP') {
18+
if ((props.relation || (props.filters && props.filters.size)) && action !== NavigationType.Pop) {
1919
backButton = (
2020
<a
2121
className={styles.iconButton}
22-
onClick={goBack}
22+
onClick={() => navigate(-1)}
2323
>
2424
<Icon
2525
width={32}

src/dashboard/Analytics/Explorer/Explorer.react.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import stylesTable from 'components/Table/Table.scss';
2828
import subscribeTo from 'lib/subscribeTo';
2929
import Toolbar from 'components/Toolbar/Toolbar.react';
3030
import baseStyles from 'stylesheets/base.scss';
31+
import { withRouter } from 'lib/withRouter';
3132

3233
let buildFriendlyName = (query) => {
3334
let name = [query.source];
@@ -38,8 +39,8 @@ let buildFriendlyName = (query) => {
3839
return name.join(' ');
3940
};
4041

41-
export default
4242
@subscribeTo('AnalyticsQuery', 'customQueries')
43+
@withRouter
4344
class Explorer extends DashboardView {
4445
constructor() {
4546
super();
@@ -547,3 +548,5 @@ class Explorer extends DashboardView {
547548
);
548549
}
549550
}
551+
552+
export default Explorer;

src/dashboard/AppData.react.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,33 @@
88
import React from 'react';
99
import AppSelector from 'dashboard/AppSelector.react';
1010
import AppsManager from 'lib/AppsManager';
11-
import history from 'dashboard/history';
1211
import { CurrentApp } from 'context/currentApp';
12+
import { Outlet, useNavigate , useParams} from 'react-router-dom';
1313

14-
class AppData extends React.Component {
15-
render() {
16-
if (this.props.params.appId === '_') {
17-
return <AppSelector />;
18-
}
19-
//Find by name to catch edge cases around escaping apostrophes in URLs
20-
let current = AppsManager.findAppBySlugOrName(this.props.params.appId);
21-
if (current) {
22-
current.setParseKeys();
23-
} else {
24-
history.replace('/apps');
25-
return <div />;
26-
}
27-
return (
28-
<CurrentApp.Provider value={current}>
29-
<div>
30-
{this.props.children}
31-
</div>
32-
</CurrentApp.Provider>
33-
);
14+
15+
function AppData() {
16+
const navigate = useNavigate();
17+
const params = useParams();
18+
19+
if (params.appId === '_') {
20+
return <AppSelector />;
21+
}
22+
23+
// Find by name to catch edge cases around escaping apostrophes in URLs
24+
let current = AppsManager.findAppBySlugOrName(params.appId);
25+
26+
if (current) {
27+
current.setParseKeys();
28+
} else {
29+
navigate('/apps', { replace: true });
30+
return <div />;
3431
}
32+
33+
return (
34+
<CurrentApp.Provider value={current}>
35+
<Outlet />
36+
</CurrentApp.Provider>
37+
);
3538
}
3639

3740
export default AppData;

src/dashboard/AppSelector.react.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
import AppsManager from 'lib/AppsManager';
99
import Dropdown from 'components/Dropdown/Dropdown.react';
1010
import Field from 'components/Field/Field.react';
11-
import history from 'dashboard/history';
1211
import Label from 'components/Label/Label.react';
1312
import Modal from 'components/Modal/Modal.react';
1413
import Option from 'components/Dropdown/Option.react';
1514
import React from 'react';
15+
import { withRouter } from 'lib/withRouter';
1616

17-
export default class AppSelector extends React.Component {
17+
@withRouter
18+
class AppSelector extends React.Component {
1819
constructor(props) {
1920
super(props);
2021
let apps = AppsManager.apps();
@@ -25,12 +26,12 @@ export default class AppSelector extends React.Component {
2526
}
2627

2728
handleConfirm() {
28-
let newPath = location.pathname.replace(/\/_(\/|$)/, '/' + this.state.slug + '/');
29-
history.push(newPath);
29+
let newPath = this.location.pathname.replace(/\/_(\/|$)/, '/' + this.state.slug + '/');
30+
this.props.navigate(newPath);
3031
}
3132

3233
handleCancel() {
33-
history.push('/apps');
34+
this.props.navigate('/apps');
3435
}
3536

3637
render() {
@@ -58,3 +59,5 @@ export default class AppSelector extends React.Component {
5859
);
5960
}
6061
}
62+
63+
export default AppSelector;

src/dashboard/Apps/AppsIndex.react.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88
import AppsManager from 'lib/AppsManager';
99
import FlowFooter from 'components/FlowFooter/FlowFooter.react';
10-
import history from 'dashboard/history';
1110
import html from 'lib/htmlString';
1211
import Icon from 'components/Icon/Icon.react';
1312
import joinWithFinal from 'lib/joinWithFinal';
@@ -17,6 +16,8 @@ import React from 'react';
1716
import styles from 'dashboard/Apps/AppsIndex.scss';
1817
import baseStyles from 'stylesheets/base.scss';
1918
import AppBadge from 'components/AppBadge/AppBadge.react';
19+
import { withRouter } from 'lib/withRouter';
20+
import { useNavigate } from 'react-router-dom';
2021

2122
function dash(value, content) {
2223
if (value === undefined) {
@@ -64,7 +65,8 @@ let AppCard = ({
6465
app,
6566
icon,
6667
}) => {
67-
let canBrowse = app.serverInfo.error ? null : () => history.push(html`/apps/${app.slug}/browser`);
68+
const navigate = useNavigate();
69+
let canBrowse = app.serverInfo.error ? null : () => navigate(html`/apps/${app.slug}/browser`);
6870
let versionMessage = app.serverInfo.error ?
6971
<div className={styles.serverVersion}>Server not reachable: <span className={styles.ago}>{app.serverInfo.error.toString()}</span></div>:
7072
<div className={styles.serverVersion}>
@@ -88,7 +90,8 @@ let AppCard = ({
8890
</li>
8991
}
9092

91-
export default class AppsIndex extends React.Component {
93+
@withRouter
94+
class AppsIndex extends React.Component {
9295
constructor() {
9396
super();
9497
this.state = { search: '' };
@@ -99,7 +102,7 @@ export default class AppsIndex extends React.Component {
99102
componentWillMount() {
100103
if (AppsManager.apps().length === 1) {
101104
const [app] = AppsManager.apps();
102-
history.push(`/apps/${app.slug}/browser`);
105+
this.props.navigate(`/apps/${app.slug}/browser`);
103106
return;
104107
}
105108
document.body.addEventListener('keydown', this.focusField);
@@ -169,3 +172,5 @@ export default class AppsIndex extends React.Component {
169172
);
170173
}
171174
}
175+
176+
export default AppsIndex;

0 commit comments

Comments
 (0)