import { Ban, Check, Loader2, Lock, Share2, Star, UserCheck, UserPlus } from 'lucide-react'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { fmtNumber, t } from '../../i18n'
import { follow, getProfile, getProfileItems, profileLink, unfollow, type Profile } from '../../lib/cloud/social'
import { STATUSES, TOP_CATEGORIES } from '../../lib/constants'
import { cx, formatRating } from '../../lib/utils'
import { useMedia } from '../../store'
import type { MediaItem, WatchStatus } from '../../types'
import ConfirmDialog from '../ConfirmDialog'
import ReportButton from '../moderation/ReportButton'
import { blockUser, unblockUser, useBlocked } from '../../lib/cloud/moderation'
import Poster from '../Poster'
import { EmptyState, SectionTitle } from '../ui'
import Avatar from './Avatar'
import Sheet from './Sheet'
import { useSocial } from './SocialProvider'
function PosterTile({ item, onOpen, caption }: { item: MediaItem; onOpen: () => void; caption?: string }) {
const { settings } = useMedia()
return (
)
}
function Stat({ value, label, onClick }: { value?: number; label: string; onClick?: () => void }) {
const body = (
<>
{value == null ? '—' : fmtNumber(value)}
{label}
>
)
return onClick ? (
) : (
{body}
)
}
/** Page de profil (la mienne ou celle d'un autre). */
export default function ProfileView({ username, onClose }: { username: string; onClose: () => void }) {
const social = useSocial()
const { settings } = useMedia()
const [profile, setProfile] = useState()
const [error, setError] = useState()
const [tab, setTab] = useState<'profile' | 'library'>('profile')
const [busy, setBusy] = useState(false)
const [confirmUnfollow, setConfirmUnfollow] = useState(false)
const [toast, setToast] = useState()
const [confirmBlock, setConfirmBlock] = useState(false)
const { isBlocked } = useBlocked(!!profile && !profile.isMe)
const blocked = !!profile && !profile.isMe && isBlocked(profile.id)
const load = useCallback(async () => {
setError(undefined)
try {
setProfile(await getProfile(username))
} catch (e) {
setError((e as Error).message)
setProfile(null)
}
}, [username])
useEffect(() => {
void load()
}, [load])
// Mon profil a changé (modification) : on recharge
useEffect(() => {
if (profile?.isMe && social.me) setProfile(social.me)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [social.me])
useEffect(() => {
if (!toast) return
const timer = setTimeout(() => setToast(undefined), 2500)
return () => clearTimeout(timer)
}, [toast])
const owner = profile ? { username: profile.username, displayName: profile.displayName, avatarUrl: profile.avatarUrl } : undefined
const openItem = (item: MediaItem) => social.openItem(item, profile?.isMe ? undefined : owner)
const share = async () => {
if (!profile) return
const url = profileLink(profile.username)
try {
if (navigator.share && window.matchMedia?.('(pointer: coarse)').matches) await navigator.share({ title: profile.displayName, url })
else {
await navigator.clipboard.writeText(url)
setToast(t('social.linkCopied'))
}
} catch {
/* partage annulé */
}
}
const toggleFollow = async () => {
if (!profile) return
if (profile.relation !== 'none') return setConfirmUnfollow(true)
setBusy(true)
try {
await follow(profile.id)
await load()
void social.refresh()
} catch (e) {
setToast((e as Error).message)
} finally {
setBusy(false)
}
}
const doUnfollow = async () => {
if (!profile) return
setConfirmUnfollow(false)
setBusy(true)
try {
await unfollow(profile.id)
await load()
void social.refresh()
} catch (e) {
setToast((e as Error).message)
} finally {
setBusy(false)
}
}
// Bloquer : il ne peut plus me suivre ni réagir à mes avis ; ses avis me sont masqués
const doBlock = async () => {
if (!profile) return
setConfirmBlock(false)
setBusy(true)
try {
await blockUser({ id: profile.id, username: profile.username, displayName: profile.displayName, avatarUrl: profile.avatarUrl })
await load()
void social.refresh()
setToast(t('block.done', { name: profile.displayName }))
} catch (e) {
setToast((e as Error).message)
} finally {
setBusy(false)
}
}
const doUnblock = async () => {
if (!profile) return
setBusy(true)
try {
await unblockUser(profile.id)
setToast(t('block.undone', { name: profile.displayName }))
} catch (e) {
setToast((e as Error).message)
} finally {
setBusy(false)
}
}
const topByCategory = useMemo(() => {
const out: { label: string; items: MediaItem[] }[] = []
// Mon profil : mes réglages locaux font foi tout de suite (même avant la synchro)
const shown = profile?.isMe ? TOP_CATEGORIES.filter((c) => settings.topCategories.includes(c.value)) : TOP_CATEGORIES
for (const c of shown) {
const list = (profile?.top ?? []).filter((i) => i.top?.category === c.value).sort((a, b) => a.top!.rank - b.top!.rank)
if (list.length) out.push({ label: c.plural, items: list })
}
return out
}, [profile, settings.topCategories])
return (
) : undefined
}
>
{profile === undefined ? (
{t('social.loading')}
) : profile === null ? (
) : (
<>
{/* Bannière + photo */}
{profile.bannerUrl &&

}
{profile.isMe ? (
) : (
{blocked ? (
) : (
)}
, label: t('block.unblock'), onClick: () => void doUnblock() }
: { icon: , label: t('block.block'), onClick: () => setConfirmBlock(true) },
]}
/>
)}
{profile.displayName}
@{profile.username}
{profile.isPrivate && }
{profile.followsMe && !profile.isMe && (
{t('social.followsYou')}
)}
{profile.bio &&
{profile.bio}
}
{blocked && (
{t('block.notice')}
)}
social.openFollowList(profile, 'followers') : undefined} />
social.openFollowList(profile, 'following') : undefined} />
{profile.isMe && social.requests.length > 0 && (
)}
{!profile.visible ? (
{t('social.privateTitle')}
{t('social.privateText')}
) : (
<>
{(['profile', 'library'] as const).map((v) => (
))}
{tab === 'profile' ? (
) : (
)}
>
)}
>
)}
{toast && (
{toast}
)}
setConfirmBlock(false)}
/>
setConfirmUnfollow(false)}
/>
)
}
function ProfileTab({ profile, topByCategory, onOpen }: { profile: Profile; topByCategory: { label: string; items: MediaItem[] }[]; onOpen: (i: MediaItem) => void }) {
const s = profile.stats
return (
<>
{topByCategory.map((c) => (
{t('social.top5')} · {c.label}
{c.items.map((item) => (
onOpen(item)} />
{item.top!.rank}
))}
))}
{profile.watching.length > 0 && (
<>
{t('social.watching')}
{profile.watching.map((item) => (
onOpen(item)} />
))}
>
)}
{t('social.recent')}
{profile.recent.length ? (
{profile.recent.map((item) => (
onOpen(item)} />
))}
) : (
{t('social.noRecent')}
)}
{profile.watchlist.length > 0 && (
<>
{t('social.watchlist')}
{profile.watchlist.map((item) => (
onOpen(item)} />
))}
>
)}
{s && (
<>
{t('nav.stats')}
{[
[s.finishedYear, t('social.statYear')],
[s.films, t('social.statFilms')],
[s.series, t('social.statSeries')],
[s.episodes, t('home.episodesSeen')],
].map(([v, label]) => (
{label}
{fmtNumber(v as number)}
))}
>
)}
>
)
}
function LibraryTab({ profile, onOpen }: { profile: Profile; onOpen: (i: MediaItem) => void }) {
const [status, setStatus] = useState('')
const [list, setList] = useState([])
const [loading, setLoading] = useState(false)
const [done, setDone] = useState(false)
const request = useRef(0)
const loadPage = useCallback(
async (reset: boolean) => {
// Une réponse arrivée après un changement de filtre ne doit pas écraser la nouvelle liste
const id = ++request.current
setLoading(true)
if (reset) setList([])
try {
const page = await getProfileItems(profile.id, status || null, reset ? 0 : list.length)
if (id !== request.current) return
setList((prev) => (reset ? page : [...prev, ...page]))
setDone(page.length < 48)
} catch {
if (id === request.current) setDone(true)
} finally {
if (id === request.current) setLoading(false)
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[profile.id, status, list.length],
)
useEffect(() => {
void loadPage(true)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [profile.id, status])
return (
{STATUSES.map((s) => (
))}
{list.length ? (
{list.map((item) => (
onOpen(item)} />
))}
) : (
!loading &&
{t('social.emptyLibrary')}
)}
{loading && (
)}
{!loading && !done && list.length > 0 && (
)}
)
}