import { useEffect, useRef } from 'react'; import { useParams } from 'react-router-dom'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import type { PublicProfile, Wish, } from '@family-wishlist/shared'; import { api } from '@/lib/api'; import { WishCard } from '@/components/WishCard/WishCard'; import { Footer } from '@/components/Layout/Footer'; import { Gift } from 'lucide-react'; import { LanguageSwitcher } from '@/components/LanguageSwitcher'; import { useI18n } from '@/i18n/i18n'; export function PublicProfilePage() { const { t } = useI18n(); const { slug = '' } = useParams<{ slug: string }>(); const queryClient = useQueryClient(); const profile = useQuery({ queryKey: ['public-profile', slug], queryFn: () => api.get(`/api/public/${encodeURIComponent(slug)}`), retry: false, enabled: slug.length > 0, }); const wishes = useQuery({ queryKey: ['public-wishes', slug], queryFn: () => api.get(`/api/public/${encodeURIComponent(slug)}/wishes`), enabled: slug.length > 0 && profile.isSuccess, }); const markSeen = useMutation({ mutationFn: (wishIds: string[]) => api.post(`/api/public/${encodeURIComponent(slug)}/views`, { wishIds }), }); const marked = useRef(false); useEffect(() => { if (!wishes.data || marked.current) return; const newIds = wishes.data.filter((w) => w.isNewForGuest).map((w) => w.id); if (newIds.length === 0) { marked.current = true; return; } const t = window.setTimeout(() => { markSeen.mutate(newIds, { onSuccess: () => { marked.current = true; void queryClient.invalidateQueries({ queryKey: ['public-wishes', slug] }); }, }); }, 1500); return () => window.clearTimeout(t); }, [wishes.data, markSeen, queryClient, slug]); return (
{profile.isLoading &&
{t('common.loading')}
} {profile.isError && (

{t('public.notFoundTitle')}

{t('public.notFoundText')}

)} {profile.data && ( <>
{profile.data.avatarUrl ? ( ) : ( )}

{t('public.wishlistTitle', { name: profile.data.displayName })}

{profile.data.bio && (

{profile.data.bio}

)}
{wishes.isLoading &&
{t('public.loadingWishes')}
} {wishes.data && wishes.data.length === 0 && (

{t('public.emptyTitle')}

{t('public.emptyText')}

)} {wishes.data && wishes.data.length > 0 && (
{wishes.data.map((wish) => ( ))}
)} )}
); }