generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathpage.tsx
83 lines (78 loc) · 2.54 KB
/
page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Description } from '@/src/components/typography'
import { Separator } from '@/src/components/ui/separator'
import type { SearchParams } from 'nuqs/server'
import { Suspense } from 'react'
import { SourceOnGitHub } from '../_components/source-on-github'
import { getMetadata } from '../demos'
import { fetchProducts, pageCount } from './api'
import { ClientPaginationControls } from './pagination-controls.client'
import { ServerPaginationControls } from './pagination-controls.server'
import { ProductView } from './product'
import { RenderingControls } from './rendering-controls'
import { searchParamsCache } from './searchParams'
export const metadata = getMetadata('pagination')
type PageProps = {
searchParams: Promise<SearchParams>
}
export default async function PaginationDemoPage({ searchParams }: PageProps) {
// Allow nested RSCs to access the search params (in a type-safe way)
await searchParamsCache.parse(searchParams)
return (
<>
<h1>{metadata.title}</h1>
<Description>{metadata.description}</Description>
<h2>Rendering controls</h2>
<Suspense>
<RenderingControls />
</Suspense>
<Separator className="my-8" />
<PaginationRenderer />
<Suspense>
<ProductSection />
</Suspense>
<SourceOnGitHub path="pagination/searchParams.ts" />
<SourceOnGitHub path="pagination/page.tsx" />
<SourceOnGitHub path="pagination/pagination-controls.server.tsx" />
<SourceOnGitHub path="pagination/pagination-controls.client.tsx" />
</>
)
}
function PaginationRenderer() {
// Showcasing the use of search params cache in nested RSCs
const renderOn = searchParamsCache.get('renderOn')
return (
<>
<h2>
Pagination controls{' '}
<small className="text-sm font-medium text-zinc-500">
({renderOn}-rendered)
</small>
</h2>
{renderOn === 'server' && (
<ServerPaginationControls numPages={pageCount} />
)}
<Suspense key="client">
{renderOn === 'client' && (
<ClientPaginationControls numPages={pageCount} />
)}
</Suspense>
</>
)
}
async function ProductSection() {
const { page, delay } = searchParamsCache.all()
const products = await fetchProducts(page, delay)
return (
<section>
<h2>
Product list{' '}
<small className="text-sm font-medium text-zinc-500">
(server-rendered)
</small>
</h2>
{products.map(product => (
<ProductView product={product} key={product.id} />
))}
</section>
)
}