Skip to content

Commit 5a38e0a

Browse files
committed
add inventory sort
1 parent e7c0f2e commit 5a38e0a

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

server/utils/inventoryCache.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import { Credentials } from "../types";
22
import { Ecosystem } from "./topiaInit.js";
33
import { standardizeError } from "./standardizeError.js";
4-
import { InventoryItemInterface } from "@rtsdk/topia";
4+
import { InventoryItemInterface as BaseInventoryItemInterface } from "@rtsdk/topia";
5+
6+
// Extend InventoryItemInterface to include metadata with optional sortOrder
7+
interface InventoryItemMetadata {
8+
sortOrder?: number;
9+
[key: string]: any;
10+
}
11+
12+
interface InventoryItemInterface extends BaseInventoryItemInterface {
13+
metadata?: InventoryItemMetadata | null;
14+
}
515

616
interface CachedInventory {
717
items: InventoryItemInterface[];
@@ -44,7 +54,19 @@ export const getCachedInventoryItems = async ({
4454

4555
// Update cache
4656
inventoryCache = {
47-
items: ecosystem.inventoryItems,
57+
items: (ecosystem.inventoryItems as InventoryItemInterface[])
58+
.map((item) => ({
59+
...item,
60+
metadata: {
61+
...(item.metadata || {}),
62+
sortOrder: typeof item.metadata?.sortOrder === "number" ? item.metadata.sortOrder : 0,
63+
},
64+
}))
65+
.sort((a, b) => {
66+
const aOrder = a.metadata?.sortOrder ?? 0;
67+
const bOrder = b.metadata?.sortOrder ?? 0;
68+
return aOrder - bOrder;
69+
}),
4870
timestamp: now,
4971
};
5072

0 commit comments

Comments
 (0)