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

fix(ui): avoid annotation re-renders #1791

Merged
merged 1 commit into from
May 31, 2020
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
1 change: 0 additions & 1 deletion ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ const Alert = ({
value={a.value}
visible={a.visible}
afterUpdate={afterUpdate}
alertStore={alertStore}
/>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import { faExternalLinkAlt } from "@fortawesome/free-solid-svg-icons/faExternalL
import { faSearchPlus } from "@fortawesome/free-solid-svg-icons/faSearchPlus";
import { faSearchMinus } from "@fortawesome/free-solid-svg-icons/faSearchMinus";

import { AlertStore } from "Stores/AlertStore";
import { TooltipWrapper } from "Components/TooltipWrapper";

const RenderNonLinkAnnotation = observer(
({ alertStore, name, value, visible, afterUpdate }) => {
({ name, value, visible, afterUpdate }) => {
const mountRef = useRef(false);

useEffect(() => {
Expand Down Expand Up @@ -74,7 +73,6 @@ const RenderNonLinkAnnotation = observer(
}
);
RenderNonLinkAnnotation.propTypes = {
alertStore: PropTypes.instanceOf(AlertStore).isRequired,
name: PropTypes.string.isRequired,
value: PropTypes.string.isRequired,
visible: PropTypes.bool.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@ import { shallow, mount } from "enzyme";

import toDiffableHtml from "diffable-html";

import { AlertStore } from "Stores/AlertStore";
import { RenderNonLinkAnnotation, RenderLinkAnnotation } from ".";

let alertStore;

beforeEach(() => {
alertStore = new AlertStore([]);
});

const ShallowLinkAnnotation = () => {
return shallow(
<RenderLinkAnnotation name="annotation name" value="http://localhost/foo" />
Expand All @@ -38,7 +31,6 @@ const MockAfterUpdate = jest.fn();
const ShallowNonLinkAnnotation = (visible) => {
return shallow(
<RenderNonLinkAnnotation
alertStore={alertStore}
name="foo"
value="some long text"
visible={visible}
Expand All @@ -50,7 +42,6 @@ const ShallowNonLinkAnnotation = (visible) => {
const MountedNonLinkAnnotation = (visible) => {
return mount(
<RenderNonLinkAnnotation
alertStore={alertStore}
name="foo"
value="some long text"
visible={visible}
Expand All @@ -62,7 +53,6 @@ const MountedNonLinkAnnotation = (visible) => {
const MountedNonLinkAnnotationContainingLink = (visible) => {
return mount(
<RenderNonLinkAnnotation
alertStore={alertStore}
name="foo"
value="some long text with http://example.com link"
visible={visible}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const GroupFooter = ({
value={a.value}
visible={a.visible}
afterUpdate={afterUpdate}
alertStore={alertStore}
/>
))}
</div>
Expand Down
14 changes: 6 additions & 8 deletions ui/src/Components/Grid/AlertGrid/AlertGroup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const AlertGroup = observer(
alertStore: PropTypes.instanceOf(AlertStore).isRequired,
settingsStore: PropTypes.instanceOf(Settings).isRequired,
silenceFormStore: PropTypes.instanceOf(SilenceFormStore).isRequired,
style: PropTypes.object,
groupWidth: PropTypes.number.isRequired,
gridLabelValue: PropTypes.string.isRequired,
};

Expand Down Expand Up @@ -185,7 +185,7 @@ const AlertGroup = observer(
silenceFormStore,
alertStore,
settingsStore,
style,
groupWidth,
gridLabelValue,
} = this.props;

Expand Down Expand Up @@ -221,19 +221,17 @@ const AlertGroup = observer(
}
}

let extraStyle = {};
if (this.renderConfig.isMenuOpen) {
extraStyle.zIndex = 100;
}

return (
<div
className={`components-grid-alertgrid-alertgroup ${
this.renderConfig.animationDone
? "components-animation-fade-appear-done"
: ""
}`}
style={{ ...style, ...extraStyle }}
style={{
width: groupWidth,
zIndex: this.renderConfig.isMenuOpen ? 100 : null,
}}
>
<Fade
in={this.context.animations.in}
Expand Down
1 change: 1 addition & 0 deletions ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const MountedAlertGroup = (afterUpdate, showAlertmanagers) => {
alertStore={alertStore}
silenceFormStore={silenceFormStore}
gridLabelValue=""
groupWidth={420}
/>,
{
wrappingComponent: ThemeContext.Provider,
Expand Down
36 changes: 18 additions & 18 deletions ui/src/Components/Grid/AlertGrid/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const Grid = ({
outerPadding,
}) => {
const { ref, repack } = useGrid(gridSizesConfig);
const debouncedRepack = debounce(repack, 10);
const debouncedRepack = useCallback(debounce(repack, 10), [repack]);

const [groupsToRender, setGroupsToRender] = useState(50);

Expand Down Expand Up @@ -115,23 +115,23 @@ const Grid = ({
}}
>
{isExpanded || grid.labelName === ""
? grid.alertGroups.slice(0, groupsToRender).map((group) => (
<AlertGroup
key={group.id}
group={group}
showAlertmanagers={
Object.keys(alertStore.data.upstreams.clusters).length > 1
}
afterUpdate={debouncedRepack}
alertStore={alertStore}
settingsStore={settingsStore}
silenceFormStore={silenceFormStore}
style={{
width: groupWidth,
}}
gridLabelValue={grid.labelValue}
/>
))
? grid.alertGroups
.slice(0, groupsToRender)
.map((group) => (
<AlertGroup
key={group.id}
group={group}
showAlertmanagers={
Object.keys(alertStore.data.upstreams.clusters).length > 1
}
afterUpdate={debouncedRepack}
alertStore={alertStore}
settingsStore={settingsStore}
silenceFormStore={silenceFormStore}
groupWidth={groupWidth}
gridLabelValue={grid.labelValue}
/>
))
: []}
</div>
{isExpanded && grid.alertGroups.length > groupsToRender && (
Expand Down
12 changes: 6 additions & 6 deletions ui/src/Components/Grid/AlertGrid/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ describe("<AlertGrid />", () => {
groupWidth: wrapper.instance().viewport.groupWidth,
});

expect(tree.find("AlertGroup").at(0).props().style.width).toBe(
expect(tree.find("AlertGroup").at(0).props().groupWidth).toBe(
Math.floor(innerWidth / columns)
);
};
Expand Down Expand Up @@ -419,15 +419,15 @@ describe("<AlertGrid />", () => {
gridSizesConfig: wrapper.instance().viewport.gridSizesConfig,
groupWidth: wrapper.instance().viewport.groupWidth,
});
expect(tree.find("AlertGroup").at(0).props().style.width).toBe(1980 / 4);
expect(tree.find("AlertGroup").at(0).props().groupWidth).toBe(1980 / 4);

// then resize and verify if column count was changed
wrapper.instance().viewport.updateWidths(1000, 1000);
tree.setProps({
gridSizesConfig: wrapper.instance().viewport.gridSizesConfig,
groupWidth: wrapper.instance().viewport.groupWidth,
});
expect(tree.find("AlertGroup").at(0).props().style.width).toBe(1000 / 2);
expect(tree.find("AlertGroup").at(0).props().groupWidth).toBe(1000 / 2);
});

it("scrollbar render doesn't resize alert groups", () => {
Expand All @@ -443,15 +443,15 @@ describe("<AlertGrid />", () => {
gridSizesConfig: wrapper.instance().viewport.gridSizesConfig,
groupWidth: wrapper.instance().viewport.groupWidth,
});
expect(tree.find("AlertGroup").at(0).props().style.width).toBe(400);
expect(tree.find("AlertGroup").at(0).props().groupWidth).toBe(400);

// then resize and verify if column count was changed
wrapper.instance().viewport.updateWidths(1584, 1600);
tree.setProps({
gridSizesConfig: wrapper.instance().viewport.gridSizesConfig,
groupWidth: wrapper.instance().viewport.groupWidth,
});
expect(tree.find("AlertGroup").at(0).props().style.width).toBe(396);
expect(tree.find("AlertGroup").at(0).props().groupWidth).toBe(396);
});

it("window resize event calls updateWidths", () => {
Expand Down Expand Up @@ -480,7 +480,7 @@ describe("<AlertGrid />", () => {
gridSizesConfig: wrapper.instance().viewport.gridSizesConfig,
groupWidth: wrapper.instance().viewport.groupWidth,
});
results.push(tree.find("AlertGroup").at(0).props().style.width);
results.push(tree.find("AlertGroup").at(0).props().groupWidth);
}

expect(results).toStrictEqual([
Expand Down
16 changes: 7 additions & 9 deletions ui/src/Hooks/useGrid.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { useEffect, useRef } from "react";
import { useEffect, useRef, useState } from "react";

import Bricks from "bricks.js";

const useGrid = (sizes) => {
const ref = useRef(null);
const grid = useRef(null);

const repack = () => {
if (grid.current) {
grid.current.pack();
}
};
const [repack, setRepack] = useState(() => () => {});

useEffect(() => {
if (!grid.current && ref.current) {
Expand All @@ -20,12 +15,15 @@ const useGrid = (sizes) => {
packed: "packed",
position: false,
});
window.addEventListener("resize", repack);
window.addEventListener("resize", grid.current.pack);
grid.current.pack();
setRepack(() => () => {
grid.current && grid.current.pack();
});
}

return () => {
window.removeEventListener("resize", repack);
if (grid.current) window.removeEventListener("resize", grid.current.pack);
grid.current = null;
};
}, [sizes]);
Expand Down
9 changes: 8 additions & 1 deletion ui/src/Hooks/useGrid.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";

import { renderHook, act } from "@testing-library/react-hooks";
import { renderHook } from "@testing-library/react-hooks";

import { mount } from "enzyme";

Expand Down Expand Up @@ -57,4 +57,11 @@ describe("useGrid", () => {
const tree = mount(<Component count={4} />);
tree.unmount();
});

it("repack after unmount does nothing", () => {
const tree = mount(<Component count={4} />);
const repack = tree.find("#root").props().onClick;
tree.unmount();
repack();
});
});