Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web: memoize resource selection provider #5530

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion web/src/ResourceSelectionContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from "@testing-library/react"
import { act, render, screen } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import React, { ChangeEvent, useState } from "react"
import {
Expand Down Expand Up @@ -157,4 +157,33 @@ describe("ResourceSelectionContext", () => {
expect(selectedState()).toBe(JSON.stringify([]))
})
})

it("memoizes renders", () => {
let renderCount = 0
let selection: any
let FakeEl = React.memo(() => {
selection = useResourceSelection()
renderCount++
return <div></div>
})

let tree = () => {
return (
<ResourceSelectionProvider initialValuesForTesting={INITIAL_SELECTIONS}>
<FakeEl />
</ResourceSelectionProvider>
)
}

let { rerender } = render(tree())

expect(renderCount).toEqual(1)
rerender(tree())

// Make sure we don't re-render on a no-op update.
expect(renderCount).toEqual(1)

act(() => selection.clearSelections())
expect(renderCount).toEqual(2)
})
})
57 changes: 32 additions & 25 deletions web/src/ResourceSelectionContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { createContext, PropsWithChildren, useContext, useState } from "react"
import {
createContext,
PropsWithChildren,
useContext,
useMemo,
useState,
} from "react"

/**
* The ResourceSelection state keeps track of what resources are selected for bulk actions to be performed on them.
Expand Down Expand Up @@ -41,33 +47,34 @@ export function ResourceSelectionProvider(
const selections = new Set(props.initialValuesForTesting) || new Set()
const [selectedResources, setSelectedResources] = useState(selections)

function isSelected(resourceName: string) {
return selectedResources.has(resourceName)
}
const contextValue: ResourceSelectionContext = useMemo(() => {
function isSelected(resourceName: string) {
return selectedResources.has(resourceName)
}

function select(...resourceNames: string[]) {
const newSelections = new Set<string>(selectedResources)
resourceNames.forEach((name) => newSelections.add(name))
return setSelectedResources(newSelections)
}
function select(...resourceNames: string[]) {
const newSelections = new Set<string>(selectedResources)
resourceNames.forEach((name) => newSelections.add(name))
return setSelectedResources(newSelections)
}

function deselect(...resourceNames: string[]) {
const newSelections = new Set<string>(selectedResources)
resourceNames.forEach((name) => newSelections.delete(name))
return setSelectedResources(newSelections)
}
function deselect(...resourceNames: string[]) {
const newSelections = new Set<string>(selectedResources)
resourceNames.forEach((name) => newSelections.delete(name))
return setSelectedResources(newSelections)
}

function clearSelections() {
setSelectedResources(new Set())
}

const contextValue: ResourceSelectionContext = {
selected: selectedResources,
isSelected,
select,
deselect,
clearSelections,
}
function clearSelections() {
setSelectedResources(new Set())
}
return {
selected: selectedResources,
isSelected,
select,
deselect,
clearSelections,
}
}, [selectedResources, setSelectedResources])

return (
<ResourceSelectionContext.Provider value={contextValue}>
Expand Down