Skip to content

Commit 05c6390

Browse files
committed
fix(dashboard): memoize rpc chart data
1 parent 21f7987 commit 05c6390

File tree

1 file changed

+42
-31
lines changed

1 file changed

+42
-31
lines changed

apps/dashboard/src/app/team/[team_slug]/[project_slug]/components/RpcMethodBarChartCard/RpcMethodBarChartCardUI.tsx

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ChartTooltipContent,
88
} from "@/components/ui/chart";
99
import { formatTickerNumber } from "lib/format-utils";
10+
import { useMemo } from "react";
1011
import {
1112
Bar,
1213
CartesianGrid,
@@ -20,41 +21,51 @@ import { EmptyStateCard } from "../../../../components/Analytics/EmptyStateCard"
2021
export function RpcMethodBarChartCardUI({
2122
rawData,
2223
}: { rawData: RpcMethodStats[] }) {
23-
const uniqueMethods = Array.from(new Set(rawData.map((d) => d.evmMethod)));
24-
const uniqueDates = Array.from(new Set(rawData.map((d) => d.date)));
24+
const uniqueMethods = useMemo(
25+
() => Array.from(new Set(rawData.map((d) => d.evmMethod))),
26+
[rawData],
27+
);
28+
const uniqueDates = useMemo(
29+
() => Array.from(new Set(rawData.map((d) => d.date))),
30+
[rawData],
31+
);
2532

26-
const data = uniqueDates.map((date) => {
27-
const dateData: { [key: string]: string | number } = { date };
28-
for (const method of uniqueMethods) {
29-
const methodData = rawData.find(
30-
(d) => d.date === date && d.evmMethod === method,
31-
);
32-
dateData[method] = methodData?.count ?? 0;
33-
}
33+
const data = useMemo(
34+
() =>
35+
uniqueDates.map((date) => {
36+
const dateData: { [key: string]: string | number } = { date };
37+
for (const method of uniqueMethods) {
38+
const methodData = rawData.find(
39+
(d) => d.date === date && d.evmMethod === method,
40+
);
41+
dateData[method] = methodData?.count ?? 0;
42+
}
3443

35-
// If we have too many methods to display well, add "other" and group the lowest keys for each time period
36-
if (uniqueMethods.length > 5) {
37-
// If we haven't added "other" as a key yet, add it
38-
if (!uniqueMethods.includes("Other")) {
39-
uniqueMethods.push("Other");
40-
}
44+
// If we have too many methods to display well, add "other" and group the lowest keys for each time period
45+
if (uniqueMethods.length > 5) {
46+
// If we haven't added "other" as a key yet, add it
47+
if (!uniqueMethods.includes("Other")) {
48+
uniqueMethods.push("Other");
49+
}
4150

42-
// Sort the methods by their count for the time period
43-
const sortedMethods = uniqueMethods
44-
.filter((m) => m !== "Other")
45-
.sort(
46-
(a, b) =>
47-
((dateData[b] as number) ?? 0) - ((dateData[a] as number) ?? 0),
48-
);
51+
// Sort the methods by their count for the time period
52+
const sortedMethods = uniqueMethods
53+
.filter((m) => m !== "Other")
54+
.sort(
55+
(a, b) =>
56+
((dateData[b] as number) ?? 0) - ((dateData[a] as number) ?? 0),
57+
);
4958

50-
dateData.Other = 0;
51-
for (const method of sortedMethods.slice(5, sortedMethods.length)) {
52-
dateData.Other += (dateData[method] as number) ?? 0;
53-
delete dateData[method];
54-
}
55-
}
56-
return dateData;
57-
});
59+
dateData.Other = 0;
60+
for (const method of sortedMethods.slice(5, sortedMethods.length)) {
61+
dateData.Other += (dateData[method] as number) ?? 0;
62+
delete dateData[method];
63+
}
64+
}
65+
return dateData;
66+
}),
67+
[uniqueDates, uniqueMethods, rawData],
68+
);
5869

5970
const config: ChartConfig = {};
6071
for (const method of uniqueMethods) {

0 commit comments

Comments
 (0)