Notifications.tsx102 lines, 3.2 KB
| 1 | import { |
| 2 | infiniteQueryOptions, |
| 3 | useInfiniteQuery, |
| 4 | useMutation, |
| 5 | useQueryClient, |
| 6 | } from '@tanstack/react-query' |
| 7 | import { Heart } from 'lucide-react' |
| 8 | import { useEffect } from 'react' |
| 9 | import { Link } from 'react-router' |
| 10 | import { More } from '../../components/ui/More' |
| 11 | import { Failed, Notice, Skeletons } from '../../components/ui/Notice' |
| 12 | import { getSession, request, type Notes } from '../../lib/api' |
| 13 | import { ago } from '../../lib/time' |
| 14 | import { unreadOptions } from './unread' |
| 15 | |
| 16 | const notesOptions = infiniteQueryOptions({ |
| 17 | queryKey: ['notifications'], |
| 18 | queryFn: ({ pageParam }) => |
| 19 | request<Notes>(`/notifications?cursor=${pageParam || ''}`), |
| 20 | initialPageParam: 0, |
| 21 | getNextPageParam: (last) => last.next_cursor, |
| 22 | }) |
| 23 | |
| 24 | export function Notifications() { |
| 25 | const client = useQueryClient() |
| 26 | const query = useInfiniteQuery(notesOptions) |
| 27 | const handle = getSession()?.handle |
| 28 | const notes = query.data?.pages.flatMap((p) => p.notifications) ?? [] |
| 29 | const { mutate: read } = useMutation({ |
| 30 | mutationFn: (id: number) => |
| 31 | request('/notifications/read', { |
| 32 | method: 'POST', |
| 33 | json: { up_to: id }, |
| 34 | }), |
| 35 | onSuccess: () => client.setQueryData(unreadOptions.queryKey, 0), |
| 36 | }) |
| 37 | // Seen is read: the newest one marks everything up to it. The marks |
| 38 | // stay on screen until the next visit, so you can tell what's new. |
| 39 | const newest = notes[0] |
| 40 | useEffect(() => { |
| 41 | if (newest?.unread) read(newest.id) |
| 42 | }, [newest, read]) |
| 43 | |
| 44 | return ( |
| 45 | <> |
| 46 | <h1 className="bar">Notifications</h1> |
| 47 | {query.isPending ? ( |
| 48 | <Skeletons /> |
| 49 | ) : query.isError ? ( |
| 50 | <Failed error={query.error} retry={query.refetch} /> |
| 51 | ) : !notes.length ? ( |
| 52 | <Notice title="Nothing yet"> |
| 53 | When someone likes your post, it shows up here. |
| 54 | </Notice> |
| 55 | ) : ( |
| 56 | <> |
| 57 | <ol className="divide-y divide-line border-b border-line"> |
| 58 | {notes.map((n) => ( |
| 59 | <li |
| 60 | key={n.id} |
| 61 | className={`flex gap-3 px-4 py-3 text-sm |
| 62 | ${n.unread ? 'bg-accent/8' : ''}`} |
| 63 | > |
| 64 | <Heart |
| 65 | size={16} |
| 66 | className="mt-0.5 shrink-0 text-accent-ink" |
| 67 | /> |
| 68 | <div className="min-w-0 flex-1"> |
| 69 | <p className="flex gap-2"> |
| 70 | <span className="truncate"> |
| 71 | <Link |
| 72 | to={`/u/${n.actor}`} |
| 73 | className="font-medium hover:underline" |
| 74 | > |
| 75 | @{n.actor} |
| 76 | </Link>{' '} |
| 77 | liked your post |
| 78 | </span> |
| 79 | <time |
| 80 | dateTime={n.created_at} |
| 81 | className="ml-auto text-mute" |
| 82 | > |
| 83 | {ago(n.created_at)} |
| 84 | </time> |
| 85 | </p> |
| 86 | <Link |
| 87 | to={`/u/${handle}`} |
| 88 | className="block truncate text-mute hover:text-ink" |
| 89 | > |
| 90 | {n.body} |
| 91 | </Link> |
| 92 | </div> |
| 93 | {n.unread && <span className="sr-only">(new)</span>} |
| 94 | </li> |
| 95 | ))} |
| 96 | </ol> |
| 97 | <More pages={query} /> |
| 98 | </> |
| 99 | )} |
| 100 | </> |
| 101 | ) |
| 102 | } |