summaryrefslogtreecommitdiff
path: root/src/utils/shelves.ts
diff options
context:
space:
mode:
authorrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
committerrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
commitbce557cc2dc767628bed6aac87301a1be7c5431b (patch)
treeb51a051228d01fe3306cd7626d4a96768aadb944 /src/utils/shelves.ts
init commit
Diffstat (limited to 'src/utils/shelves.ts')
-rw-r--r--src/utils/shelves.ts56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/utils/shelves.ts b/src/utils/shelves.ts
new file mode 100644
index 0000000..e144f4b
--- /dev/null
+++ b/src/utils/shelves.ts
@@ -0,0 +1,56 @@
+import type {
+ ShelfBasedProductPage,
+ Shelf,
+} from '@jet-app/app-store/api/models';
+import { isProductMediaShelf } from '~/components/jet/shelf/ProductMediaShelf.svelte';
+
+type ShelfWithExpandedMedia = Shelf & {
+ expandedMedia?: ShelfWithExpandedMedia[];
+};
+
+export const getProductPageShelvesForOrdering = (
+ page: ShelfBasedProductPage,
+ shelfOrder: string,
+): Shelf[] => {
+ return (
+ page.shelfOrderings[shelfOrder]
+ ?.map((shelfIdentifier) => page.shelfMapping[shelfIdentifier])
+ // The type system doesn't reflect this, but ordering identifier may be provided for
+ // shelves that do not exist. We should probably filter those out
+ .filter((shelf): shelf is Shelf => !!shelf)
+ );
+};
+
+export const getProductPageShelvesWithExpandedMedia = (
+ page: ShelfBasedProductPage,
+): ShelfWithExpandedMedia[] => {
+ const { defaultShelfOrdering = 'notPurchasedOrdering' } = page;
+
+ const shelves = getProductPageShelvesForOrdering(
+ page,
+ defaultShelfOrdering,
+ ) as ShelfWithExpandedMedia[];
+
+ // find the location of the product media of selected platform in shelves
+ const mainMediaShelfIndex = shelves.findIndex((shelf) =>
+ isProductMediaShelf(shelf),
+ );
+
+ let expandedMedia: ShelfWithExpandedMedia[] | undefined;
+
+ if (mainMediaShelfIndex !== -1) {
+ expandedMedia = getProductPageShelvesForOrdering(
+ page,
+ 'notPurchasedOrdering_ExpandedMedia',
+ )
+ .filter((shelf) => isProductMediaShelf(shelf))
+ // filter out the product media shelf of selected platform to avoid duplicate shelves
+ .filter(({ id }) => id !== shelves[mainMediaShelfIndex].id);
+ }
+
+ if (expandedMedia) {
+ shelves[mainMediaShelfIndex].expandedMedia = expandedMedia;
+ }
+
+ return shelves;
+};