Skip to content

Commit 9a94067

Browse files
committed
web: update to react router 6
Signed-off-by: Nick Santos <nick.santos@docker.com>
1 parent a855d95 commit 9a94067

15 files changed

Lines changed: 238 additions & 231 deletions

web/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"react-from-dom": "^0.6.0",
1818
"react-modal": "^3.14.4",
1919
"react-outline-manager": "^1.2.2",
20-
"react-router": "^5.1.0",
21-
"react-router-dom": "^5.1.0",
20+
"react-router": "^6.23.1",
21+
"react-router-dom": "^6.23.1",
2222
"react-split-pane": "^0.1.92",
2323
"react-storage-hooks": "^4.0.1",
2424
"react-table": "^7.7.0",
@@ -67,7 +67,7 @@
6767
"@types/react": "^16.9.0",
6868
"@types/react-dom": "^16.9.0",
6969
"@types/react-modal": "^3.13.1",
70-
"@types/react-router-dom": "^5.3.2",
70+
"@types/react-router-dom": "^5.3.3",
7171
"@types/react-table": "^7.7.8",
7272
"@types/react-test-renderer": "^16.9.0",
7373
"@types/react-timeago": "^4.1.0",

web/src/HUD.test.tsx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { createMemoryHistory } from "history"
2-
import React from "react"
1+
import React, { Component } from "react"
32
import { findRenderedComponentWithType } from "react-dom/test-utils"
43
import ReactModal from "react-modal"
4+
import { useNavigate, useLocation } from "react-router-dom"
55
import { MemoryRouter } from "react-router"
66
import HUD, { mergeAppUpdate } from "./HUD"
77
import LogStore from "./LogStore"
@@ -21,22 +21,36 @@ import { SocketState } from "./types"
2121
// be set as the app root so that accessibility features are set correctly
2222
ReactModal.setAppElement(document.body)
2323

24-
const fakeHistory = createMemoryHistory()
2524
const interfaceVersion = { isNewDefault: () => false, toggleDefault: () => {} }
26-
const emptyHUD = () => {
25+
26+
let InjectHUD = () => {
27+
let navigate = useNavigate()
28+
let location = useLocation()
2729
return (
28-
<MemoryRouter initialEntries={["/"]}>
29-
<HUD history={fakeHistory} interfaceVersion={interfaceVersion} />
30-
</MemoryRouter>
30+
<HUD
31+
navigate={navigate}
32+
location={location}
33+
interfaceVersion={interfaceVersion}
34+
/>
3135
)
3236
}
3337

38+
class EmptyHUD extends Component {
39+
render() {
40+
return (
41+
<MemoryRouter initialEntries={["/"]}>
42+
<InjectHUD />
43+
</MemoryRouter>
44+
)
45+
}
46+
}
47+
3448
beforeEach(() => {
3549
Date.now = jest.fn(() => 1482363367071)
3650
})
3751

3852
it("renders reconnecting bar", async () => {
39-
const { rootTree, container } = renderTestComponent<HUD>(emptyHUD())
53+
const { rootTree, container } = renderTestComponent(<EmptyHUD />)
4054
expect(container.textContent).toEqual(expect.stringContaining("Loading"))
4155

4256
const hud = findRenderedComponentWithType(rootTree, HUD)
@@ -54,7 +68,7 @@ it("renders reconnecting bar", async () => {
5468
})
5569

5670
it("loads logs incrementally", async () => {
57-
const { rootTree } = renderTestComponent<HUD>(emptyHUD())
71+
const { rootTree } = renderTestComponent(<EmptyHUD />)
5872
const hud = findRenderedComponentWithType(rootTree, HUD)
5973

6074
let now = new Date().toString()
@@ -101,7 +115,7 @@ it("loads logs incrementally", async () => {
101115
})
102116

103117
it("renders logs to snapshot", async () => {
104-
const { rootTree } = renderTestComponent<HUD>(emptyHUD())
118+
const { rootTree } = renderTestComponent(<EmptyHUD />)
105119
const hud = findRenderedComponentWithType(rootTree, HUD)
106120

107121
let now = new Date().toString()

web/src/HUD.tsx

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { StylesProvider } from "@material-ui/core/styles"
2-
import { History } from "history"
32
import React, { Component } from "react"
43
import ReactOutlineManager from "react-outline-manager"
5-
import { useHistory } from "react-router"
6-
import { Route, RouteComponentProps, Switch } from "react-router-dom"
4+
import { useLocation, useNavigate, Location } from "react-router-dom"
5+
import { Route, Routes } from "react-router-dom"
76
import AnalyticsNudge from "./AnalyticsNudge"
87
import AppController from "./AppController"
98
import { tiltfileKeyContext } from "./BrowserStorage"
@@ -37,8 +36,9 @@ import {
3736
} from "./types"
3837

3938
export type HudProps = {
40-
history: History
4139
interfaceVersion: InterfaceVersion
40+
navigate: ReturnType<typeof useNavigate>
41+
location: Location
4242
}
4343

4444
// Snapshot logs are capped to 1MB (max upload size is 4MB; this ensures between the rest of state and JSON overhead
@@ -51,14 +51,16 @@ export default class HUD extends Component<HudProps, HudState> {
5151
// The root of the HUD view, without the slash.
5252
private pathBuilder: PathBuilder
5353
private controller: AppController
54-
private history: History
54+
private navigate: ReturnType<typeof useNavigate>
55+
private location: Location
5556

5657
constructor(props: HudProps) {
5758
super(props)
5859

5960
this.pathBuilder = new PathBuilder(window.location)
6061
this.controller = new AppController(this.pathBuilder, this)
61-
this.history = props.history
62+
this.navigate = props.navigate
63+
this.location = props.location
6264

6365
this.state = {
6466
view: {},
@@ -102,7 +104,7 @@ export default class HUD extends Component<HudProps, HudState> {
102104
}
103105

104106
setHistoryLocation(path: string) {
105-
this.props.history.replace(path)
107+
this.props.navigate(path, { replace: true })
106108
}
107109

108110
path(relPath: string) {
@@ -119,7 +121,7 @@ export default class HUD extends Component<HudProps, HudState> {
119121
}
120122
return {
121123
view: view,
122-
path: this.props.history.location.pathname,
124+
path: this.props.location.pathname,
123125
snapshotHighlight: state.snapshotHighlight,
124126
createdAt: new Date().toISOString(),
125127
}
@@ -226,27 +228,28 @@ export default class HUD extends Component<HudProps, HudState> {
226228
<ResourceGroupsContextProvider>
227229
<ResourceListOptionsProvider>
228230
<ResourceSelectionProvider>
229-
<Switch>
231+
<Routes>
230232
<Route
231233
path={this.path("/r/:name/overview")}
232-
render={(_props: RouteComponentProps<any>) => (
234+
element={
233235
<SidebarContextProvider>
234236
<OverviewResourcePane
235237
view={this.state.view}
236238
isSocketConnected={isSocketConnected}
237239
/>
238240
</SidebarContextProvider>
239-
)}
241+
}
240242
/>
241243
<Route
242-
render={() => (
244+
path="*"
245+
element={
243246
<OverviewTablePane
244247
view={this.state.view}
245248
isSocketConnected={isSocketConnected}
246249
/>
247-
)}
250+
}
248251
/>
249-
</Switch>
252+
</Routes>
250253
</ResourceSelectionProvider>
251254
</ResourceListOptionsProvider>
252255
</ResourceGroupsContextProvider>
@@ -304,12 +307,18 @@ export default class HUD extends Component<HudProps, HudState> {
304307
}
305308

306309
export function HUDFromContext(props: React.PropsWithChildren<{}>) {
307-
let history = useHistory()
308-
let interfaceVersion = useInterfaceVersion()
310+
const navigate = useNavigate()
311+
const location = useLocation()
312+
const interfaceVersion = useInterfaceVersion()
313+
309314
return (
310315
/* allow Styled Components to override MUI - https://material-ui.com/guides/interoperability/#controlling-priority-3*/
311316
<StylesProvider injectFirst>
312-
<HUD history={history} interfaceVersion={interfaceVersion} />
317+
<HUD
318+
interfaceVersion={interfaceVersion}
319+
navigate={navigate}
320+
location={location}
321+
/>
313322
</StylesProvider>
314323
)
315324
}

web/src/InterfaceVersion.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Cookies from "js-cookie"
22
import React, { PropsWithChildren, useContext, useState } from "react"
3-
import { useHistory } from "react-router"
3+
import { useNavigate } from "react-router-dom"
44
import { usePathBuilder } from "./PathBuilder"
55

66
export type InterfaceVersion = {
@@ -19,7 +19,6 @@ export function useInterfaceVersion(): InterfaceVersion {
1919

2020
export function InterfaceVersionProvider(props: PropsWithChildren<{}>) {
2121
let pathBuilder = usePathBuilder()
22-
let history = useHistory()
2322
let [isNew, setNew] = useState((): boolean => {
2423
return Cookies.get("tilt-interface-version") !== "legacy"
2524
})

web/src/OverviewActionBar.stories.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { createMemoryHistory } from "history"
21
import React from "react"
3-
import { Router } from "react-router"
2+
import { MemoryRouter } from "react-router"
43
import { ButtonSet } from "./ApiButton"
54
import { FilterLevel, FilterSource, useFilterSet } from "./logfilters"
65
import OverviewActionBar from "./OverviewActionBar"
@@ -14,18 +13,17 @@ export default {
1413
(Story: any, context: any) => {
1514
let level = context?.args?.level || ""
1615
let source = context?.args?.source || ""
17-
let history = createMemoryHistory()
18-
history.location.search = `?level=${level}&source=${source}`
16+
let initialEntries = [`/?level=${level}&source=${source}`]
1917
return (
20-
<Router history={history}>
18+
<MemoryRouter initialEntries={initialEntries}>
2119
<TiltSnackbarProvider>
2220
<StarredResourceMemoryProvider>
2321
<div style={{ margin: "-1rem", height: "80vh" }}>
2422
<Story {...context.args} />
2523
</div>
2624
</StarredResourceMemoryProvider>
2725
</TiltSnackbarProvider>
28-
</Router>
26+
</MemoryRouter>
2927
)
3028
},
3129
],

0 commit comments

Comments
 (0)