Skip to content

Commit 0a641ec

Browse files
committed
fix-category-navigation
1 parent 430ad17 commit 0a641ec

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/pages/News/NewsDetailPage.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ const NewsDetailPage: React.FC = () => {
8787

8888
const handleGoBack = useCallback(() => {
8989
const search = location.search || '';
90-
if (category) {
91-
navigate(`/news/${category}${search}`);
92-
} else {
93-
navigate(`/news/community-news${search}`);
94-
}
95-
}, [navigate, category, location.search]);
90+
// Prefer the category in the URL. If absent, derive from the post's category; fallback to 'all'.
91+
const derivedCategory = category
92+
? category
93+
: post?.category
94+
? post.category.toLowerCase().replace(/\s+/g, '-')
95+
: 'all';
96+
navigate(`/news/${derivedCategory}${search}`);
97+
}, [navigate, category, location.search, post?.category]);
9698

9799
const closeImageModal = useCallback(() => {
98100
setModalImage(null);

src/pages/News/NewsPage.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ const NewsPage: React.FC = () => {
6868
}, []);
6969

7070
useEffect(() => {
71+
// Don't decide active category until categories have been loaded
72+
if (!categories.length) return;
7173
if (categoryParam) {
7274
const formatted = categoryParam.toLowerCase().replace(/-/g, ' ').trim();
7375
const match = categories.find((cat) => cat.toLowerCase() === formatted);
@@ -87,14 +89,16 @@ const NewsPage: React.FC = () => {
8789
}, [location.search]);
8890

8991
useEffect(() => {
92+
// Wait until posts/categories load before syncing URL to avoid overwriting category from URL
93+
if (!categories.length) return;
9094
const pathCat =
9195
activeCategory === 'All'
9296
? 'all'
9397
: activeCategory.toLowerCase().replace(/\s+/g, '-');
9498
const query = searchTerm ? `?q=${encodeURIComponent(searchTerm)}` : '';
9599
navigate(`/news/${pathCat}${query}`, { replace: true });
96100
setDisplayCount(6);
97-
}, [activeCategory, navigate, searchTerm]);
101+
}, [activeCategory, navigate, searchTerm, categories.length]);
98102

99103
const sortedCategories = useMemo(() => {
100104
const others = categories

0 commit comments

Comments
 (0)