import {useCallback, useEffect, useState} from 'react'; import {Link} from 'react-router'; import {AbiirProductListItem} from '~/components/AbiirProductListItem'; import {CollectionPageView} from '~/components/CollectionPageView'; import {useAbiirWishlistCount} from '~/hooks/useAbiirWishlistCount'; import { ABIIR_WISHLIST_CHANGE_EVENT, readWishlistHandlesFromWindow, } from '~/lib/abiirWishlistStorage'; import { applyDisplayRatingsToListItemData, mapStorefrontProductToAbiirProductListItemData, type StorefrontProductNode, } from '~/lib/featuredProducts'; import {ABIIR_PRODUCT_LIST_GRID_CLASS} from '~/lib/abiirProductListGrid'; import type {DisplayRating} from '~/lib/productDisplayRatings.server'; async function fetchWishlistProducts( handles: string[], ): Promise<{ products: StorefrontProductNode[]; displayRatingsByHandle: Record; }> { if (handles.length === 0) { return {products: [], displayRatingsByHandle: {}}; } const fd = new FormData(); fd.set('handles', JSON.stringify(handles)); const res = await fetch('/api/wishlist-products', {method: 'POST', body: fd}); if (!res.ok) return {products: [], displayRatingsByHandle: {}}; const data = (await res.json()) as { products?: StorefrontProductNode[]; displayRatingsByHandle?: Record; }; return { products: data.products ?? [], displayRatingsByHandle: data.displayRatingsByHandle ?? {}, }; } function WishlistProductCard({ product, index, displayRatingsByHandle, }: { product: StorefrontProductNode; index: number; displayRatingsByHandle: Record; }) { const handle = product.handle?.trim() ?? ''; const item = applyDisplayRatingsToListItemData( mapStorefrontProductToAbiirProductListItemData(product), handle ? displayRatingsByHandle[handle] : undefined, ); return ; } export function WishlistPageContent() { const [products, setProducts] = useState(null); const [displayRatingsByHandle, setDisplayRatingsByHandle] = useState< Record >({}); const [loading, setLoading] = useState(true); const wishlistHandleCount = useAbiirWishlistCount(); const refresh = useCallback(async () => { const handles = readWishlistHandlesFromWindow(); setLoading(true); const {products: next, displayRatingsByHandle: stats} = await fetchWishlistProducts(handles); setProducts(next); setDisplayRatingsByHandle(stats); setLoading(false); }, []); useEffect(() => { void refresh(); window.addEventListener(ABIIR_WISHLIST_CHANGE_EVENT, refresh); window.addEventListener('storage', refresh); return () => { window.removeEventListener(ABIIR_WISHLIST_CHANGE_EVENT, refresh); window.removeEventListener('storage', refresh); }; }, [refresh]); const count = products?.length ?? 0; const loaded = !loading; const hasStoreHandles = wishlistHandleCount > 0; const showEmptyCta = loaded && !hasStoreHandles; const showOrphan = loaded && hasStoreHandles && count === 0; const showGrid = loaded && count > 0; return ( {loading && products === null ?

Loading your list…

: showEmptyCta ?

You haven’t saved any products yet. Browse the shop and tap the heart on a product to add it here.

Shop all products
: showOrphan ?

We couldn’t load those products. They may be unavailable. Try removing them from your list and add items again from the store.

Shop all products
: showGrid ?
    {(products ?? []).map((product, index) => (
  • ))}
:

Loading your list…

}
); }