import { useEffect, useState } from 'react' import { useParams } from 'react-router-dom' import { Users, Headphones } from 'lucide-react' import { supabase } from '@/lib/supabase' import { useAuthStore } from '@/stores/auth' import type { Profile as ProfileType, Podcast } from '@/types' import { Avatar } from '@/components/ui/Avatar' import { Button } from '@/components/ui/Button' import { PodcastCard } from '@/components/podcast/PodcastCard' export function Profile() { const { username } = useParams<{ username: string }>() const { user } = useAuthStore() const [profile, setProfile] = useState(null) const [podcasts, setPodcasts] = useState([]) const [isFollowing, setIsFollowing] = useState(false) const [followersCount, setFollowersCount] = useState(0) const [followingCount, setFollowingCount] = useState(0) const [loading, setLoading] = useState(true) const isOwn = user && profile && user.id === profile.id useEffect(() => { if (!username) return async function load() { const { data: profileData } = await supabase .from('profiles') .select('*') .eq('username', username) .single() if (!profileData) { setLoading(false); return } setProfile(profileData) const [podcastsRes, followersRes, followingRes] = await Promise.all([ supabase .from('podcasts') .select('*, creator:profiles(*), tags:podcast_tags(tag:tags(*))') .eq('creator_id', profileData.id) .order('created_at', { ascending: false }), supabase.from('follows').select('*', { count: 'exact', head: true }).eq('following_id', profileData.id), supabase.from('follows').select('*', { count: 'exact', head: true }).eq('follower_id', profileData.id), ]) if (podcastsRes.data) { setPodcasts(podcastsRes.data.map((p: any) => ({ ...p, tags: p.tags?.map((t: any) => t.tag).filter(Boolean) || [], }))) } setFollowersCount(followersRes.count || 0) setFollowingCount(followingRes.count || 0) if (user) { const { data: follow } = await supabase .from('follows') .select('*') .eq('follower_id', user.id) .eq('following_id', profileData.id) .maybeSingle() setIsFollowing(!!follow) } setLoading(false) } load() }, [username, user]) async function handleFollow() { if (!user || !profile) return if (isFollowing) { await supabase.from('follows').delete().eq('follower_id', user.id).eq('following_id', profile.id) setIsFollowing(false) setFollowersCount((c) => c - 1) } else { await supabase.from('follows').insert({ follower_id: user.id, following_id: profile.id }) setIsFollowing(true) setFollowersCount((c) => c + 1) } } if (loading) { return (
) } if (!profile) { return
Utilisateur introuvable.
} return (

{profile.username}

{profile.is_premium && ( PRO )}
{profile.bio &&

{profile.bio}

}
{podcasts.length} podcasts {followersCount} abonnés {followingCount} abonnements
{!isOwn && user && (
)}

Podcasts

{podcasts.length === 0 ? (

Aucun podcast publié.

) : (
{podcasts.map((p) => ( ))}
)}
) }