|
| 1 | +import Toolbar from "@/components/toolbar"; |
| 2 | +import CollectionMapCard from "@/components/collection-map-card"; |
| 3 | +import { |
| 4 | + Pagination, |
| 5 | + PaginationContent, |
| 6 | + PaginationEllipsis, |
| 7 | + PaginationItem, |
| 8 | + PaginationLink, |
| 9 | + PaginationNext, |
| 10 | + PaginationPrevious, |
| 11 | +} from "@/components/ui/pagination"; |
| 12 | +import { getCollectionMaps } from "@/lib/collections"; |
| 13 | + |
| 14 | +const PAGE_SIZE = 6; |
| 15 | +const MAX_PAGE_LINKS = 5; |
| 16 | + |
| 17 | +const parsePage = (rawPage: string | string[] | undefined) => { |
| 18 | + const pageValue = Array.isArray(rawPage) ? rawPage[0] : rawPage; |
| 19 | + const parsed = Number.parseInt(pageValue ?? "1", 10); |
| 20 | + if (!Number.isFinite(parsed) || parsed < 1) return 1; |
| 21 | + return parsed; |
| 22 | +}; |
| 23 | + |
| 24 | +const getPaginationItems = (currentPage: number, totalPages: number) => { |
| 25 | + if (totalPages <= MAX_PAGE_LINKS) { |
| 26 | + return Array.from({ length: totalPages }, (_, index) => index + 1); |
| 27 | + } |
| 28 | + |
| 29 | + const pages = new Set<number>([1, totalPages, currentPage]); |
| 30 | + if (currentPage > 1) pages.add(currentPage - 1); |
| 31 | + if (currentPage < totalPages) pages.add(currentPage + 1); |
| 32 | + |
| 33 | + const sorted = [...pages].sort((a, b) => a - b); |
| 34 | + const result: Array<number | "ellipsis"> = []; |
| 35 | + |
| 36 | + for (let i = 0; i < sorted.length; i += 1) { |
| 37 | + const page = sorted[i]; |
| 38 | + const prev = sorted[i - 1]; |
| 39 | + if (prev !== undefined && page - prev > 1) { |
| 40 | + result.push("ellipsis"); |
| 41 | + } |
| 42 | + result.push(page); |
| 43 | + } |
| 44 | + |
| 45 | + return result; |
| 46 | +}; |
| 47 | + |
| 48 | +export default async function CollectionsPage({ |
| 49 | + searchParams, |
| 50 | +}: { |
| 51 | + searchParams: Promise<{ page?: string | string[] }>; |
| 52 | +}) { |
| 53 | + const { page } = await searchParams; |
| 54 | + const maps = await getCollectionMaps(); |
| 55 | + const totalPages = Math.max(1, Math.ceil(maps.length / PAGE_SIZE)); |
| 56 | + const currentPage = Math.min(parsePage(page), totalPages); |
| 57 | + const startIndex = (currentPage - 1) * PAGE_SIZE; |
| 58 | + const pageMaps = maps.slice(startIndex, startIndex + PAGE_SIZE); |
| 59 | + const prevPage = currentPage > 1 ? currentPage - 1 : null; |
| 60 | + const nextPage = currentPage < totalPages ? currentPage + 1 : null; |
| 61 | + |
| 62 | + return ( |
| 63 | + <main className="flex h-screen flex-col overflow-hidden bg-background"> |
| 64 | + <Toolbar /> |
| 65 | + <section className="flex-1 overflow-auto px-6 py-10"> |
| 66 | + <div className="mx-auto max-w-4xl space-y-8"> |
| 67 | + <header className="space-y-2"> |
| 68 | + <h1 className="text-3xl font-semibold tracking-tight"> |
| 69 | + Community Collections |
| 70 | + </h1> |
| 71 | + <p className="text-sm text-muted-foreground"> |
| 72 | + Shared maps contributed through GitHub pull requests. |
| 73 | + </p> |
| 74 | + <p className="text-xs text-muted-foreground"> |
| 75 | + Add your own map: <code>collections/maps/<id>.json</code> |
| 76 | + </p> |
| 77 | + </header> |
| 78 | + |
| 79 | + {maps.length === 0 ? ( |
| 80 | + <div className="rounded-lg border bg-card p-6 text-sm text-muted-foreground"> |
| 81 | + No community maps yet. |
| 82 | + </div> |
| 83 | + ) : ( |
| 84 | + <div className="grid gap-4 lg:grid-cols-2"> |
| 85 | + {pageMaps.map((map) => ( |
| 86 | + <CollectionMapCard key={map.id} map={map} /> |
| 87 | + ))} |
| 88 | + </div> |
| 89 | + )} |
| 90 | + {maps.length > 0 ? ( |
| 91 | + <div className="space-y-3 border-t pt-4"> |
| 92 | + <p className="text-sm text-muted-foreground"> |
| 93 | + Page {currentPage} of {totalPages} |
| 94 | + </p> |
| 95 | + <Pagination className="mx-0 justify-center"> |
| 96 | + <PaginationContent> |
| 97 | + <PaginationItem> |
| 98 | + <PaginationPrevious |
| 99 | + href={prevPage ? `/collections?page=${prevPage}` : "#"} |
| 100 | + className={ |
| 101 | + !prevPage ? "pointer-events-none opacity-50" : undefined |
| 102 | + } |
| 103 | + aria-disabled={!prevPage} |
| 104 | + tabIndex={prevPage ? undefined : -1} |
| 105 | + /> |
| 106 | + </PaginationItem> |
| 107 | + {getPaginationItems(currentPage, totalPages).map( |
| 108 | + (item, index) => { |
| 109 | + if (item === "ellipsis") { |
| 110 | + return ( |
| 111 | + <PaginationItem key={`ellipsis-${index}`}> |
| 112 | + <PaginationEllipsis /> |
| 113 | + </PaginationItem> |
| 114 | + ); |
| 115 | + } |
| 116 | + return ( |
| 117 | + <PaginationItem key={item}> |
| 118 | + <PaginationLink |
| 119 | + href={`/collections?page=${item}`} |
| 120 | + isActive={item === currentPage} |
| 121 | + > |
| 122 | + {item} |
| 123 | + </PaginationLink> |
| 124 | + </PaginationItem> |
| 125 | + ); |
| 126 | + }, |
| 127 | + )} |
| 128 | + <PaginationItem> |
| 129 | + <PaginationNext |
| 130 | + href={nextPage ? `/collections?page=${nextPage}` : "#"} |
| 131 | + className={ |
| 132 | + !nextPage ? "pointer-events-none opacity-50" : undefined |
| 133 | + } |
| 134 | + aria-disabled={!nextPage} |
| 135 | + tabIndex={nextPage ? undefined : -1} |
| 136 | + /> |
| 137 | + </PaginationItem> |
| 138 | + </PaginationContent> |
| 139 | + </Pagination> |
| 140 | + </div> |
| 141 | + ) : null} |
| 142 | + </div> |
| 143 | + </section> |
| 144 | + </main> |
| 145 | + ); |
| 146 | +} |
0 commit comments