Files
bobinoo/src/components/social/FriendsFeed.tsx
T
Claude e366f38727 Nettoyage : variables inutilisées, textes inutilisés, fichiers parasites
- TypeScript refuse désormais les variables et paramètres inutilisés (noUnusedLocals/Parameters)
- 9 lectures de réglages devenues inutiles (échelle de notes) retirées
- 2 textes plus utilisés retirés des 18 langues
- Dossier « Claude outputs » (copie d'une ancienne migration) et tsconfig.tsbuildinfo retirés du dépôt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
2026-11-27 16:57:30 +00:00

167 lines
6.9 KiB
TypeScript

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<FeedEntry[]>()
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<string | undefined>(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 (
<>
<SectionTitle action={<LinkArrow onClick={social.openSearch}>{t('social.findFriends')}</LinkArrow>}>{t('feed.title')}</SectionTitle>
{entries === undefined && offline ? (
<p className="text-sm text-ink-3">{t('common.offline')}</p>
) : entries === undefined ? (
<p className="flex items-center gap-2 text-sm text-ink-3">
<Loader2 size={14} className="animate-spin" /> {t('social.loading')}
</p>
) : entries.length === 0 ? (
<button onClick={social.openSearch} className="flex w-full items-center gap-3 rounded-2xl border border-dashed border-line-strong p-4 text-start">
<span className="grid size-10 shrink-0 place-items-center rounded-full bg-surface-2 text-accent">
<UserPlus size={18} />
</span>
<span className="text-sm text-ink-2">{t('feed.empty')}</span>
</button>
) : (
<>
<ul className="divide-y divide-line">
{entries.slice(0, shown).map((e) => (
<li key={feedKey(e)} className="flex items-center gap-3 py-2.5">
<button onClick={() => social.openProfile(e.user.username)} aria-label={e.user.displayName}>
<Avatar url={e.user.avatarUrl} name={e.user.displayName} size={36} />
</button>
<button onClick={() => social.openItem(e.item, e.user)} className="flex min-w-0 flex-1 items-center gap-3 text-start">
<span className="min-w-0 flex-1">
<span className="line-clamp-2 text-sm leading-snug text-ink-2">
<span className="font-semibold text-ink">{e.user.displayName}</span> {t(VERB[e.kind])}{' '}
<span className="font-semibold text-ink">{e.item.title}</span>
</span>
<span className="mt-0.5 flex items-center gap-2 text-[11px] text-ink-3">
{e.item.rating != null && e.kind !== 'added' && (
<span className="flex items-center gap-0.5 font-semibold text-ink-2">
<Star size={10} className="fill-accent text-accent" />
{formatRating(e.item.rating)}
</span>
)}
{e.item.notes && <span>{t('feed.review')}</span>}
<span>{timeAgo(e.createdAt)}</span>
</span>
</span>
<span className="w-10 shrink-0">
<Poster src={e.item.poster} title={e.item.title} />
</span>
</button>
</li>
))}
</ul>
{(shown < entries.length || more) && (
<button onClick={loadMore} disabled={loadingMore} className="mt-2 w-full py-2 text-xs font-medium text-ink-3">
{loadingMore ? <Loader2 size={13} className="mx-auto animate-spin" /> : (
<>
{t('social.loadMore')} <span className="text-accent">→</span>
</>
)}
</button>
)}
</>
)}
</>
)
}