import {useEffect, useState} from 'react'; import {useNavigate} from 'react-router-dom'; import {useTranslation} from 'react-i18next'; import {ExternalLink, Gift, Percent} from 'lucide-react'; import {Badge} from '@/components/ui/badge'; import {Button} from '@/components/ui/button'; import {Card} from '@/components/ui/card'; import {Skeleton} from '@/components/ui/skeleton'; import GameCover from '../components/GameCover.jsx'; import LibraryTabs from '../components/LibraryTabs.jsx'; import useSessionExpired from '../hooks/useSessionExpired.js'; import PageContainer from '../components/PageContainer.jsx'; const formatAdded = (instant, locale) => instant ? new Date(instant).toLocaleDateString(locale, {year: 'numeric', month: 'short', day: 'numeric'}) : null; export default function WishlistPage() { const {t, i18n} = useTranslation(); const navigate = useNavigate(); const expireSession = useSessionExpired(); const [games, setGames] = useState(null); const [error, setError] = useState(null); const [onlyOnSale, setOnlyOnSale] = useState(false); useEffect(() => { const token = localStorage.getItem('tbp_jwt'); if (!token) { navigate('/login'); return undefined; } let cancelled = false; (async () => { try { const response = await fetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/wishlist`, { headers: {'Authorization': `Bearer ${token}`}, }); if (cancelled) return; if (response.status === 401) { expireSession(); return; } if (!response.ok) { setError(t('WISHLIST.ERROR_LOAD')); return; } const json = await response.json(); if (!cancelled) setGames(json.games ?? []); } catch (err) { console.error('Wishlist load error:', err); if (!cancelled) setError(t('INFO.ERROR.NETWORK_FAILURE')); } })(); return () => { cancelled = true; }; }, [navigate, expireSession, t]); const shown = (games ?? []).filter((g) => !onlyOnSale || g.discountPct > 0); const onSaleCount = (games ?? []).filter((g) => g.discountPct > 0).length; return (

{t('WISHLIST.TITLE')}

{games?.length > 0 && ( )}
{games === null && !error && ( {Array.from({length: 5}, (_, i) => (
))}
)} {error &&
{error}
} {games !== null && shown.length === 0 && (

{games.length === 0 ? t('WISHLIST.EMPTY') : t('WISHLIST.NONE_ON_SALE')}

)} {shown.length > 0 && ( )}
); }