|
1 | 1 | import { Search, X } from 'lucide-react'; |
2 | 2 | import { PAGE_SIZE } from '@/lib/config'; |
3 | | -import { useState, useMemo } from 'react'; |
4 | 3 | import type { CSSProperties } from 'react'; |
5 | 4 | import type { LGTMEntry, Rarity } from '@/lib/lgtm'; |
| 5 | +import { useState, useMemo, useEffect } from 'react'; |
6 | 6 | import { Pagination } from '@/components/pagination'; |
| 7 | +import { SETTINGS_KEY, loadSettings } from '@/lib/settings'; |
7 | 8 | import { RARITY_LABELS, CATEGORY_LABELS, getAllRarities } from '@/lib/lgtm'; |
8 | 9 | import { CATEGORY_COLORS, RARITY_COLORS, RARITY_ORDER } from '@/lib/content'; |
9 | 10 |
|
@@ -119,12 +120,26 @@ export function BrowseFilters({ entries }: BrowseFiltersProps) { |
119 | 120 | const allCategories = useMemo(() => [...new Set(entries.map((entry) => entry.category))].sort(), [entries]); |
120 | 121 |
|
121 | 122 | const allRarities = getAllRarities(); |
| 123 | + const [pageSize, setPageSize] = useState(PAGE_SIZE); |
122 | 124 | const [search, setSearch] = useState(''); |
123 | 125 | const [activeCategories, setActiveCategories] = useState<Set<string>>(new Set()); |
124 | 126 | const [activeRarities, setActiveRarities] = useState<Set<string>>(new Set()); |
125 | 127 | const [sort, setSort] = useState<SortOption>('alpha'); |
126 | 128 | const [page, setPage] = useState(1); |
127 | 129 |
|
| 130 | + useEffect(() => { |
| 131 | + // eslint-disable-next-line react-hooks/set-state-in-effect |
| 132 | + setPageSize(loadSettings().pageSize); |
| 133 | + |
| 134 | + function onStorage(e: StorageEvent) { |
| 135 | + if (e.key !== SETTINGS_KEY) return; |
| 136 | + setPageSize(loadSettings().pageSize); |
| 137 | + } |
| 138 | + |
| 139 | + window.addEventListener('storage', onStorage); |
| 140 | + return () => window.removeEventListener('storage', onStorage); |
| 141 | + }, []); |
| 142 | + |
128 | 143 | function toggleCategory(category: string) { |
129 | 144 | setActiveCategories((prev) => { |
130 | 145 | const next = new Set(prev); |
@@ -193,10 +208,10 @@ export function BrowseFilters({ entries }: BrowseFiltersProps) { |
193 | 208 | }); |
194 | 209 | }, [entries, search, activeCategories, activeRarities, sort]); |
195 | 210 |
|
196 | | - const totalPages = Math.max(1, Math.ceil(filtered.length / PAGE_SIZE)); |
| 211 | + const totalPages = Math.max(1, Math.ceil(filtered.length / pageSize)); |
197 | 212 | const clampedPage = Math.min(page, totalPages); |
198 | | - const pageStart = (clampedPage - 1) * PAGE_SIZE; |
199 | | - const paginated = filtered.slice(pageStart, pageStart + PAGE_SIZE); |
| 213 | + const pageStart = (clampedPage - 1) * pageSize; |
| 214 | + const paginated = filtered.slice(pageStart, pageStart + pageSize); |
200 | 215 | const hasFilters = isFiltered(search, activeCategories, activeRarities); |
201 | 216 |
|
202 | 217 | function clearAll() { |
@@ -332,7 +347,7 @@ export function BrowseFilters({ entries }: BrowseFiltersProps) { |
332 | 347 | > |
333 | 348 | <p style={{ color: 'var(--color-text-muted)' }} className="m-0 text-sm"> |
334 | 349 | <strong style={{ color: 'var(--color-text)' }}>{filtered.length}</strong> of {entries.length} entries |
335 | | - {filtered.length > PAGE_SIZE && ( |
| 350 | + {filtered.length > pageSize && ( |
336 | 351 | <span style={{ color: 'var(--color-text-faint)' }} className="ml-2"> |
337 | 352 | page {clampedPage} of {totalPages} |
338 | 353 | </span> |
|
0 commit comments