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
This commit is contained in:
23 files changed
+444
-3
No files matched your search
@@ -0,0 +1,103 @@
|
||||
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>
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from 'react'
|
||||
import { getFollowRequests, getMyProfile, type Profile, type ProfileCard } from '../../lib/cloud/social'
|
||||
import { getFollowRequests, getMyProfile, unreadNotifications, type Profile, type ProfileCard } from '../../lib/cloud/social'
|
||||
import NotificationsSheet from './NotificationsSheet'
|
||||
import { useMedia } from '../../store'
|
||||
import type { MediaItem } from '../../types'
|
||||
import EditProfile from './EditProfile'
|
||||
@@ -20,6 +21,9 @@ interface SocialApi {
|
||||
enabled: boolean
|
||||
me: Profile | null
|
||||
requests: ProfileCard[]
|
||||
/** Notifications d'abonnement non lues */
|
||||
unread: number
|
||||
openNotifications: () => void
|
||||
refresh: () => Promise<void>
|
||||
openProfile: (username: string) => void
|
||||
openSearch: () => void
|
||||
@@ -44,6 +48,8 @@ const SocialContext = createContext<SocialApi>({
|
||||
enabled: false,
|
||||
me: null,
|
||||
requests: [],
|
||||
unread: 0,
|
||||
openNotifications: () => {},
|
||||
refresh: async () => {},
|
||||
openProfile: () => {},
|
||||
openSearch: () => {},
|
||||
@@ -70,6 +76,7 @@ type Overlay =
|
||||
| { kind: 'reviews'; externalId: string; title: string }
|
||||
| { kind: 'sharedList'; id: string }
|
||||
| { kind: 'invite'; listId: string; listName: string }
|
||||
| { kind: 'notifications' }
|
||||
|
||||
/** Profils, recherche et aperçus : affichés par-dessus l'app, empilés (le bouton fermer revient au précédent). */
|
||||
export function SocialProvider({
|
||||
@@ -86,6 +93,7 @@ export function SocialProvider({
|
||||
const enabled = !!account
|
||||
const [me, setMe] = useState<Profile | null>(null)
|
||||
const [requests, setRequests] = useState<ProfileCard[]>([])
|
||||
const [unread, setUnread] = useState(0)
|
||||
const [stack, setStack] = useState<(Overlay & { uid: number })[]>([])
|
||||
const [peeked, setPeeked] = useState<{ item: MediaItem; owner?: PeekOwner }>()
|
||||
const [sharedTick, setSharedTick] = useState(0)
|
||||
@@ -116,6 +124,8 @@ export function SocialProvider({
|
||||
} catch {
|
||||
/* hors-ligne / serveur pas encore à jour : le reste de l'app fonctionne */
|
||||
}
|
||||
// À part : un serveur sans notifications ne doit pas empêcher le reste
|
||||
unreadNotifications().then(setUnread, () => {})
|
||||
}, [enabled])
|
||||
|
||||
useEffect(() => {
|
||||
@@ -123,6 +133,14 @@ export function SocialProvider({
|
||||
}, [refresh])
|
||||
// Nouvelles demandes d'abonnement, compteurs… mis à jour au retour sur l'app
|
||||
useOnResume(() => void refresh(), enabled)
|
||||
// Pendant que l'app reste ouverte : nouvelles notifications d'abonnement toutes les 2 minutes
|
||||
useEffect(() => {
|
||||
if (!enabled) return
|
||||
const id = setInterval(() => {
|
||||
if (document.visibilityState === 'visible' && navigator.onLine) unreadNotifications().then(setUnread, () => {})
|
||||
}, 120_000)
|
||||
return () => clearInterval(id)
|
||||
}, [enabled])
|
||||
|
||||
// Clé stable par écran (et non sa place dans la pile, qui change quand la pile est écourtée)
|
||||
const push = useCallback((o: Overlay) => setStack((s) => [...s.slice(-8), { ...o, uid: ++overlayUid }]), [])
|
||||
@@ -181,6 +199,8 @@ export function SocialProvider({
|
||||
enabled,
|
||||
me,
|
||||
requests,
|
||||
unread,
|
||||
openNotifications: () => push({ kind: 'notifications' }),
|
||||
refresh,
|
||||
openProfile: (username) => push({ kind: 'profile', username }),
|
||||
openSearch: () => push({ kind: 'search' }),
|
||||
@@ -194,7 +214,7 @@ export function SocialProvider({
|
||||
sharedTick,
|
||||
bumpShared,
|
||||
}),
|
||||
[enabled, me, requests, refresh, push, openItem, openTitle, sharedTick, bumpShared],
|
||||
[enabled, me, requests, unread, refresh, push, openItem, openTitle, sharedTick, bumpShared],
|
||||
)
|
||||
|
||||
return (
|
||||
@@ -214,6 +234,8 @@ export function SocialProvider({
|
||||
<TitleReviews key={o.uid} externalId={o.externalId} title={o.title} onClose={() => close(o.uid)} onOpenProfile={(u) => push({ kind: 'profile', username: u })} />
|
||||
) : o.kind === 'sharedList' ? (
|
||||
<SharedListSheet key={o.uid} id={o.id} onClose={() => close(o.uid)} />
|
||||
) : o.kind === 'notifications' ? (
|
||||
<NotificationsSheet key={o.uid} onClose={() => close(o.uid)} onSeen={() => setUnread(0)} />
|
||||
) : (
|
||||
<InvitePeople key={o.uid} listId={o.listId} listName={o.listName} onClose={() => close(o.uid)} />
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user