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 = { 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() const [error, setError] = useState(false) const [busy, setBusy] = useState() 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 (
{error ? (

{t('notif.error')}

) : list === undefined ? (

) : list.length === 0 ? (

{t('notif.empty')}

) : (
    {list.map((n) => (
  • {n.kind === 'follow_request' && pending.has(n.actor.id) && ( )}
  • ))}
)}
) }