|
1 | | -import React, { useState, useMemo } from 'react'; |
| 1 | +import React, { useState, useMemo, useEffect } from 'react'; |
2 | 2 | import { motion, AnimatePresence } from 'framer-motion'; |
3 | 3 | import { Spinner, Button } from '@nextui-org/react'; |
4 | 4 | import { FiRefreshCw, FiAlertCircle } from 'react-icons/fi'; |
@@ -121,25 +121,30 @@ const ShowcaseListPage: React.FC<ShowcaseListPageProps> = ({ |
121 | 121 | }) => { |
122 | 122 | const location = useLocation(); |
123 | 123 | const navigate = useNavigate(); |
| 124 | + const searchParams = new URLSearchParams(location.search); |
124 | 125 |
|
125 | | - // 从 URL 参数获取 category,默认为 'all' |
126 | | - const getCategoryFromUrl = () => { |
127 | | - const params = new URLSearchParams(location.search); |
128 | | - return params.get('category') || 'all'; |
129 | | - }; |
130 | | - |
131 | | - const [activeCategory, setActiveCategory] = useState(getCategoryFromUrl); |
| 126 | + // Get category from URL params, default to 'all' |
| 127 | + const [activeCategory, setActiveCategory] = useState(searchParams.get('category') || 'all'); |
132 | 128 |
|
133 | | - // 当 category 改变时更新 URL |
| 129 | + // Update URL when category changes |
134 | 130 | const handleCategoryChange = (categoryId: string) => { |
135 | 131 | setActiveCategory(categoryId); |
136 | | - const params = new URLSearchParams(); |
137 | | - if (categoryId !== 'all') { |
138 | | - params.set('category', categoryId); |
| 132 | + const newSearchParams = new URLSearchParams(location.search); |
| 133 | + if (categoryId === 'all') { |
| 134 | + newSearchParams.delete('category'); |
| 135 | + } else { |
| 136 | + newSearchParams.set('category', categoryId); |
139 | 137 | } |
140 | | - const newPath = params.toString() ? `/showcase?${params.toString()}` : '/showcase'; |
141 | | - navigate(newPath, { replace: true }); |
| 138 | + navigate(`/showcase${newSearchParams.toString() ? `?${newSearchParams.toString()}` : ''}`, { replace: true }); |
142 | 139 | }; |
| 140 | + |
| 141 | + // Sync state with URL changes (for browser back/forward) |
| 142 | + useEffect(() => { |
| 143 | + const currentCategory = searchParams.get('category') || 'all'; |
| 144 | + if (currentCategory !== activeCategory) { |
| 145 | + setActiveCategory(currentCategory); |
| 146 | + } |
| 147 | + }, [location.search]); |
143 | 148 |
|
144 | 149 | const filteredItems = useMemo(() => { |
145 | 150 | return processedData?.getItemsByCategory(activeCategory) || []; |
@@ -404,7 +409,7 @@ const ShowcaseDetailPage: React.FC<ShowcaseDetailPageProps> = ({ |
404 | 409 | } |
405 | 410 |
|
406 | 411 | const handleBackToShowcase = () => { |
407 | | - // 从 referrer 中提取 category 参数 |
| 412 | + // Extract category parameter from referrer |
408 | 413 | const referrer = document.referrer; |
409 | 414 | if (referrer && referrer.includes('/showcase?category=')) { |
410 | 415 | const url = new URL(referrer); |
|
0 commit comments