Skip to content

Commit cc49d58

Browse files
committed
feat: adding query params in character, tag, company, person detail page
1 parent ba20351 commit cc49d58

File tree

9 files changed

+76
-30
lines changed

9 files changed

+76
-30
lines changed

app/company/[id]/page.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { generateKunMetadataTemplate } from './metadata'
22
import { CompanyDetailContainer } from '~/components/company/detail/Container'
33
import { kunGetCompanyByIdActions, kunCompanyGalgameActions } from './actions'
44
import { ErrorComponent } from '~/components/error/ErrorComponent'
5+
import { pageSearchParamsCache } from '~/components/nuqs/page'
6+
import { NuqsAdapter } from 'nuqs/adapters/next/app'
57
import type { Metadata } from 'next'
8+
import type { SearchParams } from 'nuqs/server'
69

710
export const revalidate = 5
811

912
interface Props {
1013
params: Promise<{ id: string }>
14+
searchParams: Promise<SearchParams>
1115
}
1216

1317
export const generateMetadata = async ({
@@ -21,8 +25,9 @@ export const generateMetadata = async ({
2125
return generateKunMetadataTemplate(company)
2226
}
2327

24-
export default async function Kun({ params }: Props) {
28+
export default async function Kun({ params, searchParams }: Props) {
2529
const { id } = await params
30+
const { page } = await pageSearchParamsCache.parse(searchParams)
2631

2732
const company = await kunGetCompanyByIdActions({ companyId: Number(id) })
2833
if (typeof company === 'string') {
@@ -31,18 +36,20 @@ export default async function Kun({ params }: Props) {
3136

3237
const response = await kunCompanyGalgameActions({
3338
companyId: Number(id),
34-
page: 1,
39+
page,
3540
limit: 24
3641
})
3742
if (typeof response === 'string') {
3843
return <ErrorComponent error={response} />
3944
}
4045

4146
return (
42-
<CompanyDetailContainer
43-
initialCompany={company}
44-
initialPatches={response.galgames}
45-
total={response.total}
46-
/>
47+
<NuqsAdapter>
48+
<CompanyDetailContainer
49+
initialCompany={company}
50+
initialPatches={response.galgames}
51+
total={response.total}
52+
/>
53+
</NuqsAdapter>
4754
)
4855
}

app/galgame/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CardContainer } from '~/components/galgame/Container'
22
import { kunGetActions } from './actions'
33
import { ErrorComponent } from '~/components/error/ErrorComponent'
44
import { kunMetadata } from './metadata'
5-
import { galgameSearchParamsCache } from '~/components/galgame/_searchParams'
5+
import { galgameSearchParamsCache } from '~/components/nuqs/galgame'
66
import { NuqsAdapter } from 'nuqs/adapters/next/app'
77
import type { Metadata } from 'next'
88
import type { SearchParams } from 'nuqs/server'

app/tag/[id]/page.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { TagDetailContainer } from '~/components/tag/detail/Container'
22
import { generateKunMetadataTemplate } from './metadata'
33
import { kunGetTagByIdActions, kunTagGalgameActions } from './actions'
44
import { ErrorComponent } from '~/components/error/ErrorComponent'
5+
import { pageSearchParamsCache } from '~/components/nuqs/page'
6+
import { NuqsAdapter } from 'nuqs/adapters/next/app'
57
import type { Metadata } from 'next'
8+
import type { SearchParams } from 'nuqs/server'
69

710
export const revalidate = 5
811

912
interface Props {
1013
params: Promise<{ id: string }>
14+
searchParams: Promise<SearchParams>
1115
}
1216

1317
export const generateMetadata = async ({
@@ -21,8 +25,9 @@ export const generateMetadata = async ({
2125
return generateKunMetadataTemplate(tag)
2226
}
2327

24-
export default async function Kun({ params }: Props) {
28+
export default async function Kun({ params, searchParams }: Props) {
2529
const { id } = await params
30+
const { page } = await pageSearchParamsCache.parse(searchParams)
2631

2732
const tag = await kunGetTagByIdActions({ tagId: Number(id) })
2833
if (typeof tag === 'string') {
@@ -31,18 +36,20 @@ export default async function Kun({ params }: Props) {
3136

3237
const response = await kunTagGalgameActions({
3338
tagId: Number(id),
34-
page: 1,
39+
page,
3540
limit: 24
3641
})
3742
if (typeof response === 'string') {
3843
return <ErrorComponent error={response} />
3944
}
4045

4146
return (
42-
<TagDetailContainer
43-
initialTag={tag}
44-
initialPatches={response.galgames}
45-
total={response.total}
46-
/>
47+
<NuqsAdapter>
48+
<TagDetailContainer
49+
initialTag={tag}
50+
initialPatches={response.galgames}
51+
total={response.total}
52+
/>
53+
</NuqsAdapter>
4754
)
4855
}

components/company/detail/Container.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use client'
22

33
import { useState, useTransition, useEffect } from 'react'
4-
import { useRouter } from '@bprogress/next'
4+
import { useQueryState, parseAsInteger } from 'nuqs'
55
import { Chip } from '@heroui/react'
6-
import { useDisclosure } from '@heroui/modal'
76
import { Link } from '@heroui/link'
87
import { KunPagination } from '~/components/kun/KunPagination'
98
import { useMounted } from '~/hooks/useMounted'
@@ -28,11 +27,18 @@ export const CompanyDetailContainer: FC<Props> = ({
2827
total
2928
}) => {
3029
const isMounted = useMounted()
31-
const [page, setPage] = useState(1)
30+
const [loading, startTransition] = useTransition()
31+
32+
const [page, setPage] = useQueryState(
33+
'page',
34+
parseAsInteger.withDefault(1).withOptions({
35+
shallow: false,
36+
startTransition
37+
})
38+
)
3239

33-
const [company, setCompany] = useState(initialCompany)
40+
const [company] = useState(initialCompany)
3441
const [patches, setPatches] = useState<GalgameCard[]>(initialPatches)
35-
const [loading, startTransition] = useTransition()
3642

3743
const fetchPatches = () => {
3844
startTransition(async () => {
@@ -55,6 +61,11 @@ export const CompanyDetailContainer: FC<Props> = ({
5561
fetchPatches()
5662
}, [page])
5763

64+
const handlePageChange = (newPage: number) => {
65+
setPage(newPage)
66+
window.scrollTo(0, 0)
67+
}
68+
5869
return (
5970
<div className="w-ful space-y-8 my-4">
6071
<KunHeader
@@ -135,7 +146,7 @@ export const CompanyDetailContainer: FC<Props> = ({
135146
<KunPagination
136147
total={Math.ceil(total / 24)}
137148
page={page}
138-
onChange={setPage}
149+
onChange={handlePageChange}
139150
isDisabled={loading}
140151
/>
141152
</div>

components/galgame/Container.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { useMounted } from '~/hooks/useMounted'
99
import { KunLoading } from '~/components/kun/Loading'
1010
import { KunHeader } from '../kun/Header'
1111
import { KunPagination } from '~/components/kun/KunPagination'
12-
import { galgameParsers } from './_searchParams'
13-
import type { SortDirection, SortOption } from './_searchParams'
12+
import { galgameParsers } from '~/components/nuqs/galgame'
13+
import type { SortDirection, SortOption } from '~/components/nuqs/galgame'
1414

1515
interface Props {
1616
initialGalgames: GalgameCard[]

components/galgame/FilterBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
GALGAME_SORT_YEARS_MAP,
2424
GALGAME_SORT_MONTHS
2525
} from '~/constants/galgame'
26-
import type { SortDirection, SortOption } from './_searchParams'
26+
import type { SortDirection, SortOption } from '~/components/nuqs/galgame'
2727

2828
interface Props {
2929
selectedType: string

components/nuqs/page.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createSearchParamsCache, parseAsInteger } from 'nuqs/server'
2+
3+
export const pageParsers = {
4+
page: parseAsInteger.withDefault(1)
5+
}
6+
7+
export const pageSearchParamsCache = createSearchParamsCache(pageParsers)

components/tag/detail/Container.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client'
22

3-
import { useEffect, useState } from 'react'
3+
import { useEffect, useState, useTransition } from 'react'
4+
import { useQueryState, parseAsInteger } from 'nuqs'
45
import { KunPagination } from '~/components/kun/KunPagination'
56
import { kunFetchGet } from '~/utils/kunFetch'
67
import { Chip } from '@heroui/chip'
@@ -23,9 +24,17 @@ export const TagDetailContainer = ({
2324
total
2425
}: Props) => {
2526
const isMounted = useMounted()
26-
const [page, setPage] = useState(1)
27+
const [isPending, startTransition] = useTransition()
2728

28-
const [tag, setTag] = useState(initialTag)
29+
const [page, setPage] = useQueryState(
30+
'page',
31+
parseAsInteger.withDefault(1).withOptions({
32+
shallow: false,
33+
startTransition
34+
})
35+
)
36+
37+
const [tag] = useState(initialTag)
2938
const [patches, setPatches] = useState<GalgameCard[]>(initialPatches)
3039
const [loading, setLoading] = useState(false)
3140

@@ -52,6 +61,11 @@ export const TagDetailContainer = ({
5261
fetchPatches()
5362
}, [page])
5463

64+
const handlePageChange = (newPage: number) => {
65+
setPage(newPage)
66+
window.scrollTo(0, 0)
67+
}
68+
5569
return (
5670
<div className="w-full space-y-8 my-4">
5771
<KunHeader
@@ -77,7 +91,7 @@ export const TagDetailContainer = ({
7791
</div>
7892
)}
7993

80-
{loading ? (
94+
{loading || isPending ? (
8195
<KunLoading hint="正在获取 Galgame 中..." />
8296
) : (
8397
<>
@@ -92,8 +106,8 @@ export const TagDetailContainer = ({
92106
<KunPagination
93107
total={Math.ceil(total / 24)}
94108
page={page}
95-
onChange={setPage}
96-
isDisabled={loading}
109+
onChange={handlePageChange}
110+
isDisabled={loading || isPending}
97111
/>
98112
</div>
99113
)}

0 commit comments

Comments
 (0)