From a03146391f3f1d61fcd1ce6de89ac4792355c148 Mon Sep 17 00:00:00 2001 From: Bartosz Prusinowski Date: Tue, 10 Sep 2024 11:59:21 +0200 Subject: [PATCH] refactor: Simplify --- app/components/chart-panel-layout-grid.tsx | 21 ++------------------- app/components/react-grid.tsx | 17 +++++------------ 2 files changed, 7 insertions(+), 31 deletions(-) diff --git a/app/components/chart-panel-layout-grid.tsx b/app/components/chart-panel-layout-grid.tsx index 05e4a07ebc..bd1a3065d6 100644 --- a/app/components/chart-panel-layout-grid.tsx +++ b/app/components/chart-panel-layout-grid.tsx @@ -1,14 +1,11 @@ import clsx from "clsx"; import { fold } from "fp-ts/lib/Either"; import { pipe } from "fp-ts/lib/function"; -import { useEffect, useState } from "react"; +import { useState } from "react"; import { Layouts } from "react-grid-layout"; import { ChartPanelLayoutTypeProps } from "@/components/chart-panel"; -import ChartGridLayout, { - getInitialTileHeight, - getInitialTileWidth, -} from "@/components/react-grid"; +import ChartGridLayout from "@/components/react-grid"; import { ReactGridLayoutsType, hasChartConfigs } from "@/configurator"; import { useConfiguratorState } from "@/src"; import { assert } from "@/utils/assert"; @@ -66,19 +63,6 @@ const ChartPanelLayoutCanvas = (props: ChartPanelLayoutTypeProps) => { }); setLayouts(layouts); }; - const [initializeMap, setInitializeMap] = useState( - new Map() - ); - useEffect(() => { - const newMap = new Map(); - layouts["lg"].forEach(({ i, w, h }) => { - newMap.set( - i, - w === getInitialTileWidth() && h === getInitialTileHeight() - ); - }); - setInitializeMap(newMap); - }, [layouts]); return ( { resize={state.state === "LAYOUTING"} draggableHandle={`.${chartPanelLayoutGridClasses.dragHandle}`} onLayoutChange={(_l, allLayouts) => handleChangeLayouts(allLayouts)} - initializeMap={initializeMap} > {chartConfigs.map((chartConfig) => props.renderChart(chartConfig))} diff --git a/app/components/react-grid.tsx b/app/components/react-grid.tsx index 092f26a965..3d651c933b 100644 --- a/app/components/react-grid.tsx +++ b/app/components/react-grid.tsx @@ -1,6 +1,7 @@ import { Theme } from "@mui/material/styles"; import { makeStyles } from "@mui/styles"; import clsx from "clsx"; +import isEqual from "lodash/isEqual"; import map from "lodash/map"; import mapValues from "lodash/mapValues"; import range from "lodash/range"; @@ -202,13 +203,11 @@ export const ChartGridLayout = ({ className, layouts, resize, - initializeMap, ...rest }: { className: string; onLayoutChange: Function; resize?: boolean; - initializeMap: Map; } & ComponentProps) => { const classes = useStyles(); const [mounted, setMounted] = useState(false); @@ -238,19 +237,12 @@ export const ChartGridLayout = ({ }, []); useEffect(() => { - if ( - !mountedForSomeTime || - [...initializeMap.values()].every((v) => v === false) - ) { + if (!mountedForSomeTime) { return; } const newLayouts = mapValues(enhancedLayouts, (chartLayouts) => { return chartLayouts.map((chartLayout) => { - if (!initializeMap.get(chartLayout.i)) { - return chartLayout; - } - let minH = MIN_H; const chartKey = chartLayout.i; @@ -283,10 +275,11 @@ export const ChartGridLayout = ({ }); }); - setEnhancedLayouts(newLayouts); + if (!isEqual(newLayouts, enhancedLayouts)) { + setEnhancedLayouts(newLayouts); + } }, [ chartContainerClasses.chartContainer, - initializeMap, enhancedLayouts, mountedForSomeTime, resize,