Skip to content

Commit e1ad554

Browse files
authored
Fixed preserve search query after navigating back from article (#411)
* Fixed preserve search query after navigating back from article * Fix: preserve search query when navigating back from article
1 parent 820082f commit e1ad554

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

src/pages/News/NewsDetailPage.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState, useEffect, useCallback } from 'react';
22
import ShareModal from '@/components/ShareModal';
33
import { Share2 } from 'lucide-react';
4-
import { useParams, useNavigate } from 'react-router-dom';
4+
import { useParams, useNavigate, useLocation } from 'react-router-dom';
55
import { getPostBySlug, Post } from '@/utils/posts-utils';
66
import { motion } from 'framer-motion';
77
import Header from '@/sections/Header';
@@ -13,6 +13,7 @@ const NewsDetailPage: React.FC = () => {
1313

1414
const { slug, category } = useParams<{ slug?: string; category?: string }>();
1515
const navigate = useNavigate();
16+
const location = useLocation();
1617
const [post, setPost] = useState<Post | null>(null);
1718
const [isLoading, setIsLoading] = useState<boolean>(true);
1819
const [error, setError] = useState<string | null>(null);
@@ -85,12 +86,13 @@ const NewsDetailPage: React.FC = () => {
8586
});
8687

8788
const handleGoBack = useCallback(() => {
89+
const search = location.search || '';
8890
if (category) {
89-
navigate(`/news/${category}`);
91+
navigate(`/news/${category}${search}`);
9092
} else {
91-
navigate('/news/community-news');
93+
navigate(`/news/community-news${search}`);
9294
}
93-
}, [navigate, category]);
95+
}, [navigate, category, location.search]);
9496

9597
const closeImageModal = useCallback(() => {
9698
setModalImage(null);

src/pages/News/NewsPage.tsx

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState, useEffect, useMemo } from 'react';
22
import ShareModal from '@/components/ShareModal';
3-
import { useNavigate, useParams } from 'react-router-dom';
3+
import { useNavigate, useParams, useLocation } from 'react-router-dom';
44
import { getAllPosts, groupPostsByCategory, Post } from '@/utils/posts-utils';
55
import Header from '@/sections/Header';
66
import Footer from '@/sections/Footer';
@@ -42,7 +42,11 @@ const NewsPage: React.FC = () => {
4242
const [viewMode, setViewMode] = useState<'grid' | 'list' | 'magazine'>(
4343
'grid',
4444
);
45-
const [searchTerm, setSearchTerm] = useState<string>('');
45+
const location = useLocation();
46+
const [searchTerm, setSearchTerm] = useState<string>(() => {
47+
const params = new URLSearchParams(location.search);
48+
return params.get('q') || '';
49+
});
4650

4751
useEffect(() => {
4852
async function load() {
@@ -74,14 +78,22 @@ const NewsPage: React.FC = () => {
7478
setActiveCategory('All');
7579
}, [categoryParam, categories]);
7680

81+
// Keep searchTerm in sync with the URL (?q=)
82+
useEffect(() => {
83+
const params = new URLSearchParams(location.search);
84+
const q = params.get('q') || '';
85+
setSearchTerm(q);
86+
}, [location.search]);
87+
7788
useEffect(() => {
7889
const pathCat =
7990
activeCategory === 'All'
8091
? 'all'
8192
: activeCategory.toLowerCase().replace(/\s+/g, '-');
82-
navigate(`/news/${pathCat}`, { replace: true });
93+
const query = searchTerm ? `?q=${encodeURIComponent(searchTerm)}` : '';
94+
navigate(`/news/${pathCat}${query}`, { replace: true });
8395
setDisplayCount(6);
84-
}, [activeCategory, navigate]);
96+
}, [activeCategory, navigate, searchTerm]);
8597

8698
const sortedCategories = useMemo(() => {
8799
const others = categories
@@ -124,7 +136,8 @@ const NewsPage: React.FC = () => {
124136
activeCategory === 'All'
125137
? 'all'
126138
: activeCategory.toLowerCase().replace(/\s+/g, '-');
127-
navigate(`/news/${catPath}/${slug}`);
139+
const query = searchTerm ? `?q=${encodeURIComponent(searchTerm)}` : '';
140+
navigate(`/news/${catPath}/${slug}${query}`);
128141
};
129142

130143
const handleShareClick = (post: Post, e: React.MouseEvent) => {
@@ -278,7 +291,18 @@ const NewsPage: React.FC = () => {
278291
type="text"
279292
placeholder="Search articles, topics, or categories..."
280293
value={searchTerm}
281-
onChange={(e) => setSearchTerm(e.target.value)}
294+
onChange={(e) => {
295+
const value = e.target.value;
296+
setSearchTerm(value);
297+
const catPath =
298+
activeCategory === 'All'
299+
? 'all'
300+
: activeCategory.toLowerCase().replace(/\s+/g, '-');
301+
const query = value
302+
? `?q=${encodeURIComponent(value)}`
303+
: '';
304+
navigate(`/news/${catPath}${query}`, { replace: true });
305+
}}
282306
className="w-full pl-10 pr-4 py-3 border border-gray-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-300"
283307
/>
284308
</div>
@@ -380,7 +404,14 @@ const NewsPage: React.FC = () => {
380404
<span>{filteredPosts.length} articles found</span>
381405
{searchTerm && (
382406
<button
383-
onClick={() => setSearchTerm('')}
407+
onClick={() => {
408+
setSearchTerm('');
409+
const catPath =
410+
activeCategory === 'All'
411+
? 'all'
412+
: activeCategory.toLowerCase().replace(/\s+/g, '-');
413+
navigate(`/news/${catPath}`, { replace: true });
414+
}}
384415
className="text-blue-600 hover:text-blue-700 underline text-sm"
385416
>
386417
Clear search

0 commit comments

Comments
 (0)