Initial commit
This commit is contained in:
commit
459e93d314
100 files changed
+26594
No files matched your search
@@ -0,0 +1,12 @@
|
||||
import { cx } from '../../lib/utils'
|
||||
|
||||
/** Photo de profil ronde, ou initiale sur fond neutre. */
|
||||
export default function Avatar({ url, name, size = 40, className }: { url?: string; name: string; size?: number; className?: string }) {
|
||||
const style = { width: size, height: size }
|
||||
if (url) return <img src={url} alt="" style={style} className={cx('shrink-0 rounded-full border border-line bg-surface-2 object-cover', className)} />
|
||||
return (
|
||||
<span style={{ ...style, fontSize: size * 0.42 }} className={cx('grid shrink-0 place-items-center rounded-full border border-line bg-surface-2 font-bold text-ink-2', className)}>
|
||||
{(name.trim().charAt(0) || '?').toUpperCase()}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
import { Camera, ImagePlus, Loader2, Lock, Trash2 } from 'lucide-react'
|
||||
import { useRef, useState } from 'react'
|
||||
import { t } from '../../i18n'
|
||||
import { deleteProfileImage, updateProfile, uploadProfileImage, USERNAME_RE, type ProfilePatch } from '../../lib/cloud/social'
|
||||
import { cropImageToJpeg } from '../../lib/image'
|
||||
import { cx } from '../../lib/utils'
|
||||
import Avatar from './Avatar'
|
||||
import Sheet from './Sheet'
|
||||
import { useSocial } from './SocialProvider'
|
||||
|
||||
/** Modifier mon profil : photo, bannière, nom, pseudo, bio, compte privé. */
|
||||
export default function EditProfile({ onClose }: { onClose: () => void }) {
|
||||
const { me, refresh } = useSocial()
|
||||
const [displayName, setDisplayName] = useState(me?.displayName ?? '')
|
||||
const [username, setUsername] = useState(me?.username ?? '')
|
||||
const [bio, setBio] = useState(me?.bio ?? '')
|
||||
const [isPrivate, setIsPrivate] = useState(me?.isPrivate ?? false)
|
||||
const [avatar, setAvatar] = useState<{ blob?: Blob; preview?: string; removed?: boolean }>({ preview: me?.avatarUrl })
|
||||
const [banner, setBanner] = useState<{ blob?: Blob; preview?: string; removed?: boolean }>({ preview: me?.bannerUrl })
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [error, setError] = useState<string>()
|
||||
const avatarInput = useRef<HTMLInputElement>(null)
|
||||
const bannerInput = useRef<HTMLInputElement>(null)
|
||||
|
||||
const cleanUsername = username.trim().toLowerCase().replace(/^@/, '')
|
||||
const usernameOk = USERNAME_RE.test(cleanUsername)
|
||||
const nameOk = displayName.trim().length >= 1 && displayName.trim().length <= 40
|
||||
|
||||
const pick = async (kind: 'avatar' | 'banner', file?: File) => {
|
||||
if (!file) return
|
||||
setError(undefined)
|
||||
try {
|
||||
const blob = kind === 'avatar' ? await cropImageToJpeg(file, 400, 400) : await cropImageToJpeg(file, 1500, 500, 0.84)
|
||||
const preview = URL.createObjectURL(blob)
|
||||
if (kind === 'avatar') setAvatar({ blob, preview })
|
||||
else setBanner({ blob, preview })
|
||||
} catch (e) {
|
||||
setError((e as Error).message)
|
||||
}
|
||||
}
|
||||
|
||||
const save = async () => {
|
||||
if (!me || !usernameOk || !nameOk) return
|
||||
setSaving(true)
|
||||
setError(undefined)
|
||||
const uploaded: string[] = []
|
||||
try {
|
||||
const patch: ProfilePatch = {}
|
||||
if (displayName.trim() !== me.displayName) patch.display_name = displayName.trim()
|
||||
if (cleanUsername !== me.username) patch.username = cleanUsername
|
||||
if (bio.trim() !== me.bio) patch.bio = bio.trim()
|
||||
if (isPrivate !== me.isPrivate) patch.is_private = isPrivate
|
||||
if (avatar.blob) {
|
||||
patch.avatar_url = await uploadProfileImage('avatar', avatar.blob)
|
||||
uploaded.push(patch.avatar_url)
|
||||
} else if (avatar.removed) patch.avatar_url = null
|
||||
if (banner.blob) {
|
||||
patch.banner_url = await uploadProfileImage('banner', banner.blob)
|
||||
uploaded.push(patch.banner_url)
|
||||
} else if (banner.removed) patch.banner_url = null
|
||||
if (Object.keys(patch).length) await updateProfile(patch)
|
||||
// Les anciennes images ne servent plus
|
||||
if ('avatar_url' in patch) await deleteProfileImage(me.avatarUrl)
|
||||
if ('banner_url' in patch) await deleteProfileImage(me.bannerUrl)
|
||||
await refresh()
|
||||
onClose()
|
||||
} catch (e) {
|
||||
for (const url of uploaded) await deleteProfileImage(url)
|
||||
setError((e as Error).message)
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
if (!me) {
|
||||
return (
|
||||
<Sheet label={t('social.editProfile')} title={t('social.editProfile')} onClose={onClose}>
|
||||
<p className="px-4 py-12 text-center text-sm text-ink-3">{t('social.loadError')}</p>
|
||||
</Sheet>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Sheet
|
||||
label={t('social.editProfile')}
|
||||
title={t('social.editProfile')}
|
||||
onClose={onClose}
|
||||
right={
|
||||
<button onClick={save} disabled={saving || !usernameOk || !nameOk} className="btn btn-light px-3 py-1.5 text-sm">
|
||||
{saving ? <Loader2 size={15} className="animate-spin" /> : t('common.save')}
|
||||
</button>
|
||||
}
|
||||
>
|
||||
{/* Bannière */}
|
||||
<button type="button" onClick={() => bannerInput.current?.click()} className="relative block aspect-[3/1] w-full overflow-hidden bg-surface-2" aria-label={t('social.changeBanner')}>
|
||||
{banner.preview && !banner.removed && <img src={banner.preview} alt="" className="size-full object-cover" />}
|
||||
<span className="absolute inset-0 grid place-items-center bg-black/25">
|
||||
<span className="flex items-center gap-2 rounded-full bg-bg/85 px-3 py-1.5 text-xs font-semibold">
|
||||
<ImagePlus size={14} /> {t('social.changeBanner')}
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
<input ref={bannerInput} type="file" accept="image/*" hidden onChange={(e) => pick('banner', e.target.files?.[0])} />
|
||||
|
||||
<div className="px-4">
|
||||
<div className="-mt-11 flex items-end gap-3">
|
||||
<button type="button" onClick={() => avatarInput.current?.click()} className="relative rounded-full" aria-label={t('social.changeAvatar')}>
|
||||
<Avatar url={avatar.removed ? undefined : avatar.preview} name={displayName || me.username} size={88} className="border-4 border-bg" />
|
||||
<span className="absolute bottom-1 right-1 grid size-7 place-items-center rounded-full bg-accent-fill text-on-accent">
|
||||
<Camera size={14} />
|
||||
</span>
|
||||
</button>
|
||||
<input ref={avatarInput} type="file" accept="image/*" hidden onChange={(e) => pick('avatar', e.target.files?.[0])} />
|
||||
<div className="mb-1 flex flex-wrap gap-x-4 gap-y-1 text-xs text-ink-3">
|
||||
{avatar.preview && !avatar.removed && (
|
||||
<button type="button" onClick={() => setAvatar({ removed: true })} className="flex items-center gap-1">
|
||||
<Trash2 size={12} /> {t('social.removeAvatar')}
|
||||
</button>
|
||||
)}
|
||||
{banner.preview && !banner.removed && (
|
||||
<button type="button" onClick={() => setBanner({ removed: true })} className="flex items-center gap-1">
|
||||
<Trash2 size={12} /> {t('social.removeBanner')}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 space-y-4">
|
||||
<label className="block">
|
||||
<span className="label">{t('social.displayName')}</span>
|
||||
<input className="field" value={displayName} onChange={(e) => setDisplayName(e.target.value)} maxLength={40} required />
|
||||
</label>
|
||||
<label className="block">
|
||||
<span className="label">{t('social.username')}</span>
|
||||
<div className="relative">
|
||||
<span className="pointer-events-none absolute left-3.5 top-1/2 -translate-y-1/2 text-ink-3">@</span>
|
||||
<input
|
||||
className={cx('field pl-8', username && !usernameOk && 'border-accent!')}
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value.toLowerCase())}
|
||||
maxLength={21}
|
||||
autoCapitalize="off"
|
||||
autoComplete="off"
|
||||
spellCheck={false}
|
||||
/>
|
||||
</div>
|
||||
<span className={cx('mt-1.5 block text-xs', usernameOk ? 'text-ink-3' : 'text-accent')}>{t('social.usernameRule')}</span>
|
||||
</label>
|
||||
<label className="block">
|
||||
<span className="label">{t('social.bio')}</span>
|
||||
<textarea className="field min-h-24 resize-y" value={bio} onChange={(e) => setBio(e.target.value)} maxLength={300} placeholder={t('social.bioPh')} />
|
||||
<span className="mt-1 block text-right text-[11px] text-ink-3">{bio.length}/300</span>
|
||||
</label>
|
||||
|
||||
<button type="button" onClick={() => setIsPrivate((v) => !v)} className="flex w-full items-center gap-3 rounded-2xl border border-line px-4 py-3.5 text-left">
|
||||
<Lock size={18} className="shrink-0 text-ink-2" />
|
||||
<span className="flex-1">
|
||||
<span className="block text-sm font-medium">{t('social.privateAccount')}</span>
|
||||
<span className="mt-0.5 block text-xs text-ink-3">{t('social.privateHint')}</span>
|
||||
</span>
|
||||
<span className={cx('relative h-6 w-11 shrink-0 rounded-full transition-colors', isPrivate ? 'bg-accent-fill' : 'bg-surface-2 border border-line-strong')}>
|
||||
<span className={cx('absolute top-0.5 size-5 rounded-full bg-ink transition-all', isPrivate ? 'left-[22px] bg-on-accent' : 'left-0.5')} />
|
||||
</span>
|
||||
</button>
|
||||
|
||||
{error && (
|
||||
<p role="alert" className="rounded-xl border border-accent px-3 py-2.5 text-sm">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<p className="text-xs leading-relaxed text-ink-3">{t('social.publicHint')}</p>
|
||||
</div>
|
||||
</div>
|
||||
</Sheet>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import { Loader2 } from 'lucide-react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { t } from '../../i18n'
|
||||
import { acceptFollower, getFollowList, removeFollower, type Profile, type ProfileCard } from '../../lib/cloud/social'
|
||||
import { cx } from '../../lib/utils'
|
||||
import PersonRow from './PersonRow'
|
||||
import Sheet from './Sheet'
|
||||
import { useSocial } from './SocialProvider'
|
||||
|
||||
type Tab = 'followers' | 'following' | 'requests'
|
||||
|
||||
/** Abonnés / abonnements d'un profil ; pour le mien, aussi les demandes en attente. */
|
||||
export default function FollowList({ profile, initial, onClose }: { profile: Profile; initial: 'followers' | 'following'; onClose: () => void }) {
|
||||
const social = useSocial()
|
||||
const showRequests = profile.isMe && social.requests.length > 0
|
||||
const [tab, setTab] = useState<Tab>(showRequests && initial === 'followers' ? 'requests' : initial)
|
||||
const [list, setList] = useState<ProfileCard[]>()
|
||||
const [busy, setBusy] = useState<string>()
|
||||
|
||||
useEffect(() => {
|
||||
if (tab === 'requests') return setList(social.requests)
|
||||
let alive = true
|
||||
setList(undefined)
|
||||
getFollowList(profile.id, tab)
|
||||
.then((l) => alive && setList(l))
|
||||
.catch(() => alive && setList([]))
|
||||
return () => {
|
||||
alive = false
|
||||
}
|
||||
}, [tab, profile.id, social.requests])
|
||||
|
||||
const act = async (person: ProfileCard, action: 'accept' | 'remove') => {
|
||||
setBusy(person.id)
|
||||
try {
|
||||
if (action === 'accept') await acceptFollower(person.id)
|
||||
else await removeFollower(person.id)
|
||||
setList((l) => l?.filter((p) => p.id !== person.id))
|
||||
await social.refresh()
|
||||
} finally {
|
||||
setBusy(undefined)
|
||||
}
|
||||
}
|
||||
|
||||
const tabs: Tab[] = showRequests ? ['requests', 'followers', 'following'] : ['followers', 'following']
|
||||
|
||||
return (
|
||||
<Sheet label={profile.displayName} title={profile.displayName} onClose={onClose}>
|
||||
<div className="px-4 pt-4">
|
||||
<div className={cx('grid gap-1 rounded-full border border-line p-1', tabs.length === 3 ? 'grid-cols-3' : 'grid-cols-2')}>
|
||||
{tabs.map((v) => (
|
||||
<button key={v} onClick={() => setTab(v)} className={cx('rounded-full py-2 text-xs font-semibold transition-colors', tab === v ? 'bg-ink text-bg' : 'text-ink-3')}>
|
||||
{v === 'requests'
|
||||
? t('social.requestsTab', { count: social.requests.length })
|
||||
: v === 'followers'
|
||||
? t('social.followers', { count: profile.followers })
|
||||
: t('social.followingCount', { count: profile.following })}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{list === undefined ? (
|
||||
<p className="flex justify-center py-10">
|
||||
<Loader2 size={16} className="animate-spin text-ink-3" />
|
||||
</p>
|
||||
) : list.length === 0 ? (
|
||||
<p className="py-10 text-center text-sm text-ink-3">{tab === 'following' ? t('social.noFollowing') : t('social.noFollowers')}</p>
|
||||
) : (
|
||||
<div className="mt-3 divide-y divide-line">
|
||||
{list.map((p) => (
|
||||
<PersonRow
|
||||
key={p.id}
|
||||
person={p}
|
||||
actions={
|
||||
tab === 'requests' ? (
|
||||
<span className="flex shrink-0 gap-1.5">
|
||||
<button onClick={() => act(p, 'accept')} disabled={!!busy} className="h-8 rounded-full bg-accent-fill px-3 text-xs font-semibold text-on-accent">
|
||||
{t('social.accept')}
|
||||
</button>
|
||||
<button onClick={() => act(p, 'remove')} disabled={!!busy} className="h-8 rounded-full border border-line-strong px-3 text-xs font-semibold text-ink-2">
|
||||
{t('social.decline')}
|
||||
</button>
|
||||
</span>
|
||||
) : profile.isMe && tab === 'followers' ? (
|
||||
<button onClick={() => act(p, 'remove')} disabled={!!busy} className="h-8 shrink-0 rounded-full border border-line-strong px-3 text-xs font-semibold text-ink-2">
|
||||
{t('social.removeFollower')}
|
||||
</button>
|
||||
) : undefined
|
||||
}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Sheet>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
import { Loader2, Star, UserPlus } from 'lucide-react'
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { t } from '../../i18n'
|
||||
import { getFeed, type FeedEntry } from '../../lib/cloud/social'
|
||||
import { timeAgo } from '../../lib/timeAgo'
|
||||
import { formatRating } from '../../lib/utils'
|
||||
import { useMedia } from '../../store'
|
||||
import Poster from '../Poster'
|
||||
import { LinkArrow, SectionTitle } from '../ui'
|
||||
import Avatar from './Avatar'
|
||||
import { useSocial } from './SocialProvider'
|
||||
|
||||
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 { settings } = useMedia()
|
||||
const [entries, setEntries] = useState<FeedEntry[]>()
|
||||
const [more, setMore] = useState(false)
|
||||
const [loadingMore, setLoadingMore] = useState(false)
|
||||
const [shown, setShown] = useState(6)
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
const list = await getFeed()
|
||||
setEntries(list)
|
||||
setMore(list.length >= 30)
|
||||
} catch {
|
||||
setEntries([])
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (social.enabled && navigator.onLine) void load()
|
||||
}, [social.enabled, load])
|
||||
|
||||
if (!social.enabled) return null
|
||||
|
||||
const loadMore = async () => {
|
||||
if (!entries) return
|
||||
if (shown < entries.length) return setShown((n) => n + 10)
|
||||
setLoadingMore(true)
|
||||
try {
|
||||
const next = await getFeed(entries[entries.length - 1]?.createdAt)
|
||||
setEntries([...entries, ...next])
|
||||
setMore(next.length >= 30)
|
||||
setShown((n) => n + 10)
|
||||
} finally {
|
||||
setLoadingMore(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<SectionTitle action={<LinkArrow onClick={social.openSearch}>{t('social.findFriends')}</LinkArrow>}>{t('feed.title')}</SectionTitle>
|
||||
{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-left">
|
||||
<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={`${e.id}-${e.createdAt}`} 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-left">
|
||||
<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, settings.ratingScale)}
|
||||
</span>
|
||||
)}
|
||||
{e.item.notes && e.item.notesPublic && <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>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
import { Check, Loader2, Plus, Star } from 'lucide-react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { t } from '../../i18n'
|
||||
import { genreLabel } from '../../lib/genres'
|
||||
import { cx, formatDate, formatRating, normalizeText } from '../../lib/utils'
|
||||
import { useMedia } from '../../store'
|
||||
import type { MediaInput, MediaItem } from '../../types'
|
||||
import { StatusPill, TypeBadge } from '../Badges'
|
||||
import Poster from '../Poster'
|
||||
import Avatar from './Avatar'
|
||||
|
||||
export interface PeekOwner {
|
||||
username: string
|
||||
displayName: string
|
||||
avatarUrl?: string
|
||||
}
|
||||
|
||||
/** Aperçu d'un titre de quelqu'un d'autre : sa note, son avis public, et ajout à ma liste « À voir ». */
|
||||
export default function ItemPeek({ item, owner, onClose }: { item: MediaItem; owner?: PeekOwner; onClose: () => void }) {
|
||||
const { items, add, settings } = useMedia()
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [added, setAdded] = useState(false)
|
||||
const owned = items.some(
|
||||
(i) => (item.externalId && i.externalId === item.externalId) || normalizeText(i.title) === normalizeText(item.title),
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose()
|
||||
window.addEventListener('keydown', onKey)
|
||||
return () => window.removeEventListener('keydown', onKey)
|
||||
}, [onClose])
|
||||
|
||||
const addToWatchlist = async () => {
|
||||
setBusy(true)
|
||||
try {
|
||||
const input: MediaInput = {
|
||||
title: item.title,
|
||||
originalTitle: item.originalTitle,
|
||||
type: item.type,
|
||||
subtype: item.subtype,
|
||||
year: item.year,
|
||||
status: 'a_voir',
|
||||
criteria: {},
|
||||
episodesWatched: 0,
|
||||
episodesTotal: item.episodesTotal,
|
||||
episodeDuration: item.episodeDuration,
|
||||
duration: item.duration,
|
||||
genres: item.genres,
|
||||
platform: item.platform,
|
||||
poster: item.poster,
|
||||
overview: item.overview,
|
||||
externalId: item.externalId,
|
||||
countries: item.countries,
|
||||
publicRating: item.publicRating,
|
||||
}
|
||||
await add(input)
|
||||
setAdded(true)
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-[60] flex items-end justify-center bg-black/70 sm:items-center sm:p-4" onClick={onClose}>
|
||||
<div
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label={item.title}
|
||||
className="sheet-in safe-bottom max-h-[88dvh] w-full max-w-md overflow-y-auto rounded-t-3xl border border-line-strong bg-surface p-5 sm:rounded-3xl"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div className="flex gap-4">
|
||||
<div className="w-24 shrink-0">
|
||||
<Poster src={item.poster} title={item.title} />
|
||||
</div>
|
||||
<div className="min-w-0 flex-1">
|
||||
<h2 className="text-lg font-bold leading-snug">{item.title}</h2>
|
||||
{item.originalTitle && <p className="truncate text-sm text-ink-3">{item.originalTitle}</p>}
|
||||
<div className="mt-2 flex flex-wrap items-center gap-2 text-xs text-ink-3">
|
||||
<TypeBadge item={item} />
|
||||
{item.year && <span>{item.year}</span>}
|
||||
</div>
|
||||
{item.genres.length > 0 && <p className="mt-2 line-clamp-1 text-xs text-ink-3">{item.genres.slice(0, 3).map(genreLabel).join(' · ')}</p>}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{owner && (
|
||||
<div className="mt-5 rounded-2xl border border-line p-3.5">
|
||||
<div className="flex items-center gap-2.5">
|
||||
<Avatar url={owner.avatarUrl} name={owner.displayName} size={28} />
|
||||
<span className="min-w-0 flex-1 truncate text-sm font-semibold">{owner.displayName}</span>
|
||||
{item.rating != null && (
|
||||
<span className="flex items-center gap-1 text-sm font-semibold">
|
||||
<Star size={14} className="fill-accent text-accent" />
|
||||
{formatRating(item.rating, settings.ratingScale)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="mt-2 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-ink-3">
|
||||
<StatusPill status={item.status} />
|
||||
{item.endDate && item.status === 'termine' && <span>{formatDate(item.endDate)}</span>}
|
||||
{item.favorite && <span className="text-accent">♥ {t('form.favorite')}</span>}
|
||||
</div>
|
||||
{item.notes && <p className="mt-3 whitespace-pre-line text-sm leading-relaxed text-ink-2">{item.notes}</p>}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{item.overview && <p className="mt-4 line-clamp-5 text-sm leading-relaxed text-ink-2">{item.overview}</p>}
|
||||
|
||||
<div className="mt-5 grid grid-cols-2 gap-2">
|
||||
<button onClick={onClose} className="btn btn-ghost">
|
||||
{t('common.close')}
|
||||
</button>
|
||||
<button onClick={addToWatchlist} disabled={busy || added || owned} className={cx('btn', added || owned ? 'btn-ghost' : 'btn-primary')}>
|
||||
{busy ? <Loader2 size={16} className="animate-spin" /> : added || owned ? <Check size={16} /> : <Plus size={16} />}
|
||||
{added ? t('reco.addedShort') : owned ? t('social.inLibrary') : t('social.addToWatch')}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import { Loader2, Search } from 'lucide-react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { t } from '../../i18n'
|
||||
import { profileLink, searchProfiles, type ProfileCard } from '../../lib/cloud/social'
|
||||
import PersonRow from './PersonRow'
|
||||
import Sheet from './Sheet'
|
||||
import { useSocial } from './SocialProvider'
|
||||
|
||||
/** Trouver des amis par pseudo ou par nom. */
|
||||
export default function PeopleSearch({ onClose }: { onClose: () => void }) {
|
||||
const { me } = useSocial()
|
||||
const [q, setQ] = useState('')
|
||||
const [results, setResults] = useState<ProfileCard[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [copied, setCopied] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
const term = q.trim()
|
||||
if (term.length < 2) return setResults([])
|
||||
let alive = true
|
||||
setLoading(true)
|
||||
const timer = setTimeout(() => {
|
||||
searchProfiles(term)
|
||||
.then((r) => alive && setResults(r))
|
||||
.catch(() => alive && setResults([]))
|
||||
.finally(() => alive && setLoading(false))
|
||||
}, 300)
|
||||
return () => {
|
||||
alive = false
|
||||
clearTimeout(timer)
|
||||
}
|
||||
}, [q])
|
||||
|
||||
const invite = async () => {
|
||||
if (!me) return
|
||||
const url = profileLink(me.username)
|
||||
try {
|
||||
if (navigator.share && window.matchMedia?.('(pointer: coarse)').matches) await navigator.share({ title: 'AzuuCine', text: t('social.inviteText'), url })
|
||||
else {
|
||||
await navigator.clipboard.writeText(url)
|
||||
setCopied(true)
|
||||
}
|
||||
} catch {
|
||||
/* annulé */
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Sheet label={t('social.findFriends')} title={t('social.findFriends')} onClose={onClose}>
|
||||
<div className="px-4 pt-4">
|
||||
<div className="relative">
|
||||
<Search size={17} className="pointer-events-none absolute left-3.5 top-1/2 -translate-y-1/2 text-ink-3" />
|
||||
<input
|
||||
type="search"
|
||||
value={q}
|
||||
onChange={(e) => setQ(e.target.value)}
|
||||
placeholder={t('social.searchPh')}
|
||||
className="field pl-10"
|
||||
autoFocus
|
||||
autoCapitalize="off"
|
||||
autoComplete="off"
|
||||
spellCheck={false}
|
||||
enterKeyHint="search"
|
||||
/>
|
||||
{loading && <Loader2 size={17} className="absolute right-3.5 top-1/2 -translate-y-1/2 animate-spin text-ink-3" />}
|
||||
</div>
|
||||
|
||||
{results.length > 0 ? (
|
||||
<div className="mt-3 divide-y divide-line">
|
||||
{results.map((p) => (
|
||||
<PersonRow key={p.id} person={p} />
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
q.trim().length >= 2 && !loading && <p className="py-8 text-center text-sm text-ink-3">{t('social.noResults')}</p>
|
||||
)}
|
||||
|
||||
{me && (
|
||||
<div className="mt-8 rounded-2xl border border-dashed border-line-strong p-4 text-sm">
|
||||
<p className="text-ink-2">{t('social.inviteHint')}</p>
|
||||
<button onClick={invite} className="mt-2 font-medium text-ink">
|
||||
{copied ? t('social.linkCopied') : t('social.shareMyProfile')} <span className="text-accent">→</span>
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Sheet>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import { Check, Loader2, Lock } from 'lucide-react'
|
||||
import { useState, type ReactNode } from 'react'
|
||||
import { t } from '../../i18n'
|
||||
import { follow, unfollow, type ProfileCard } from '../../lib/cloud/social'
|
||||
import { cx } from '../../lib/utils'
|
||||
import Avatar from './Avatar'
|
||||
import { useSocial } from './SocialProvider'
|
||||
|
||||
/** Ligne « personne » : photo, nom, @pseudo, et bouton d'abonnement (ou actions personnalisées). */
|
||||
export default function PersonRow({ person, actions }: { person: ProfileCard; actions?: ReactNode }) {
|
||||
const social = useSocial()
|
||||
const [relation, setRelation] = useState(person.relation)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const isMe = social.me?.id === person.id
|
||||
|
||||
const toggle = async () => {
|
||||
setBusy(true)
|
||||
try {
|
||||
if (relation === 'none') setRelation(await follow(person.id))
|
||||
else {
|
||||
await unfollow(person.id)
|
||||
setRelation('none')
|
||||
}
|
||||
} catch {
|
||||
/* réseau : rien ne change */
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-3 py-2.5">
|
||||
<button onClick={() => social.openProfile(person.username)} className="flex min-w-0 flex-1 items-center gap-3 text-left">
|
||||
<Avatar url={person.avatarUrl} name={person.displayName} size={44} />
|
||||
<span className="min-w-0">
|
||||
<span className="block truncate text-sm font-semibold">{person.displayName}</span>
|
||||
<span className="flex items-center gap-1 text-xs text-ink-3">
|
||||
@{person.username}
|
||||
{person.isPrivate && <Lock size={10} />}
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
{actions ??
|
||||
(!isMe && (
|
||||
<button
|
||||
onClick={toggle}
|
||||
disabled={busy}
|
||||
className={cx(
|
||||
'flex h-8 shrink-0 items-center gap-1 rounded-full px-3 text-xs font-semibold transition active:scale-95',
|
||||
relation === 'none' ? 'bg-accent-fill text-on-accent' : 'border border-line-strong text-ink-2',
|
||||
)}
|
||||
>
|
||||
{busy ? <Loader2 size={13} className="animate-spin" /> : relation === 'accepted' ? <Check size={13} /> : null}
|
||||
{relation === 'accepted' ? t('social.following') : relation === 'pending' ? t('social.requested') : t('social.follow')}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,383 @@
|
||||
import { Check, Loader2, Lock, Share2, Star, UserPlus } from 'lucide-react'
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react'
|
||||
import { 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 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 (
|
||||
<button onClick={onOpen} className="block w-full text-left">
|
||||
<Poster src={item.poster} title={item.title} />
|
||||
<p className="mt-1.5 truncate text-[11px] font-medium text-ink-2">{item.title}</p>
|
||||
<p className="flex h-4 items-center gap-1 text-[11px] text-ink-3">
|
||||
{item.rating != null && (
|
||||
<>
|
||||
<Star size={10} className="fill-accent text-accent" />
|
||||
{formatRating(item.rating, settings.ratingScale)}
|
||||
</>
|
||||
)}
|
||||
{caption && <span className="truncate">{caption}</span>}
|
||||
</p>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
function Stat({ value, label, onClick }: { value?: number; label: string; onClick?: () => void }) {
|
||||
const body = (
|
||||
<>
|
||||
<span className="block text-lg font-bold tabular-nums leading-tight">{value == null ? '—' : value.toLocaleString()}</span>
|
||||
<span className="block text-[11px] text-ink-3">{label}</span>
|
||||
</>
|
||||
)
|
||||
return onClick ? (
|
||||
<button onClick={onClick} className="flex-1 text-center">
|
||||
{body}
|
||||
</button>
|
||||
) : (
|
||||
<div className="flex-1 text-center">{body}</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** Page de profil (la mienne ou celle d'un autre). */
|
||||
export default function ProfileView({ username, onClose }: { username: string; onClose: () => void }) {
|
||||
const social = useSocial()
|
||||
const [profile, setProfile] = useState<Profile | null>()
|
||||
const [error, setError] = useState<string>()
|
||||
const [tab, setTab] = useState<'profile' | 'library'>('profile')
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [confirmUnfollow, setConfirmUnfollow] = useState(false)
|
||||
const [toast, setToast] = useState<string>()
|
||||
|
||||
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()
|
||||
} 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()
|
||||
} catch (e) {
|
||||
setToast((e as Error).message)
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const topByCategory = useMemo(() => {
|
||||
const out: { label: string; items: MediaItem[] }[] = []
|
||||
for (const c of TOP_CATEGORIES) {
|
||||
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])
|
||||
|
||||
return (
|
||||
<Sheet
|
||||
label={t('social.profile')}
|
||||
title={profile ? `@${profile.username}` : ''}
|
||||
onClose={onClose}
|
||||
right={
|
||||
profile ? (
|
||||
<button onClick={share} className="grid size-10 place-items-center rounded-full text-ink-2" aria-label={t('social.shareProfile')}>
|
||||
<Share2 size={18} />
|
||||
</button>
|
||||
) : undefined
|
||||
}
|
||||
>
|
||||
{profile === undefined ? (
|
||||
<p className="flex items-center justify-center gap-2 py-16 text-sm text-ink-3">
|
||||
<Loader2 size={16} className="animate-spin" /> {t('social.loading')}
|
||||
</p>
|
||||
) : profile === null ? (
|
||||
<div className="px-4">
|
||||
<EmptyState title={error ? t('social.loadError') : t('social.notFound')} text={error ?? t('social.notFoundHint', { username })} />
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{/* Bannière + photo */}
|
||||
<div className="relative aspect-[3/1] w-full overflow-hidden bg-surface-2">
|
||||
{profile.bannerUrl && <img src={profile.bannerUrl} alt="" className="size-full object-cover" />}
|
||||
</div>
|
||||
<div className="px-4">
|
||||
<div className="relative -mt-11 flex items-end justify-between gap-3">
|
||||
<Avatar url={profile.avatarUrl} name={profile.displayName} size={88} className="border-4 border-bg" />
|
||||
{profile.isMe ? (
|
||||
<button onClick={social.openEdit} className="btn btn-ghost mb-1 px-4 py-2 text-sm">
|
||||
{t('social.editProfile')}
|
||||
</button>
|
||||
) : (
|
||||
<button
|
||||
onClick={toggleFollow}
|
||||
disabled={busy}
|
||||
className={cx('btn mb-1 px-4 py-2 text-sm', profile.relation === 'none' ? 'btn-primary' : 'btn-ghost')}
|
||||
>
|
||||
{busy ? (
|
||||
<Loader2 size={15} className="animate-spin" />
|
||||
) : profile.relation === 'accepted' ? (
|
||||
<Check size={15} />
|
||||
) : profile.relation === 'none' ? (
|
||||
<UserPlus size={15} />
|
||||
) : null}
|
||||
{profile.relation === 'accepted'
|
||||
? t('social.following')
|
||||
: profile.relation === 'pending'
|
||||
? t('social.requested')
|
||||
: profile.followsMe
|
||||
? t('social.followBack')
|
||||
: t('social.follow')}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<h1 className="mt-3 text-2xl font-bold leading-tight">{profile.displayName}</h1>
|
||||
<p className="mt-0.5 flex items-center gap-1.5 text-sm text-ink-3">
|
||||
@{profile.username}
|
||||
{profile.isPrivate && <Lock size={12} aria-label={t('social.private')} />}
|
||||
{profile.followsMe && !profile.isMe && (
|
||||
<span className="rounded-full bg-surface-2 px-2 py-0.5 text-[10px] font-semibold uppercase tracking-[0.04em] text-ink-2">
|
||||
{t('social.followsYou')}
|
||||
</span>
|
||||
)}
|
||||
</p>
|
||||
{profile.bio && <p className="mt-3 whitespace-pre-line text-sm leading-relaxed text-ink-2">{profile.bio}</p>}
|
||||
|
||||
<div className="mt-5 flex divide-x divide-line rounded-2xl border border-line py-3">
|
||||
<Stat value={profile.visible ? (profile.stats?.finished ?? 0) : undefined} label={t('social.seen')} />
|
||||
<Stat value={profile.followers} label={t('social.followers', { count: profile.followers })} onClick={profile.visible ? () => social.openFollowList(profile, 'followers') : undefined} />
|
||||
<Stat value={profile.following} label={t('social.followingCount', { count: profile.following })} onClick={profile.visible ? () => social.openFollowList(profile, 'following') : undefined} />
|
||||
</div>
|
||||
|
||||
{profile.isMe && social.requests.length > 0 && (
|
||||
<button onClick={() => social.openFollowList(profile, 'followers')} className="mt-3 flex w-full items-center justify-between rounded-2xl border border-accent px-4 py-3 text-sm">
|
||||
<span>{t('social.requests', { count: social.requests.length })}</span>
|
||||
<span className="text-accent">→</span>
|
||||
</button>
|
||||
)}
|
||||
|
||||
{!profile.visible ? (
|
||||
<div className="mt-8 flex flex-col items-center rounded-2xl border border-line px-6 py-10 text-center">
|
||||
<Lock size={26} className="text-ink-3" />
|
||||
<p className="mt-3 font-semibold">{t('social.privateTitle')}</p>
|
||||
<p className="mt-1 text-sm text-ink-3">{t('social.privateText')}</p>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<div className="mt-6 grid grid-cols-2 gap-1 rounded-full border border-line p-1">
|
||||
{(['profile', 'library'] as const).map((v) => (
|
||||
<button
|
||||
key={v}
|
||||
onClick={() => setTab(v)}
|
||||
className={cx('rounded-full py-2 text-sm font-medium transition-colors', tab === v ? 'bg-ink text-bg' : 'text-ink-3')}
|
||||
>
|
||||
{v === 'profile' ? t('social.tabProfile') : t('social.tabLibrary')}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{tab === 'profile' ? (
|
||||
<ProfileTab profile={profile} topByCategory={topByCategory} onOpen={openItem} />
|
||||
) : (
|
||||
<LibraryTab profile={profile} onOpen={openItem} />
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{toast && (
|
||||
<p role="status" className="sheet-in fixed inset-x-4 bottom-8 z-[70] mx-auto max-w-md rounded-2xl border border-line-strong bg-surface-2 p-3.5 text-center text-sm">
|
||||
{toast}
|
||||
</p>
|
||||
)}
|
||||
<ConfirmDialog
|
||||
open={confirmUnfollow}
|
||||
title={profile?.relation === 'pending' ? t('social.cancelRequestTitle') : t('social.unfollowTitle', { name: profile?.displayName ?? '' })}
|
||||
message={profile?.isPrivate && profile.relation === 'accepted' ? t('social.unfollowPrivate') : undefined}
|
||||
confirmLabel={profile?.relation === 'pending' ? t('social.cancelRequest') : t('social.unfollow')}
|
||||
onConfirm={doUnfollow}
|
||||
onCancel={() => setConfirmUnfollow(false)}
|
||||
/>
|
||||
</Sheet>
|
||||
)
|
||||
}
|
||||
|
||||
function ProfileTab({ profile, topByCategory, onOpen }: { profile: Profile; topByCategory: { label: string; items: MediaItem[] }[]; onOpen: (i: MediaItem) => void }) {
|
||||
const s = profile.stats
|
||||
return (
|
||||
<>
|
||||
{topByCategory.map((c) => (
|
||||
<section key={c.label}>
|
||||
<SectionTitle>
|
||||
{t('social.top5')} · {c.label}
|
||||
</SectionTitle>
|
||||
<div className="grid grid-cols-5 gap-2">
|
||||
{c.items.map((item) => (
|
||||
<div key={item.id} className="relative">
|
||||
<PosterTile item={item} onOpen={() => onOpen(item)} />
|
||||
<span className="absolute left-1 top-1 grid size-5 place-items-center rounded-full bg-bg/85 text-[10px] font-bold">{item.top!.rank}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
))}
|
||||
|
||||
<SectionTitle>{t('social.recent')}</SectionTitle>
|
||||
{profile.recent.length ? (
|
||||
<div className="grid grid-cols-4 gap-2.5">
|
||||
{profile.recent.map((item) => (
|
||||
<PosterTile key={item.id} item={item} onOpen={() => onOpen(item)} />
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-sm text-ink-3">{t('social.noRecent')}</p>
|
||||
)}
|
||||
|
||||
{s && (
|
||||
<>
|
||||
<SectionTitle>{t('nav.stats')}</SectionTitle>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
{[
|
||||
[s.finishedYear, t('social.statYear')],
|
||||
[s.films, t('social.statFilms')],
|
||||
[s.series, t('social.statSeries')],
|
||||
[s.episodes, t('home.episodesSeen')],
|
||||
].map(([v, label]) => (
|
||||
<div key={label as string} className="card p-4">
|
||||
<div className="eyebrow">{label}</div>
|
||||
<div className="mt-2 text-2xl font-bold tabular-nums leading-none">{(v as number).toLocaleString()}</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function LibraryTab({ profile, onOpen }: { profile: Profile; onOpen: (i: MediaItem) => void }) {
|
||||
const [status, setStatus] = useState<WatchStatus | ''>('')
|
||||
const [list, setList] = useState<MediaItem[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [done, setDone] = useState(false)
|
||||
|
||||
const loadPage = useCallback(
|
||||
async (reset: boolean) => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const page = await getProfileItems(profile.id, status || null, reset ? 0 : list.length)
|
||||
setList((prev) => (reset ? page : [...prev, ...page]))
|
||||
setDone(page.length < 48)
|
||||
} finally {
|
||||
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 (
|
||||
<div className="mt-5">
|
||||
<div className="no-scrollbar -mx-4 mb-4 flex gap-2 overflow-x-auto px-4">
|
||||
<button onClick={() => setStatus('')} className={cx('chip shrink-0 py-1! text-[13px]', !status && 'chip-on')}>
|
||||
{t('catalog.all')}
|
||||
</button>
|
||||
{STATUSES.map((s) => (
|
||||
<button key={s.value} onClick={() => setStatus(s.value)} className={cx('chip shrink-0 py-1! text-[13px]', status === s.value && 'chip-on')}>
|
||||
{s.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{list.length ? (
|
||||
<div className="grid grid-cols-3 gap-3 sm:grid-cols-4">
|
||||
{list.map((item) => (
|
||||
<PosterTile key={item.id} item={item} onOpen={() => onOpen(item)} />
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
!loading && <p className="py-6 text-center text-sm text-ink-3">{t('social.emptyLibrary')}</p>
|
||||
)}
|
||||
{loading && (
|
||||
<p className="flex items-center justify-center gap-2 py-6 text-sm text-ink-3">
|
||||
<Loader2 size={14} className="animate-spin" />
|
||||
</p>
|
||||
)}
|
||||
{!loading && !done && list.length > 0 && (
|
||||
<button onClick={() => loadPage(false)} className="btn btn-ghost mt-5 w-full">
|
||||
{t('social.loadMore')}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { X } from 'lucide-react'
|
||||
import { useEffect, type ReactNode } from 'react'
|
||||
import { t } from '../../i18n'
|
||||
|
||||
/** Écran plein (profil, recherche…) avec en-tête et bouton fermer. */
|
||||
export default function Sheet({ title, onClose, right, children, label }: { title: ReactNode; onClose: () => void; right?: ReactNode; children: ReactNode; label: string }) {
|
||||
useEffect(() => {
|
||||
const prev = document.body.style.overflow
|
||||
document.body.style.overflow = 'hidden'
|
||||
const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose()
|
||||
window.addEventListener('keydown', onKey)
|
||||
return () => {
|
||||
document.body.style.overflow = prev
|
||||
window.removeEventListener('keydown', onKey)
|
||||
}
|
||||
}, [onClose])
|
||||
return (
|
||||
<div className="sheet-in fixed inset-0 z-50 flex flex-col bg-bg" role="dialog" aria-modal="true" aria-label={label}>
|
||||
<header className="safe-top border-b border-line">
|
||||
<div className="mx-auto flex max-w-2xl items-center gap-2 px-3 py-2.5">
|
||||
<button onClick={onClose} className="grid size-10 place-items-center rounded-full text-ink-2" aria-label={t('common.close')}>
|
||||
<X size={22} />
|
||||
</button>
|
||||
<h2 className="min-w-0 flex-1 truncate text-center text-base font-semibold">{title}</h2>
|
||||
<span className="flex size-10 items-center justify-end">{right}</span>
|
||||
</div>
|
||||
</header>
|
||||
<div className="flex-1 overflow-y-auto overscroll-contain">
|
||||
<div className="safe-bottom mx-auto max-w-2xl pb-16">{children}</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from 'react'
|
||||
import { getFollowRequests, getMyProfile, type Profile, type ProfileCard } from '../../lib/cloud/social'
|
||||
import { useMedia } from '../../store'
|
||||
import type { MediaItem } from '../../types'
|
||||
import EditProfile from './EditProfile'
|
||||
import FollowList from './FollowList'
|
||||
import ItemPeek, { type PeekOwner } from './ItemPeek'
|
||||
import PeopleSearch from './PeopleSearch'
|
||||
import ProfileView from './ProfileView'
|
||||
|
||||
interface SocialApi {
|
||||
/** Réseau disponible (compte connecté) */
|
||||
enabled: boolean
|
||||
me: Profile | null
|
||||
requests: ProfileCard[]
|
||||
refresh: () => Promise<void>
|
||||
openProfile: (username: string) => void
|
||||
openSearch: () => void
|
||||
openEdit: () => void
|
||||
openFollowList: (profile: Profile, kind: 'followers' | 'following') => void
|
||||
peek: (item: MediaItem, owner?: PeekOwner) => void
|
||||
/** Ouvre la fiche si elle est à moi, sinon l'aperçu */
|
||||
openItem: (item: MediaItem, owner?: PeekOwner) => void
|
||||
}
|
||||
|
||||
const SocialContext = createContext<SocialApi>({
|
||||
enabled: false,
|
||||
me: null,
|
||||
requests: [],
|
||||
refresh: async () => {},
|
||||
openProfile: () => {},
|
||||
openSearch: () => {},
|
||||
openEdit: () => {},
|
||||
openFollowList: () => {},
|
||||
peek: () => {},
|
||||
openItem: () => {},
|
||||
})
|
||||
|
||||
export const useSocial = () => useContext(SocialContext)
|
||||
|
||||
type Overlay =
|
||||
| { kind: 'profile'; username: string }
|
||||
| { kind: 'search' }
|
||||
| { kind: 'edit' }
|
||||
| { kind: 'follows'; profile: Profile; list: 'followers' | 'following' }
|
||||
|
||||
/** Profils, recherche et aperçus : affichés par-dessus l'app, empilés (le bouton fermer revient au précédent). */
|
||||
export function SocialProvider({ children, onOpenOwnItem }: { children: ReactNode; onOpenOwnItem: (item: MediaItem) => void }) {
|
||||
const { account, items } = useMedia()
|
||||
const enabled = !!account
|
||||
const [me, setMe] = useState<Profile | null>(null)
|
||||
const [requests, setRequests] = useState<ProfileCard[]>([])
|
||||
const [stack, setStack] = useState<Overlay[]>([])
|
||||
const [peeked, setPeeked] = useState<{ item: MediaItem; owner?: PeekOwner }>()
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
if (!enabled || !navigator.onLine) return
|
||||
try {
|
||||
const [p, r] = await Promise.all([getMyProfile(), getFollowRequests()])
|
||||
setMe(p)
|
||||
setRequests(r)
|
||||
} catch {
|
||||
/* hors-ligne / serveur pas encore à jour : le reste de l'app fonctionne */
|
||||
}
|
||||
}, [enabled])
|
||||
|
||||
useEffect(() => {
|
||||
void refresh()
|
||||
}, [refresh])
|
||||
|
||||
const push = useCallback((o: Overlay) => setStack((s) => [...s.slice(-8), o]), [])
|
||||
const pop = useCallback(() => setStack((s) => s.slice(0, -1)), [])
|
||||
|
||||
// Lien partagé « #/u/pseudo »
|
||||
useEffect(() => {
|
||||
if (!enabled) return
|
||||
const check = () => {
|
||||
const m = location.hash.match(/^#\/u\/([a-z0-9_]{3,20})$/i)
|
||||
if (!m) return
|
||||
history.replaceState(null, '', `${location.pathname}${location.search}#/home`)
|
||||
push({ kind: 'profile', username: m[1].toLowerCase() })
|
||||
}
|
||||
check()
|
||||
window.addEventListener('hashchange', check)
|
||||
return () => window.removeEventListener('hashchange', check)
|
||||
}, [enabled, push])
|
||||
|
||||
const openItem = useCallback(
|
||||
(item: MediaItem, owner?: PeekOwner) => {
|
||||
const mine = !owner || owner.username === me?.username
|
||||
const local = mine ? items.find((i) => i.id === item.id) : undefined
|
||||
if (local) onOpenOwnItem(local)
|
||||
else setPeeked({ item, owner })
|
||||
},
|
||||
[items, me?.username, onOpenOwnItem],
|
||||
)
|
||||
|
||||
const api = useMemo<SocialApi>(
|
||||
() => ({
|
||||
enabled,
|
||||
me,
|
||||
requests,
|
||||
refresh,
|
||||
openProfile: (username) => push({ kind: 'profile', username }),
|
||||
openSearch: () => push({ kind: 'search' }),
|
||||
openEdit: () => push({ kind: 'edit' }),
|
||||
openFollowList: (profile, list) => push({ kind: 'follows', profile, list }),
|
||||
peek: (item, owner) => setPeeked({ item, owner }),
|
||||
openItem,
|
||||
}),
|
||||
[enabled, me, requests, refresh, push, openItem],
|
||||
)
|
||||
|
||||
return (
|
||||
<SocialContext.Provider value={api}>
|
||||
{/* Les écrans sociaux sont placés avant l'app : une fiche ouverte depuis un profil passe au-dessus */}
|
||||
{enabled &&
|
||||
stack.map((o, k) =>
|
||||
o.kind === 'profile' ? (
|
||||
<ProfileView key={k} username={o.username} onClose={pop} />
|
||||
) : o.kind === 'search' ? (
|
||||
<PeopleSearch key={k} onClose={pop} />
|
||||
) : o.kind === 'edit' ? (
|
||||
<EditProfile key={k} onClose={pop} />
|
||||
) : (
|
||||
<FollowList key={k} profile={o.profile} initial={o.list} onClose={pop} />
|
||||
),
|
||||
)}
|
||||
{children}
|
||||
{enabled && peeked && <ItemPeek item={peeked.item} owner={peeked.owner} onClose={() => setPeeked(undefined)} />}
|
||||
</SocialContext.Provider>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user