Skip to content

Commit 69c8c3c

Browse files
authored
Blog page Tab Improvement. (#1288)
* blog page fix * Update index.page.tsx * Update index.page.tsx * Update index.page.tsx * Update index.page.tsx
1 parent 4ecd4b8 commit 69c8c3c

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

pages/blog/index.page.tsx

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const PATH = 'pages/blog/posts';
99
import TextTruncate from 'react-text-truncate';
1010
import generateRssFeed from './generateRssFeed';
1111
import { useRouter } from 'next/router';
12-
import useSetUrlParam from '~/lib/useSetUrlParam';
1312
import { SectionContext } from '~/context';
1413
import Image from 'next/image';
1514

@@ -76,7 +75,6 @@ export default function StaticMarkdownPage({
7675
filterTag: any;
7776
}) {
7877
const router = useRouter();
79-
const setParam = useSetUrlParam();
8078
const [currentFilterTag, setCurrentFilterTag] = useState<blogCategories>(
8179
filterTag || 'All',
8280
);
@@ -93,36 +91,17 @@ export default function StaticMarkdownPage({
9391
setCurrentFilterTag(filterTag);
9492
}, [filterTag]);
9593

96-
const handleClick = (event: { currentTarget: { value: any } }) => {
97-
const clickedTag = event.currentTarget.value;
98-
setParam('type', clickedTag);
99-
94+
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
95+
event.preventDefault(); // Prevent default scrolling behavior
96+
const clickedTag = event.currentTarget.value as blogCategories;
10097
if (clickedTag === 'All') {
101-
setParam('type', null);
102-
} else {
98+
setCurrentFilterTag('All');
99+
history.replaceState(null, '', '/blog'); // Update the URL without causing a scroll
100+
} else if (isValidCategory(clickedTag)) {
103101
setCurrentFilterTag(clickedTag);
104-
}
105-
106-
// Check if the user is already on the "/blog" page
107-
if (router.pathname === '/blog') {
108-
if (router.query.type) {
109-
// Remove the 'type' query parameter from the URL
110-
setParam(
111-
'type',
112-
router.query.type === clickedTag ? undefined : clickedTag,
113-
);
114-
}
115-
setCurrentFilterTag(clickedTag);
116-
} else {
117-
// If not on the "/blog" page, navigate to the "/blog" page with the type tag as a query parameter
118-
router.replace(
119-
{ pathname: '/blog', query: { type: clickedTag } },
120-
undefined,
121-
{ shallow: true },
122-
);
102+
history.replaceState(null, '', `/blog?type=${clickedTag}`); // Update URL
123103
}
124104
};
125-
126105
const recentBlog = blogPosts.sort((a, b) => {
127106
const dateA = new Date(a.frontmatter.date).getTime();
128107
const dateB = new Date(b.frontmatter.date).getTime();
@@ -283,10 +262,18 @@ export default function StaticMarkdownPage({
283262
onClick={(e) => {
284263
e.preventDefault();
285264
e.stopPropagation();
286-
setParam('type', frontmatter.type);
265+
266+
if (frontmatter.type) {
267+
setCurrentFilterTag(frontmatter.type);
268+
history.replaceState(
269+
null,
270+
'',
271+
`/blog?type=${frontmatter.type}`,
272+
);
273+
}
287274
}}
288275
>
289-
{frontmatter.type}
276+
{frontmatter.type || 'Unknown Type'}
290277
</div>
291278
</div>
292279
<div className='text-lg font-semibold'>

0 commit comments

Comments
 (0)