parsimony/admin/src/tabs/SentTab.tsx
tgiachi ee1f837a20 feat(admin): a Sent tab with the posts of a list and their progress
- route /lists/:id/sent and a Sent tab on the list page
- a table of posts (subject, date, sent of recipients with a progress
  bar, recipients, pending, failed, dropped) and an empty state; a post
  with no recipients shows no bar
- refreshes every 5 seconds while any post has pending deliveries, and
  stops when none has
- api.posts.list, the Post type and the fake API route; tests
2026-11-10 12:43:55 +01:00

81 lines
3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useOutletContext } from 'react-router'
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'
import { api, type List, type Post } from '@/lib/api'
import { formatDate } from '@/lib/format'
/** How often the tab reloads by itself while a post is still being sent. */
export const REFRESH_MS = 5000
export function SentTab() {
const list = useOutletContext<List>()
const posts = useQuery({
queryKey: ['posts', list.id],
queryFn: () => api.posts.list(list.id),
refetchInterval: (q) => (q.state.data?.some((p) => p.pending > 0) ? REFRESH_MS : false),
})
const items = posts.data ?? []
return (
<div className="space-y-4">
<h2 className="text-lg font-semibold">Sent posts</h2>
{posts.isPending && <p className="text-muted-foreground">Loading…</p>}
{posts.isError && <p role="alert" className="text-destructive">{posts.error.message}</p>}
{posts.data && items.length === 0 && (
<p className="text-muted-foreground">Nothing has been sent to this list yet.</p>
)}
{items.length > 0 && (
<Table>
<TableHeader>
<TableRow>
<TableHead>Subject</TableHead>
<TableHead>Date</TableHead>
<TableHead>Sent</TableHead>
<TableHead>Recipients</TableHead>
<TableHead>Pending</TableHead>
<TableHead>Failed</TableHead>
<TableHead>Dropped</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{items.map((p) => (
<TableRow key={p.id}>
<TableCell className="max-w-xs whitespace-normal font-medium">{p.subject || '(no subject)'}</TableCell>
<TableCell>{formatDate(p.created_at)}</TableCell>
<TableCell>
<Progress post={p} />
</TableCell>
<TableCell>{p.recipients}</TableCell>
<TableCell>{p.pending}</TableCell>
<TableCell>{p.failed}</TableCell>
<TableCell>{p.dropped}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
</div>
)
}
function Progress({ post }: { post: Post }) {
if (post.recipients === 0) return <span className="text-muted-foreground">No recipients</span>
const pct = Math.round((100 * post.sent) / post.recipients)
return (
<div className="flex items-center gap-2">
<div
role="progressbar"
aria-label={`Sent to ${post.sent} of ${post.recipients}`}
aria-valuemin={0}
aria-valuemax={post.recipients}
aria-valuenow={post.sent}
className="h-2 w-24 overflow-hidden rounded-full bg-muted"
>
<div className="h-full bg-primary" style={{ width: `${pct}%` }} />
</div>
<span className="text-sm text-muted-foreground">
{post.sent} of {post.recipients}
</span>
</div>
)
}