Skip to content

Commit

Permalink
Memoize maximums
Browse files Browse the repository at this point in the history
  • Loading branch information
atrincas authored and andresgnlez committed Dec 19, 2024
1 parent 64afd1e commit fa0b568
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions client/src/containers/overview/table/view/overview/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<PaginatedProjectsWithMaximums["data"][0]>({
data: isSuccess ? data.data : NO_DATA,
columns: columnsBasedOnFilters,
Expand All @@ -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,
Expand Down

0 comments on commit fa0b568

Please sign in to comment.