Skip to content

Commit

Permalink
web: fix re-rendering on a few providers
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks committed Feb 10, 2022
1 parent e08c400 commit 288fba1
Show file tree
Hide file tree
Showing 18 changed files with 246 additions and 127 deletions.
83 changes: 35 additions & 48 deletions web/src/HUD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import AppController from "./AppController"
import { tiltfileKeyContext } from "./BrowserStorage"
import ErrorModal from "./ErrorModal"
import FatalErrorModal from "./FatalErrorModal"
import Features, { FeaturesProvider, Flag } from "./feature"
import { FeaturesProvider } from "./feature"
import HeroScreen from "./HeroScreen"
import "./HUD.scss"
import { HudErrorContextProvider } from "./HudErrorContext"
Expand Down Expand Up @@ -163,15 +163,6 @@ export default class HUD extends Component<HudProps, HudState> {
})
}

private getFeatures(): Features {
let featureFlags = {} as { [key: string]: boolean }
let flagList = this.state.view.uiSession?.status?.featureFlags || []
flagList.forEach((flag) => {
featureFlags[flag.name || ""] = !!flag.value
})
return new Features(featureFlags)
}

handleShowCopySuccess() {
this.setState(
{
Expand Down Expand Up @@ -248,45 +239,36 @@ export default class HUD extends Component<HudProps, HudState> {
}

renderOverviewSwitch() {
const features = this.getFeatures()
let showSnapshot =
features.isEnabled(Flag.Snapshots) && !this.pathBuilder.isSnapshot()
let snapshotAction = {
enabled: showSnapshot,
openModal: this.handleOpenModal,
}

return (
/* allow Styled Components to override MUI - https://material-ui.com/guides/interoperability/#controlling-priority-3*/
<StylesProvider injectFirst>
<FeaturesProvider value={features}>
<SnapshotActionProvider value={snapshotAction}>
<PathBuilderProvider value={this.pathBuilder}>
<LogStoreProvider value={this.state.logStore || new LogStore()}>
<ResourceGroupsContextProvider>
<ResourceListOptionsProvider>
<ResourceSelectionProvider>
<Switch>
<Route
path={this.path("/r/:name/overview")}
render={(props: RouteComponentProps<any>) => (
<OverviewResourcePane view={this.state.view} />
)}
/>
<Route
render={() => (
<OverviewTablePane view={this.state.view} />
)}
/>
</Switch>
</ResourceSelectionProvider>
</ResourceListOptionsProvider>
</ResourceGroupsContextProvider>
</LogStoreProvider>
</PathBuilderProvider>
<FeaturesProvider
featureFlags={this.state.view.uiSession?.status?.featureFlags || null}
>
<PathBuilderProvider value={this.pathBuilder}>
<SnapshotActionProvider openModal={this.handleOpenModal}>
<LogStoreProvider value={this.state.logStore || new LogStore()}>
<ResourceGroupsContextProvider>
<ResourceListOptionsProvider>
<ResourceSelectionProvider>
<Switch>
<Route
path={this.path("/r/:name/overview")}
render={(props: RouteComponentProps<any>) => (
<OverviewResourcePane view={this.state.view} />
)}
/>
<Route
render={() => (
<OverviewTablePane view={this.state.view} />
)}
/>
</Switch>
</ResourceSelectionProvider>
</ResourceListOptionsProvider>
</ResourceGroupsContextProvider>
</LogStoreProvider>
</SnapshotActionProvider>
</FeaturesProvider>
</StylesProvider>
</PathBuilderProvider>
</FeaturesProvider>
)
}

Expand Down Expand Up @@ -356,7 +338,12 @@ export default class HUD extends Component<HudProps, HudState> {
export function HUDFromContext(props: React.PropsWithChildren<{}>) {
let history = useHistory()
let interfaceVersion = useInterfaceVersion()
return <HUD history={history} interfaceVersion={interfaceVersion} />
return (
/* allow Styled Components to override MUI - https://material-ui.com/guides/interoperability/#controlling-priority-3*/
<StylesProvider injectFirst>
<HUD history={history} interfaceVersion={interfaceVersion} />
</StylesProvider>
)
}

function compareObjectsOrder<
Expand Down
6 changes: 3 additions & 3 deletions web/src/HeaderBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { act } from "react-dom/test-utils"
import { MemoryRouter } from "react-router-dom"
import { TwoResources } from "./HeaderBar.stories"
import HelpDialog from "./HelpDialog"
import { SnapshotActionProvider } from "./snapshot"
import { SnapshotActionValueProvider } from "./snapshot"

it("renders shortcuts dialog on ?", () => {
const root = mount(
Expand All @@ -29,9 +29,9 @@ it("opens snapshot modal on s", () => {
}
const root = mount(
<MemoryRouter initialEntries={["/"]}>
<SnapshotActionProvider value={snapshot}>
<SnapshotActionValueProvider value={snapshot}>
{TwoResources()}
</SnapshotActionProvider>
</SnapshotActionValueProvider>
</MemoryRouter>
)

Expand Down
6 changes: 3 additions & 3 deletions web/src/OverviewResourceBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { act } from "react-dom/test-utils"
import { MemoryRouter } from "react-router-dom"
import HelpDialog from "./HelpDialog"
import { TwoResources } from "./OverviewResourceBar.stories"
import { SnapshotActionProvider } from "./snapshot"
import { SnapshotActionValueProvider } from "./snapshot"

it("renders shortcuts dialog on ?", () => {
const root = mount(
Expand All @@ -29,9 +29,9 @@ it("opens snapshot modal on s", () => {
}
const root = mount(
<MemoryRouter initialEntries={["/"]}>
<SnapshotActionProvider value={snapshot}>
<SnapshotActionValueProvider value={snapshot}>
{TwoResources()}
</SnapshotActionProvider>
</SnapshotActionValueProvider>
</MemoryRouter>
)

Expand Down
6 changes: 3 additions & 3 deletions web/src/OverviewResourcePane.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StylesProvider } from "@material-ui/core/styles"
import React from "react"
import { MemoryRouter } from "react-router"
import Features, { FeaturesProvider, Flag } from "./feature"
import Features, { FeaturesValueProvider, Flag } from "./feature"
import LogStore, { LogStoreProvider } from "./LogStore"
import OverviewResourcePane from "./OverviewResourcePane"
import { ResourceGroupsContextProvider } from "./ResourceGroupsContext"
Expand All @@ -27,7 +27,7 @@ export default {
return (
<MemoryRouter initialEntries={["/"]}>
<TiltSnackbarProvider>
<FeaturesProvider value={features}>
<FeaturesValueProvider value={features}>
<ResourceGroupsContextProvider>
<ResourceListOptionsProvider>
<StarredResourceMemoryProvider>
Expand All @@ -39,7 +39,7 @@ export default {
</StarredResourceMemoryProvider>
</ResourceListOptionsProvider>
</ResourceGroupsContextProvider>
</FeaturesProvider>
</FeaturesValueProvider>
</TiltSnackbarProvider>
</MemoryRouter>
)
Expand Down
6 changes: 3 additions & 3 deletions web/src/OverviewResourceSidebar.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react"
import { MemoryRouter } from "react-router"
import SplitPane from "react-split-pane"
import Features, { FeaturesProvider, Flag } from "./feature"
import Features, { FeaturesValueProvider, Flag } from "./feature"
import LogStore, { LogStoreProvider } from "./LogStore"
import OverviewResourceSidebar from "./OverviewResourceSidebar"
import { ResourceGroupsContextProvider } from "./ResourceGroupsContext"
Expand All @@ -27,7 +27,7 @@ export default {
})
return (
<MemoryRouter initialEntries={["/"]}>
<FeaturesProvider value={features}>
<FeaturesValueProvider value={features}>
<ResourceGroupsContextProvider>
<div style={{ margin: "-1rem", height: "80vh" }}>
<SplitPane
Expand All @@ -39,7 +39,7 @@ export default {
</SplitPane>
</div>
</ResourceGroupsContextProvider>
</FeaturesProvider>
</FeaturesValueProvider>
</MemoryRouter>
)
},
Expand Down
6 changes: 3 additions & 3 deletions web/src/OverviewTable.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react"
import { MemoryRouter } from "react-router"
import Features, { FeaturesProvider, Flag } from "./feature"
import Features, { FeaturesValueProvider, Flag } from "./feature"
import LogStore, { LogStoreProvider } from "./LogStore"
import OverviewTable from "./OverviewTable"
import { ResourceGroupsContextProvider } from "./ResourceGroupsContext"
Expand Down Expand Up @@ -28,7 +28,7 @@ export default {
return (
<MemoryRouter initialEntries={["/"]}>
<TiltSnackbarProvider>
<FeaturesProvider value={features}>
<FeaturesValueProvider value={features}>
<ResourceGroupsContextProvider>
<ResourceListOptionsProvider>
<ResourceSelectionProvider>
Expand All @@ -38,7 +38,7 @@ export default {
</ResourceSelectionProvider>
</ResourceListOptionsProvider>
</ResourceGroupsContextProvider>
</FeaturesProvider>
</FeaturesValueProvider>
</TiltSnackbarProvider>
</MemoryRouter>
)
Expand Down
6 changes: 3 additions & 3 deletions web/src/OverviewTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
mockAnalyticsCalls,
} from "./analytics_test_helpers"
import { ApiButton } from "./ApiButton"
import Features, { FeaturesProvider, Flag } from "./feature"
import Features, { FeaturesValueProvider, Flag } from "./feature"
import { GroupByLabelView, TILTFILE_LABEL, UNLABELED_LABEL } from "./labels"
import LogStore from "./LogStore"
import OverviewTable, {
Expand Down Expand Up @@ -85,7 +85,7 @@ const tableViewWithSettings = ({
return (
<MemoryRouter initialEntries={["/"]}>
<SnackbarProvider>
<FeaturesProvider value={features}>
<FeaturesValueProvider value={features}>
<ResourceGroupsContextProvider>
<ResourceListOptionsProvider
initialValuesForTesting={resourceListOptions}
Expand All @@ -97,7 +97,7 @@ const tableViewWithSettings = ({
</ResourceSelectionProvider>
</ResourceListOptionsProvider>
</ResourceGroupsContextProvider>
</FeaturesProvider>
</FeaturesValueProvider>
</SnackbarProvider>
</MemoryRouter>
)
Expand Down
6 changes: 3 additions & 3 deletions web/src/OverviewTableBulkActions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "./analytics_test_helpers"
import { buttonsByComponent } from "./ApiButton"
import { mockUIButtonUpdates } from "./ApiButton.testhelpers"
import Features, { FeaturesProvider, Flag } from "./feature"
import Features, { FeaturesValueProvider, Flag } from "./feature"
import {
BulkAction,
buttonsByAction,
Expand All @@ -36,11 +36,11 @@ const OverviewTableBulkActionsTestWrapper = (props: {
const { flagEnabled, resourceSelections } = props
const features = new Features({ [Flag.DisableResources]: flagEnabled })
return (
<FeaturesProvider value={features}>
<FeaturesValueProvider value={features}>
<ResourceSelectionProvider initialValuesForTesting={resourceSelections}>
<OverviewTableBulkActions uiButtons={TEST_UIBUTTONS} />
</ResourceSelectionProvider>
</FeaturesProvider>
</FeaturesValueProvider>
)
}

Expand Down
6 changes: 3 additions & 3 deletions web/src/OverviewTablePane.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react"
import { MemoryRouter } from "react-router"
import Features, { FeaturesProvider, Flag } from "./feature"
import Features, { FeaturesValueProvider, Flag } from "./feature"
import OverviewTablePane from "./OverviewTablePane"
import { ResourceGroupsContextProvider } from "./ResourceGroupsContext"
import { ResourceListOptionsProvider } from "./ResourceListOptionsContext"
Expand All @@ -23,7 +23,7 @@ export default {
})
return (
<MemoryRouter initialEntries={["/"]}>
<FeaturesProvider value={features}>
<FeaturesValueProvider value={features}>
<ResourceListOptionsProvider>
<StarredResourceMemoryProvider>
<ResourceGroupsContextProvider>
Expand All @@ -35,7 +35,7 @@ export default {
</ResourceGroupsContextProvider>
</StarredResourceMemoryProvider>
</ResourceListOptionsProvider>
</FeaturesProvider>
</FeaturesValueProvider>
</MemoryRouter>
)
},
Expand Down
17 changes: 12 additions & 5 deletions web/src/ResourceNav.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { render } from "@testing-library/react"
import { mount, ReactWrapper } from "enzyme"
import { createMemoryHistory, MemoryHistory } from "history"
import React from "react"
Expand Down Expand Up @@ -80,15 +81,15 @@ describe("resourceNav", () => {
// Make sure that useResourceNav() doesn't break memoization.
it("memoizes renders", () => {
let renderCount = 0
let FakeEl = () => {
let FakeEl = React.memo(() => {
useResourceNav()
renderCount++
return <div></div>
}
})

let history = createMemoryHistory()
let validateResource = () => true
let root = mount(
let { rerender } = render(
<Router history={history}>
<ResourceNavProvider validateResource={validateResource}>
<FakeEl />
Expand All @@ -99,11 +100,17 @@ describe("resourceNav", () => {
expect(renderCount).toEqual(1)

// Make sure we don't re-render on a no-op history update.
root.setProps({ history })
rerender(
<Router history={history}>
<ResourceNavProvider validateResource={validateResource}>
<FakeEl />
</ResourceNavProvider>
</Router>
)
expect(renderCount).toEqual(1)

// Make sure we do re-render on a real location update.
history.push("/r/foo")
act(() => history.push("/r/foo"))
expect(renderCount).toEqual(2)
})
})
6 changes: 3 additions & 3 deletions web/src/SidebarItemView.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mount } from "enzyme"
import React from "react"
import Features, { FeaturesProvider, Flag } from "./feature"
import Features, { FeaturesValueProvider, Flag } from "./feature"
import { LogAlertIndex } from "./LogStore"
import PathBuilder from "./PathBuilder"
import SidebarItem from "./SidebarItem"
Expand All @@ -27,15 +27,15 @@ const SidebarItemViewTestWrapper = ({
[Flag.DisableResources]: disableResourcesEnabled ?? true,
})
return (
<FeaturesProvider value={features}>
<FeaturesValueProvider value={features}>
<SidebarItemView
item={item}
selected={false}
resourceView={ResourceView.Log}
pathBuilder={PATH_BUILDER}
groupView={false}
/>
</FeaturesProvider>
</FeaturesValueProvider>
)
}

Expand Down
Loading

0 comments on commit 288fba1

Please sign in to comment.