Skip to content

Commit eb0356c

Browse files
authored
fixed category navigation (#425)
* fix-category-navigation * removed unwanted comments
1 parent f038b69 commit eb0356c

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const NewsPage: React.FC = () => {
6868
}, []);
6969

7070
useEffect(() => {
71+
if (!categories.length) return;
7172
if (categoryParam) {
7273
const formatted = categoryParam.toLowerCase().replace(/-/g, ' ').trim();
7374
const match = categories.find((cat) => cat.toLowerCase() === formatted);
@@ -87,14 +88,15 @@ const NewsPage: React.FC = () => {
8788
}, [location.search]);
8889

8990
useEffect(() => {
91+
if (!categories.length) return;
9092
const pathCat =
9193
activeCategory === 'All'
9294
? 'all'
9395
: activeCategory.toLowerCase().replace(/\s+/g, '-');
9496
const query = searchTerm ? `?q=${encodeURIComponent(searchTerm)}` : '';
9597
navigate(`/news/${pathCat}${query}`, { replace: true });
9698
setDisplayCount(6);
97-
}, [activeCategory, navigate, searchTerm]);
99+
}, [activeCategory, navigate, searchTerm, categories.length]);
98100

99101
const sortedCategories = useMemo(() => {
100102
const others = categories

0 commit comments

Comments
 (0)