|
| 1 | +import { auth } from "@/auth"; |
| 2 | +import { requireGeoidClient } from "@/lib/geoid/client"; |
| 3 | +import type { PlaceRecord } from "@/lib/geoid/types"; |
| 4 | +import { Alert } from "@/components/ui/alert"; |
| 5 | +import { Link } from "@/components/ui/link"; |
| 6 | +import { cardBase } from "@/components/ui/styles"; |
| 7 | + |
| 8 | +export const dynamic = "force-dynamic"; |
| 9 | + |
| 10 | +const PAGE_SIZE = 50; |
| 11 | + |
| 12 | +export default async function MyGeoidsPage({ |
| 13 | + searchParams, |
| 14 | +}: { |
| 15 | + searchParams: Promise<{ offset?: string }>; |
| 16 | +}) { |
| 17 | + const session = await auth(); |
| 18 | + const offset = Number((await searchParams).offset) || 0; |
| 19 | + |
| 20 | + return ( |
| 21 | + <div className="mx-auto flex w-full max-w-5xl flex-1 flex-col min-h-0 gap-5"> |
| 22 | + <h1 className="text-2xl font-semibold tracking-tight text-text-primary">My GeoIDs</h1> |
| 23 | + |
| 24 | + {!session?.user ? ( |
| 25 | + <p className="text-sm text-text-muted">Sign in to view the GeoIDs you've registered.</p> |
| 26 | + ) : ( |
| 27 | + <MyGeoidsList offset={offset} /> |
| 28 | + )} |
| 29 | + </div> |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +async function MyGeoidsList({ offset }: { offset: number }) { |
| 34 | + let records: PlaceRecord[]; |
| 35 | + try { |
| 36 | + const client = await requireGeoidClient(); |
| 37 | + records = await client.listMyGeoids({ limit: PAGE_SIZE, offset }); |
| 38 | + } catch (err) { |
| 39 | + return <Alert type="error" message={(err as Error).message} />; |
| 40 | + } |
| 41 | + |
| 42 | + if (records.length === 0 && offset === 0) { |
| 43 | + return <p className="text-sm text-text-muted">You haven't registered any GeoIDs yet.</p>; |
| 44 | + } |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className="flex min-h-0 flex-1 flex-col gap-3"> |
| 48 | + <div className={`min-h-0 flex-1 overflow-auto ${cardBase}`}> |
| 49 | + <table className="w-full text-left text-sm"> |
| 50 | + <thead className="sticky top-0 bg-surface text-text-muted"> |
| 51 | + <tr className="border-b border-border"> |
| 52 | + <th className="px-3 py-2 font-medium">GeoID</th> |
| 53 | + <th className="px-3 py-2 font-medium">Collection</th> |
| 54 | + <th className="px-3 py-2 font-medium">External ID</th> |
| 55 | + <th className="px-3 py-2 font-medium">Registered</th> |
| 56 | + </tr> |
| 57 | + </thead> |
| 58 | + <tbody> |
| 59 | + {records.map((record) => ( |
| 60 | + <tr key={record.geoid} className="border-b border-border last:border-0"> |
| 61 | + <td className="px-3 py-2 font-mono"> |
| 62 | + <Link href={record.uri}>{record.geoid}</Link> |
| 63 | + </td> |
| 64 | + <td className="px-3 py-2 text-text-primary">{record.collection}</td> |
| 65 | + <td className="px-3 py-2 text-text-muted">{record.external_id ?? "—"}</td> |
| 66 | + <td className="px-3 py-2 text-text-muted"> |
| 67 | + {new Date(record.created_at).toLocaleString()} |
| 68 | + </td> |
| 69 | + </tr> |
| 70 | + ))} |
| 71 | + </tbody> |
| 72 | + </table> |
| 73 | + </div> |
| 74 | + <div className="flex justify-between text-sm"> |
| 75 | + {offset > 0 ? ( |
| 76 | + <Link href={`/my-geoids?offset=${Math.max(0, offset - PAGE_SIZE)}`}>Previous</Link> |
| 77 | + ) : ( |
| 78 | + <span /> |
| 79 | + )} |
| 80 | + {records.length === PAGE_SIZE && ( |
| 81 | + <Link href={`/my-geoids?offset=${offset + PAGE_SIZE}`}>Next</Link> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + ); |
| 86 | +} |
0 commit comments