fix(ui): fix a regression in alert group width calculation

This commit is contained in:
Łukasz Mierzwa
2020-07-07 14:48:42 +01:00
committed by Łukasz Mierzwa
parent 9d43ad4753
commit f23a8457d4
3 changed files with 63 additions and 21 deletions
+32 -21
View File
@@ -1,6 +1,7 @@
import React, { useState } from "react";
import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import { autorun } from "mobx";
import { useObserver } from "mobx-react-lite";
import useDimensions from "react-cool-dimensions";
@@ -8,40 +9,50 @@ import useDimensions from "react-cool-dimensions";
import { AlertStore } from "Stores/AlertStore";
import { Settings } from "Stores/Settings";
import { SilenceFormStore } from "Stores/SilenceFormStore";
import { useWindowSize } from "Hooks/useWindowSize";
import { Grid } from "./Grid";
import { GridSizesConfig, GetGridElementWidth } from "./GridSize";
const AlertGrid = ({ alertStore, settingsStore, silenceFormStore }) => {
const getGridSizesConfig = (windowWidth) =>
GridSizesConfig(windowWidth, settingsStore.gridConfig.config.groupWidth);
const { width: windowWidth } = useWindowSize();
const { ref, width: bodyWidth } = useDimensions();
const getGroupWidth = (canvasWidth, windowWidth) =>
const [gridSizesConfig, setGridSizesConfig] = useState(
GridSizesConfig(windowWidth, settingsStore.gridConfig.config.groupWidth)
);
const [groupWidth, setGroupWidth] = useState(
GetGridElementWidth(
canvasWidth,
bodyWidth || document.body.clientWidth,
windowWidth,
alertStore.data.gridPadding * 2,
settingsStore.gridConfig.config.groupWidth
);
const [gridSizesConfig, setGridSizesConfig] = useState(
getGridSizesConfig(window.innerWidth)
);
const [groupWidth, setGroupWidth] = useState(
getGroupWidth(document.body.clientWidth, window.innerWidth)
)
);
const handleResize = ({ width }) => {
setGridSizesConfig(getGridSizesConfig(window.innerWidth));
setGroupWidth(getGroupWidth(width, window.innerWidth));
};
const { ref } = useDimensions({
onResize: handleResize,
});
useEffect(
() =>
autorun(() => {
setGridSizesConfig(
GridSizesConfig(
windowWidth,
settingsStore.gridConfig.config.groupWidth
)
);
setGroupWidth(
GetGridElementWidth(
bodyWidth || document.body.clientWidth,
windowWidth,
alertStore.data.gridPadding * 2,
settingsStore.gridConfig.config.groupWidth
)
);
}),
[windowWidth, bodyWidth] // eslint-disable-line react-hooks/exhaustive-deps
);
return useObserver(() => (
<React.Fragment>
<div ref={ref}></div>
<div ref={ref} />
{alertStore.data.grids.map((grid) => (
<Grid
key={`${grid.labelName}/${grid.labelValue}`}
@@ -457,6 +457,7 @@ describe("<AlertGrid />", () => {
document.body.clientWidth = 1000;
window.innerWidth = 1000;
act(() => {
window.dispatchEvent(new Event("resize"));
resizeCallback([{ contentRect: { width: 1000, height: 1000 } }]);
});
wrapper.update();
+30
View File
@@ -0,0 +1,30 @@
import { useState, useEffect } from "react";
interface Dimentions {
width: number;
height: number;
}
function getSize(): Dimentions {
return {
width: window.innerWidth,
height: window.innerHeight,
};
}
function useWindowSize() {
const [windowSize, setWindowSize] = useState(getSize());
useEffect(() => {
const handleResize = () => {
setWindowSize(getSize());
};
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}, []);
return windowSize;
}
export { useWindowSize };