| 1 | import Link from "next/link"; |
| 2 | |
| 3 | import { buttonVariants } |
| 4 | import { cn } |
| 5 | |
| 6 | /** Keyset cursors only run forward, so the way back is the first page. Renders nothing when everything fits on one page. */ |
| 7 | export function CursorPagination({ |
| 8 | pathname, |
| 9 | params = {}, |
| 10 | cursor, |
| 11 | nextCursor, |
| 12 | }: { |
| 13 | pathname: string |
| 14 | /** The page's other query parameters, kept on both links; unset ones are dropped. */ |
| 15 | params?: Record |
| 16 | cursor: string |
| 17 | nextCursor: string |
| 18 | }) { |
| 19 | const isFirstPage = |
| 20 | if (isFirstPage && |
| 21 | |
| 22 | const kept = |
| 23 | (entry): entry |
| 24 | ); |
| 25 | const firstHref = |
| 26 | ? `${pathname}? |
| 27 | : pathname; |
| 28 | const nextHref = |
| 29 | ? `${pathname}? |
| 30 | : null; |
| 31 | |
| 32 | const link = |
| 33 | buttonVariants({ |
| 34 | variant: "outline", |
| 35 | size: "sm", |
| 36 | className: cn(!enabled |
| 37 | }); |
| 38 | |
| 39 | return ( |
| 40 | <div className="flex justify-center gap-2 |
| 41 | <Link |
| 42 | href={firstHref} |
| 43 | aria-disabled={isFirstPage} |
| 44 | className={link(! |
| 45 | > |
| 46 | Newest |
| 47 | </Link> |
| 48 | <Link |
| 49 | href={nextHref ?? |
| 50 | aria-disabled={!nextHref} |
| 51 | className={link(nextHref |
| 52 | > |
| 53 | Older |
| 54 | </Link> |
| 55 | </div> |
| 56 | ); |
| 57 | } |