import { Button } from '../ui/button'; import { Card } from '../ui/card'; import { EmptyState } from '../ui/empty-state'; import { Skeleton } from '../ui/skeleton'; import { useCartStore } from '../../stores/cartStore'; import { Product } from '../../types'; import { Heart, Pause, Play, ShoppingCart, Trash2, Zap } from 'lucide-react'; import React, { useState } from 'react'; import { motion } from 'framer-motion'; import { useToast } from '../../components/feedback/ToastProvider'; import { useAuthStore } from '@/features/auth/store/authStore'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { marketplaceService } from '@/services/marketplaceService'; const gridVariants = { visible: { transition: { staggerChildren: 0.06, delayChildren: 0.04 } }, }; const cardVariants = { hidden: { opacity: 0, y: 16, scale: 0.97 }, visible: { opacity: 1, y: 0, scale: 1, transition: { duration: 0.35, ease: [0.33, 1, 0.68, 1] as const }, }, }; const WISHLIST_QUERY_KEY = ['wishlist']; export const WishlistView: React.FC = () => { const addToCart = useCartStore((state) => state.addItem); const { addToast } = useToast(); const queryClient = useQueryClient(); const isAuthenticated = useAuthStore((s) => s.isAuthenticated); const [playingPreview, setPlayingPreview] = useState(null); const { data: wishlist = [], isLoading, isError, error, } = useQuery({ queryKey: WISHLIST_QUERY_KEY, queryFn: () => marketplaceService.getWishlist(), enabled: isAuthenticated, }); const removeMutation = useMutation({ mutationFn: (productId: string) => marketplaceService.removeFromWishlist(productId), onSuccess: () => { queryClient.invalidateQueries({ queryKey: WISHLIST_QUERY_KEY }); addToast('Removed from wishlist', 'info'); }, onError: (err: Error) => { addToast(err.message || 'Failed to remove from wishlist', 'error'); }, }); const handleRemove = (id: string) => { if (!isAuthenticated) return; removeMutation.mutate(id); }; const handleAddToCart = (product: Product) => { addToCart(product); handleRemove(product.id); }; const handleAddAll = () => { wishlist.forEach((p) => addToCart(p)); wishlist.forEach((p) => handleRemove(p.id)); addToast('All items moved to cart', 'success'); }; if (!isAuthenticated) { return ( } title="Sign in to view your wishlist" description="Create an account or log in to save items you love." action={{ label: 'Sign in', onClick: () => (window.location.href = '/login'), }} size="lg" className="min-h-96" /> ); } if (isError) { return ( } title="Could not load wishlist" description={error instanceof Error ? error.message : 'Something went wrong.'} action={{ label: 'Try again', onClick: () => queryClient.invalidateQueries({ queryKey: WISHLIST_QUERY_KEY }), }} size="lg" className="min-h-96" /> ); } if (isLoading) { return (
{[1, 2, 3].map((i) => ( ))}
); } if (wishlist.length === 0) { return ( } title="Your wishlist is empty" description="Explore the marketplace and save items you love." action={{ label: 'Browse Marketplace', onClick: () => (window.location.href = '/marketplace'), }} size="lg" className="min-h-96" /> ); } return (

WISHLIST

{wishlist.length} saved items

{wishlist.map((product) => (
setPlayingPreview( playingPreview === product.id ? null : product.id, ) } > {playingPreview === product.id ? ( ) : ( )}
{product.isHot && (
HOT
)}

{product.title}

{product.author}

{product.type}

${product.price}
))}
); };