- 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
62 lines
2.3 KiB
TypeScript
62 lines
2.3 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { Link, Outlet, useLocation, useNavigate, useParams } from 'react-router'
|
|
import { Badge } from '@/components/ui/badge'
|
|
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
|
import { api } from '@/lib/api'
|
|
|
|
const tabs = ['settings', 'members', 'held', 'archive', 'sent'] as const
|
|
|
|
export function ListPage() {
|
|
const params = useParams()
|
|
const id = /^\d+$/.test(params.id ?? '') ? Number(params.id) : null
|
|
const navigate = useNavigate()
|
|
const { pathname } = useLocation()
|
|
|
|
const list = useQuery({ queryKey: ['list', id], queryFn: () => api.lists.get(id!), enabled: id !== null })
|
|
const held = useQuery({ queryKey: ['held', id], queryFn: () => api.held.list(id!), enabled: id !== null })
|
|
|
|
if (id === null || (list.isError && list.error.message === 'not found')) {
|
|
return <NotFound />
|
|
}
|
|
if (list.isPending) return <p className="text-muted-foreground">Loading…</p>
|
|
if (list.isError) return <p role="alert" className="text-destructive">{list.error.message}</p>
|
|
|
|
const current = tabs.find((t) => pathname.endsWith(`/${t}`)) ?? 'settings'
|
|
const heldCount = held.data?.length ?? 0
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div>
|
|
<Link to="/" className="text-sm text-primary underline">
|
|
All lists
|
|
</Link>
|
|
<h1 className="mt-2 text-2xl font-semibold">{list.data.address}</h1>
|
|
<p className="text-muted-foreground">{list.data.title}</p>
|
|
</div>
|
|
<Tabs value={current} onValueChange={(t) => navigate(t === 'settings' ? `/lists/${id}` : `/lists/${id}/${t}`)}>
|
|
<TabsList>
|
|
<TabsTrigger value="settings">Settings</TabsTrigger>
|
|
<TabsTrigger value="members">Members</TabsTrigger>
|
|
<TabsTrigger value="held">
|
|
Held
|
|
{heldCount > 0 && <Badge className="ml-1">{heldCount}</Badge>}
|
|
</TabsTrigger>
|
|
<TabsTrigger value="archive">Archive</TabsTrigger>
|
|
<TabsTrigger value="sent">Sent</TabsTrigger>
|
|
</TabsList>
|
|
</Tabs>
|
|
<Outlet context={list.data} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function NotFound() {
|
|
return (
|
|
<div className="space-y-2">
|
|
<h1 className="text-2xl font-semibold">List not found</h1>
|
|
<Link to="/" className="text-primary underline">
|
|
Back to lists
|
|
</Link>
|
|
</div>
|
|
)
|
|
}
|