|
1 | 1 | import { Box, Typography } from "@mui/material"; |
| 2 | +import { useMemo } from "react"; |
2 | 3 |
|
3 | 4 | import EmulatorState, { |
4 | 5 | AdminConsoleState, |
@@ -51,23 +52,28 @@ const SelectFlavorMenu = (props: { theme?: TerminalTheme; emulatorState: Emulato |
51 | 52 | ); |
52 | 53 | } |
53 | 54 |
|
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; |
58 | 60 |
|
59 | | - if (typeA !== typeB) { |
60 | | - return typeB - typeA; |
61 | | - } |
| 61 | + if (typeA !== typeB) { |
| 62 | + return typeB - typeA; |
| 63 | + } |
62 | 64 |
|
63 | | - return b.count - a.count; |
64 | | - }); |
| 65 | + return b.count - a.count; |
| 66 | + }); |
| 67 | + }, [mode.inventoryData]); |
65 | 68 |
|
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]); |
71 | 77 |
|
72 | 78 | return ( |
73 | 79 | <Box sx={{ paddingTop: 1 }}> |
|
0 commit comments