|
1 | | -'use client' |
2 | | - |
3 | | -import { useEffect, useState } from 'react' |
4 | | -import { Pagination } from '@heroui/pagination' |
5 | | -import { kunFetchGet } from '~/utils/kunFetch' |
6 | | -import { GalgameCard } from './Card' |
7 | | -import { FilterBar } from './FilterBar' |
8 | | -import { useMounted } from '~/hooks/useMounted' |
9 | | -import { KunLoading } from '~/components/kun/Loading' |
10 | | -import { KunHeader } from '../kun/Header' |
11 | | -import { useRouter, useSearchParams } from 'next/navigation' |
12 | | -import type { SortDirection, SortOption } from './_sort' |
13 | | - |
14 | | -interface Props { |
15 | | - initialGalgames: GalgameCard[] |
16 | | - initialTotal: number |
17 | | -} |
18 | | - |
19 | | -export const CardContainer = ({ initialGalgames, initialTotal }: Props) => { |
20 | | - const [galgames, setGalgames] = useState<GalgameCard[]>(initialGalgames) |
21 | | - const [total, setTotal] = useState(initialTotal) |
22 | | - const [loading, setLoading] = useState(false) |
23 | | - const [selectedType, setSelectedType] = useState<string>('all') |
24 | | - const [sortField, setSortField] = useState<SortOption>('resource_update_time') |
25 | | - const [sortOrder, setSortOrder] = useState<SortDirection>('desc') |
26 | | - const [selectedYears, setSelectedYears] = useState<string[]>(['all']) |
27 | | - const [selectedMonths, setSelectedMonths] = useState<string[]>(['all']) |
28 | | - const isMounted = useMounted() |
29 | | - const router = useRouter() |
30 | | - const searchParams = useSearchParams() |
31 | | - const [page, setPage] = useState(Number(searchParams.get('page')) || 1) |
32 | | - |
33 | | - const fetchPatches = async () => { |
34 | | - setLoading(true) |
35 | | - |
36 | | - const { galgames, total } = await kunFetchGet<{ |
37 | | - galgames: GalgameCard[] |
38 | | - total: number |
39 | | - }>('/galgame', { |
40 | | - selectedType, |
41 | | - sortField, |
42 | | - sortOrder, |
43 | | - page, |
44 | | - limit: 24, |
45 | | - yearString: JSON.stringify(selectedYears), |
46 | | - monthString: JSON.stringify(selectedMonths) |
47 | | - }) |
48 | | - |
49 | | - setGalgames(galgames) |
50 | | - setTotal(total) |
51 | | - setLoading(false) |
52 | | - } |
53 | | - |
54 | | - useEffect(() => { |
55 | | - if (!isMounted) { |
56 | | - return |
57 | | - } |
58 | | - fetchPatches() |
59 | | - }, [sortField, sortOrder, selectedType, page, selectedYears, selectedMonths]) |
60 | | - |
61 | | - const handlePageChange = (newPage: number) => { |
62 | | - setPage(newPage) |
63 | | - setTimeout(() => { |
64 | | - window.scrollTo(0, 0) |
65 | | - }) |
66 | | - const params = new URLSearchParams(window.location.search) |
67 | | - params.set('page', newPage.toString()) |
68 | | - router.push(`?${params.toString()}`) |
69 | | - } |
70 | | - |
71 | | - return ( |
72 | | - <div className="container mx-auto my-4 space-y-6"> |
73 | | - <KunHeader |
74 | | - name="Galgame" |
75 | | - description="本页面默认仅显示了 SFW (内容安全) 的补丁, 您可以在网站右上角切换显示全部补丁 (包括 NSFW, 也就是显示可能带有涩涩的补丁)" |
76 | | - /> |
77 | | - |
78 | | - <FilterBar |
79 | | - selectedType={selectedType} |
80 | | - setSelectedType={(key) => { |
81 | | - if (key) { |
82 | | - setSelectedType(key) |
83 | | - } |
84 | | - }} |
85 | | - sortField={sortField} |
86 | | - setSortField={setSortField} |
87 | | - sortOrder={sortOrder} |
88 | | - setSortOrder={setSortOrder} |
89 | | - selectedYears={selectedYears} |
90 | | - setSelectedYears={setSelectedYears} |
91 | | - selectedMonths={selectedMonths} |
92 | | - setSelectedMonths={setSelectedMonths} |
93 | | - /> |
94 | | - |
95 | | - {loading ? ( |
96 | | - <KunLoading hint="正在获取 Galgame 数据..." /> |
97 | | - ) : ( |
98 | | - <div className="grid grid-cols-2 gap-2 mx-auto mb-8 sm:gap-6 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4"> |
99 | | - {galgames.map((pa) => ( |
100 | | - <GalgameCard key={pa.id} patch={pa} /> |
101 | | - ))} |
102 | | - </div> |
103 | | - )} |
104 | | - |
105 | | - {total > 24 && ( |
106 | | - <div className="flex justify-center"> |
107 | | - <Pagination |
108 | | - total={Math.ceil(total / 24)} |
109 | | - page={page} |
110 | | - onChange={handlePageChange} |
111 | | - showControls |
112 | | - color="primary" |
113 | | - size="lg" |
114 | | - /> |
115 | | - </div> |
116 | | - )} |
117 | | - </div> |
118 | | - ) |
119 | | -} |
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useEffect, useState, useTransition } from 'react' |
| 4 | +import { useQueryStates } from 'nuqs' |
| 5 | +import { kunFetchGet } from '~/utils/kunFetch' |
| 6 | +import { GalgameCard } from './Card' |
| 7 | +import { FilterBar } from './FilterBar' |
| 8 | +import { useMounted } from '~/hooks/useMounted' |
| 9 | +import { KunLoading } from '~/components/kun/Loading' |
| 10 | +import { KunHeader } from '../kun/Header' |
| 11 | +import { KunPagination } from '~/components/kun/KunPagination' |
| 12 | +import { galgameParsers } from './_searchParams' |
| 13 | +import type { SortDirection, SortOption } from './_searchParams' |
| 14 | + |
| 15 | +interface Props { |
| 16 | + initialGalgames: GalgameCard[] |
| 17 | + initialTotal: number |
| 18 | +} |
| 19 | + |
| 20 | +export const CardContainer = ({ initialGalgames, initialTotal }: Props) => { |
| 21 | + const [galgames, setGalgames] = useState<GalgameCard[]>(initialGalgames) |
| 22 | + const [total, setTotal] = useState(initialTotal) |
| 23 | + const [loading, setLoading] = useState(false) |
| 24 | + const isMounted = useMounted() |
| 25 | + const [isPending, startTransition] = useTransition() |
| 26 | + |
| 27 | + const [params, setParams] = useQueryStates(galgameParsers, { |
| 28 | + shallow: false, |
| 29 | + startTransition |
| 30 | + }) |
| 31 | + |
| 32 | + const { page, type, sortField, sortOrder, years, months } = params |
| 33 | + |
| 34 | + const fetchPatches = async () => { |
| 35 | + setLoading(true) |
| 36 | + |
| 37 | + const { galgames, total } = await kunFetchGet<{ |
| 38 | + galgames: GalgameCard[] |
| 39 | + total: number |
| 40 | + }>('/galgame', { |
| 41 | + selectedType: type, |
| 42 | + sortField, |
| 43 | + sortOrder, |
| 44 | + page, |
| 45 | + limit: 24, |
| 46 | + yearString: JSON.stringify(years), |
| 47 | + monthString: JSON.stringify(months) |
| 48 | + }) |
| 49 | + |
| 50 | + setGalgames(galgames) |
| 51 | + setTotal(total) |
| 52 | + setLoading(false) |
| 53 | + } |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + if (!isMounted) { |
| 57 | + return |
| 58 | + } |
| 59 | + fetchPatches() |
| 60 | + }, [sortField, sortOrder, type, page, years, months]) |
| 61 | + |
| 62 | + const handlePageChange = (newPage: number) => { |
| 63 | + setParams({ page: newPage }) |
| 64 | + setTimeout(() => { |
| 65 | + window.scrollTo(0, 0) |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + const handleTypeChange = (newType: string) => { |
| 70 | + if (newType) { |
| 71 | + setParams({ type: newType, page: 1 }) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + const handleSortFieldChange = (newSortField: SortOption) => { |
| 76 | + setParams({ sortField: newSortField, page: 1 }) |
| 77 | + } |
| 78 | + |
| 79 | + const handleSortOrderChange = (newSortOrder: SortDirection) => { |
| 80 | + setParams({ sortOrder: newSortOrder, page: 1 }) |
| 81 | + } |
| 82 | + |
| 83 | + const handleYearsChange = (newYears: string[]) => { |
| 84 | + setParams({ years: newYears, page: 1 }) |
| 85 | + } |
| 86 | + |
| 87 | + const handleMonthsChange = (newMonths: string[]) => { |
| 88 | + setParams({ months: newMonths, page: 1 }) |
| 89 | + } |
| 90 | + |
| 91 | + return ( |
| 92 | + <div className="container mx-auto my-4 space-y-6"> |
| 93 | + <KunHeader |
| 94 | + name="Galgame" |
| 95 | + description="本页面默认仅显示了 SFW (内容安全) 的补丁, 您可以在网站右上角切换显示全部补丁 (包括 NSFW, 也就是显示可能带有涩涩的补丁)" |
| 96 | + /> |
| 97 | + |
| 98 | + <FilterBar |
| 99 | + selectedType={type} |
| 100 | + setSelectedType={handleTypeChange} |
| 101 | + sortField={sortField} |
| 102 | + setSortField={handleSortFieldChange} |
| 103 | + sortOrder={sortOrder} |
| 104 | + setSortOrder={handleSortOrderChange} |
| 105 | + selectedYears={years} |
| 106 | + setSelectedYears={handleYearsChange} |
| 107 | + selectedMonths={months} |
| 108 | + setSelectedMonths={handleMonthsChange} |
| 109 | + /> |
| 110 | + |
| 111 | + {loading || isPending ? ( |
| 112 | + <KunLoading hint="正在获取 Galgame 数据..." /> |
| 113 | + ) : ( |
| 114 | + <div className="grid grid-cols-2 gap-2 mx-auto mb-8 sm:gap-6 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4"> |
| 115 | + {galgames.map((pa) => ( |
| 116 | + <GalgameCard key={pa.id} patch={pa} /> |
| 117 | + ))} |
| 118 | + </div> |
| 119 | + )} |
| 120 | + |
| 121 | + {total > 24 && ( |
| 122 | + <div className="flex justify-center"> |
| 123 | + <KunPagination |
| 124 | + page={page} |
| 125 | + total={Math.ceil(total / 24)} |
| 126 | + onChange={handlePageChange} |
| 127 | + isDisabled={loading || isPending} |
| 128 | + /> |
| 129 | + </div> |
| 130 | + )} |
| 131 | + </div> |
| 132 | + ) |
| 133 | +} |
0 commit comments