Skip to content

Commit 9d55e51

Browse files
committed
memoize some stuff
1 parent 398db2d commit 9d55e51

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/components/terminal/admin/SelectFlavorMenu.tsx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Box, Typography } from "@mui/material";
2+
import { useMemo } from "react";
23

34
import EmulatorState, {
45
AdminConsoleState,
@@ -51,23 +52,28 @@ const SelectFlavorMenu = (props: { theme?: TerminalTheme; emulatorState: Emulato
5152
);
5253
}
5354

54-
const typeOrder = { currentFlavor: 3, lastBatch: 2, upcoming: 1 };
55-
const sortedInventory = [...mode.inventoryData].sort((a, b) => {
56-
const typeA = typeOrder[a.type as keyof typeof typeOrder] ?? 0;
57-
const typeB = typeOrder[b.type as keyof typeof typeOrder] ?? 0;
55+
const sortedInventory = useMemo(() => {
56+
const typeOrder = { currentFlavor: 3, lastBatch: 2, upcoming: 1 };
57+
return [...(mode.inventoryData ?? [])].sort((a, b) => {
58+
const typeA = typeOrder[a.type as keyof typeof typeOrder] ?? 0;
59+
const typeB = typeOrder[b.type as keyof typeof typeOrder] ?? 0;
5860

59-
if (typeA !== typeB) {
60-
return typeB - typeA;
61-
}
61+
if (typeA !== typeB) {
62+
return typeB - typeA;
63+
}
6264

63-
return b.count - a.count;
64-
});
65+
return b.count - a.count;
66+
});
67+
}, [mode.inventoryData]);
6568

66-
const allItems = sortedInventory;
67-
const totalPages = Math.ceil(allItems.length / ITEMS_PER_PAGE);
68-
const startIndex = currentPage * ITEMS_PER_PAGE;
69-
const endIndex = Math.min(startIndex + ITEMS_PER_PAGE, allItems.length);
70-
const itemsOnPage = allItems.slice(startIndex, endIndex);
69+
const { totalPages, startIndex, itemsOnPage } = useMemo(() => {
70+
const allItems = sortedInventory;
71+
const totalPages = Math.ceil(allItems.length / ITEMS_PER_PAGE);
72+
const startIndex = currentPage * ITEMS_PER_PAGE;
73+
const endIndex = Math.min(startIndex + ITEMS_PER_PAGE, allItems.length);
74+
const itemsOnPage = allItems.slice(startIndex, endIndex);
75+
return { totalPages, startIndex, itemsOnPage };
76+
}, [sortedInventory, currentPage]);
7177

7278
return (
7379
<Box sx={{ paddingTop: 1 }}>

0 commit comments

Comments
 (0)