From fa0b5685cf8c326d4a33533c2b389971c7696afa Mon Sep 17 00:00:00 2001 From: atrincas Date: Thu, 19 Dec 2024 10:30:40 +0100 Subject: [PATCH] Memoize maximums --- .../overview/table/view/overview/index.tsx | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/client/src/containers/overview/table/view/overview/index.tsx b/client/src/containers/overview/table/view/overview/index.tsx index 15abfdc3..6f3beec4 100644 --- a/client/src/containers/overview/table/view/overview/index.tsx +++ b/client/src/containers/overview/table/view/overview/index.tsx @@ -1,6 +1,6 @@ "use client"; -import { useState } from "react"; +import { useState, useMemo } from "react"; import { ChevronDownIcon, ChevronUpIcon } from "@radix-ui/react-icons"; import { projectsQuerySchema } from "@shared/contracts/projects.contract"; @@ -113,6 +113,25 @@ export function OverviewTable() { }, ); + const maximums = useMemo( + () => ({ + maxAbatementPotential: isSuccess + ? Math.max(...data.data.map((item) => item.abatementPotential ?? 0)) + : 0, + maxTotalCost: isSuccess + ? { + [COST_TYPE_SELECTOR.NPV]: Math.max( + ...data.data.map((item) => item.totalCostNPV ?? 0), + ), + [COST_TYPE_SELECTOR.TOTAL]: Math.max( + ...data.data.map((item) => item.totalCost ?? 0), + ), + } + : 0, + }), + [isSuccess, data?.data], + ); + const table = useReactTable({ data: isSuccess ? data.data : NO_DATA, columns: columnsBasedOnFilters, @@ -121,21 +140,7 @@ export function OverviewTable() { state: { sorting, pagination, - maximums: { - maxAbatementPotential: isSuccess - ? Math.max(...data.data.map((item) => item.abatementPotential ?? 0)) - : 0, - maxTotalCost: isSuccess - ? { - [COST_TYPE_SELECTOR.NPV]: Math.max( - ...data.data.map((item) => item.totalCostNPV ?? 0), - ), - [COST_TYPE_SELECTOR.TOTAL]: Math.max( - ...data.data.map((item) => item.totalCost ?? 0), - ), - } - : 0, - }, + maximums, } as TableStateWithMaximums, onSortingChange: setSorting, onPaginationChange: setPagination,