Skip to content

Commit 439d1ea

Browse files
committed
fix: address review comments - use English comments and improve react-router integration
1 parent ee01814 commit 439d1ea

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

  • multimodal/websites/docs/src/components/Showcase

multimodal/websites/docs/src/components/Showcase/index.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useMemo } from 'react';
1+
import React, { useState, useMemo, useEffect } from 'react';
22
import { motion, AnimatePresence } from 'framer-motion';
33
import { Spinner, Button } from '@nextui-org/react';
44
import { FiRefreshCw, FiAlertCircle } from 'react-icons/fi';
@@ -121,25 +121,30 @@ const ShowcaseListPage: React.FC<ShowcaseListPageProps> = ({
121121
}) => {
122122
const location = useLocation();
123123
const navigate = useNavigate();
124+
const searchParams = new URLSearchParams(location.search);
124125

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');
132128

133-
// 当 category 改变时更新 URL
129+
// Update URL when category changes
134130
const handleCategoryChange = (categoryId: string) => {
135131
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);
139137
}
140-
const newPath = params.toString() ? `/showcase?${params.toString()}` : '/showcase';
141-
navigate(newPath, { replace: true });
138+
navigate(`/showcase${newSearchParams.toString() ? `?${newSearchParams.toString()}` : ''}`, { replace: true });
142139
};
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]);
143148

144149
const filteredItems = useMemo(() => {
145150
return processedData?.getItemsByCategory(activeCategory) || [];
@@ -404,7 +409,7 @@ const ShowcaseDetailPage: React.FC<ShowcaseDetailPageProps> = ({
404409
}
405410

406411
const handleBackToShowcase = () => {
407-
// 从 referrer 中提取 category 参数
412+
// Extract category parameter from referrer
408413
const referrer = document.referrer;
409414
if (referrer && referrer.includes('/showcase?category=')) {
410415
const url = new URL(referrer);

0 commit comments

Comments
 (0)