31 lines
961 B
TypeScript
31 lines
961 B
TypeScript
import {useCallback, useEffect, useState} from 'react';
|
|
import {
|
|
readWishlistHandlesFromWindow,
|
|
writeWishlistHandles,
|
|
} from '~/lib/abiirWishlistStorage';
|
|
|
|
/**
|
|
* Client-only wishlist keyed by Shopify product handle (matches React `WishlistContext` semantics).
|
|
*/
|
|
export function useAbiirProductWishlist(productHandle: string) {
|
|
const [wishlisted, setWishlisted] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!productHandle) return;
|
|
setWishlisted(readWishlistHandlesFromWindow().includes(productHandle));
|
|
}, [productHandle]);
|
|
|
|
const toggleWishlist = useCallback(() => {
|
|
if (!productHandle) return;
|
|
const current = readWishlistHandlesFromWindow();
|
|
const exists = current.includes(productHandle);
|
|
const next = exists
|
|
? current.filter((h) => h !== productHandle)
|
|
: [...current, productHandle];
|
|
writeWishlistHandles(next);
|
|
setWishlisted(!exists);
|
|
}, [productHandle]);
|
|
|
|
return {wishlisted, toggleWishlist};
|
|
}
|