Files
bobinoo/src/components/social/NotificationsSheet.tsx
T
Claude c66f0f62a6 Notifications d'abonnement (et rien d'autre)
- Cloche sur l'accueil (avec le nombre non lu) : qui s'abonne, demande à s'abonner,
  se désabonne, ou accepte ma demande ; accepter/refuser une demande directement depuis la liste
- Serveur (migration 20261127120000, déjà appliquée) : déclencheur sur follows, une ligne par
  personne et par sens, 200 max par compte ; rien entre comptes bloqués ou suspendus ;
  retirer un abonné, bloquer ou supprimer un compte ne notifie pas
- Fonctions get_notifications / unread_notifications / mark_notifications_read (comptes connectés seulement)
- Textes dans les 18 langues

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

104 lines
4.3 KiB
TypeScript

import { Loader2 } from 'lucide-react'
import { useEffect, useState } from 'react'
import { t, type TKey } from '../../i18n'
import { acceptFollower, getNotifications, markNotificationsRead, removeFollower, type FollowNotification } from '../../lib/cloud/social'
import { timeAgo } from '../../lib/timeAgo'
import { cx } from '../../lib/utils'
import Avatar from './Avatar'
import Sheet from './Sheet'
import { useSocial } from './SocialProvider'
const TEXT: Record<FollowNotification['kind'], TKey> = {
new_follower: 'notif.newFollower',
follow_request: 'notif.followRequest',
unfollow: 'notif.unfollow',
follow_accepted: 'notif.followAccepted',
}
/** Qui s'est abonné, désabonné, a demandé à s'abonner ou a accepté ma demande. Ouvrir la liste la marque comme lue. */
export default function NotificationsSheet({ onClose, onSeen }: { onClose: () => void; onSeen: () => void }) {
const social = useSocial()
const [list, setList] = useState<FollowNotification[]>()
const [error, setError] = useState(false)
const [busy, setBusy] = useState<string>()
useEffect(() => {
let alive = true
getNotifications()
.then((l) => {
if (!alive) return
setList(l)
if (l.some((n) => !n.read)) void markNotificationsRead().then(onSeen, () => {})
else onSeen()
})
.catch(() => alive && setError(true))
return () => {
alive = false
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const pending = new Set(social.requests.map((r) => r.id))
const answer = async (id: string, accept: boolean) => {
setBusy(id)
try {
if (accept) await acceptFollower(id)
else await removeFollower(id)
await social.refresh()
setList(await getNotifications())
} catch {
/* réseau : rien ne change */
} finally {
setBusy(undefined)
}
}
return (
<Sheet title={t('notif.title')} label={t('notif.title')} onClose={onClose}>
<div className="px-4">
{error ? (
<p className="py-10 text-center text-sm text-ink-3">{t('notif.error')}</p>
) : list === undefined ? (
<p className="flex justify-center py-10">
<Loader2 size={16} className="animate-spin text-ink-3" />
</p>
) : list.length === 0 ? (
<p className="mx-auto max-w-xs py-12 text-center text-sm text-ink-3">{t('notif.empty')}</p>
) : (
<ul className="divide-y divide-line">
{list.map((n) => (
<li key={n.id} className="flex items-center gap-3 py-3">
<button onClick={() => social.openProfile(n.actor.username)} className="flex min-w-0 flex-1 items-center gap-3 text-start">
<span className="relative shrink-0">
<Avatar url={n.actor.avatarUrl} name={n.actor.displayName} size={44} />
{!n.read && <span className="absolute -end-0.5 -top-0.5 size-3 rounded-full border-2 border-bg bg-accent-fill" aria-hidden="true" />}
</span>
<span className="min-w-0 flex-1">
<span className={cx('block text-sm leading-snug', n.kind === 'unfollow' ? 'text-ink-2' : 'text-ink')}>
{t(TEXT[n.kind], { name: n.actor.displayName })}
</span>
<span className="mt-0.5 flex gap-2 text-xs text-ink-3">
<span className="truncate">@{n.actor.username}</span>
<span className="shrink-0">{timeAgo(n.createdAt)}</span>
</span>
</span>
</button>
{n.kind === 'follow_request' && pending.has(n.actor.id) && (
<span className="flex shrink-0 gap-1.5">
<button onClick={() => void answer(n.actor.id, true)} disabled={!!busy} className="h-8 rounded-md bg-accent-fill px-3 text-xs font-semibold text-on-accent disabled:opacity-60">
{t('social.accept')}
</button>
<button onClick={() => void answer(n.actor.id, false)} disabled={!!busy} className="h-8 rounded-md border border-line-strong px-3 text-xs font-semibold text-ink-2 disabled:opacity-60">
{t('social.decline')}
</button>
</span>
)}
</li>
))}
</ul>
)}
</div>
</Sheet>
)
}