import { Loader2, Star, UserPlus } from 'lucide-react' import { useCallback, useEffect, useRef, useState } from 'react' import { t } from '../../i18n' import { FEED_PAGE, getFeed, type FeedEntry } from '../../lib/cloud/social' import { useOnResume } from '../../lib/onResume' import { timeAgo } from '../../lib/timeAgo' import { formatRating } from '../../lib/utils' import Poster from '../Poster' import { LinkArrow, SectionTitle } from '../ui' import Avatar from './Avatar' import { useSocial } from './SocialProvider' const feedKey = (e: FeedEntry) => `${e.id}-${e.createdAt}` /** Ajoute une page sans doublon (même entrée reçue deux fois = même clé React). */ function append(list: FeedEntry[], page: FeedEntry[]): FeedEntry[] { const seen = new Set(list.map(feedKey)) const out = [...list] for (const e of page) { if (seen.has(feedKey(e))) continue seen.add(feedKey(e)) out.push(e) } return out } const VERB = { finished: 'feed.finished', started: 'feed.started', rewatched: 'feed.rewatched', added: 'feed.added' } as const /** Accueil : ce que regardent mes abonnements. */ export default function FriendsFeed() { const social = useSocial() const [entries, setEntries] = useState() const [more, setMore] = useState(false) const [loadingMore, setLoadingMore] = useState(false) const [shown, setShown] = useState(6) // Hors-ligne avant le premier chargement : message au lieu d'un chargement sans fin const [offline, setOffline] = useState(false) // Date de la dernière ligne reçue : point de départ de la page suivante const cursor = useRef(undefined) // Numéro du dernier chargement : une réponse arrivée après un plus récent est ignorée const req = useRef(0) const load = useCallback(async () => { const id = ++req.current setLoadingMore(false) try { const page = await getFeed() if (id !== req.current) return cursor.current = page.cursor setEntries(append([], page.entries)) setMore(page.received >= FEED_PAGE) setOffline(false) } catch { if (id !== req.current) return // Hors-ligne : on garde ce qui est déjà affiché if (!navigator.onLine) { setOffline(true) return } setEntries([]) } }, []) useEffect(() => { if (!social.enabled) return if (navigator.onLine) void load() else setOffline(true) }, [social.enabled, load]) // Réseau retrouvé alors que le fil n'a jamais pu être chargé : on le charge useEffect(() => { if (!social.enabled || !offline) return const on = () => void load() window.addEventListener('online', on) return () => window.removeEventListener('online', on) }, [social.enabled, offline, load]) // Retour sur l'app : on récupère ce que les abonnements ont ajouté entre-temps useOnResume(() => void load(), social.enabled) // Abonnement / désabonnement : le fil change aussi const followKey = `${social.me?.following ?? ''}` useEffect(() => { if (social.enabled && followKey && navigator.onLine) void load() }, [followKey, social.enabled, load]) if (!social.enabled) return null const loadMore = async () => { if (!entries || loadingMore) return if (shown < entries.length) return setShown((n) => n + 10) const id = req.current setLoadingMore(true) try { const page = await getFeed(cursor.current ?? entries[entries.length - 1]?.createdAt) if (id !== req.current) return cursor.current = page.cursor ?? cursor.current setEntries((list) => append(list ?? [], page.entries)) setMore(page.received >= FEED_PAGE) setShown((n) => n + 10) } catch { /* hors-ligne / serveur indisponible : le bouton reste là pour réessayer */ } finally { if (id === req.current) setLoadingMore(false) } } return ( <> {t('social.findFriends')}}>{t('feed.title')} {entries === undefined && offline ? (

{t('common.offline')}

) : entries === undefined ? (

{t('social.loading')}

) : entries.length === 0 ? ( ) : ( <> {(shown < entries.length || more) && ( )} )} ) }