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'; export function PublicProfilePage() { 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 &&
Loading...
} {profile.isError && (

Profile not found

Check the link and try again. Slugs are case-sensitive.

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

{profile.data.displayName}'s wishlist

{profile.data.bio && (

{profile.data.bio}

)}
{wishes.isLoading &&
Loading wishes...
} {wishes.data && wishes.data.length === 0 && (

No wishes yet

Check back later!

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