|
1 | | -import React, { useState, useEffect, useMemo } from 'react'; |
| 1 | +import React, { useState, useEffect, useMemo, useLayoutEffect } from 'react'; |
2 | 2 | import ShareModal from '@/components/ShareModal'; |
3 | 3 | import { useNavigate, useParams, useLocation } from 'react-router-dom'; |
4 | 4 | import { getAllPosts, groupPostsByCategory, Post } from '@/utils/posts-utils'; |
@@ -49,6 +49,18 @@ const NewsPage: React.FC = () => { |
49 | 49 | return params.get('q') || ''; |
50 | 50 | }); |
51 | 51 |
|
| 52 | + useLayoutEffect(() => { |
| 53 | + if (!isLoading) { |
| 54 | + const savedY = Number(sessionStorage.getItem('newsScrollY') || 0); |
| 55 | + if (savedY > 0) { |
| 56 | + requestAnimationFrame(() => { |
| 57 | + window.scrollTo({ top: savedY, behavior: 'auto' }); |
| 58 | + }); |
| 59 | + } |
| 60 | + sessionStorage.removeItem('newsScrollY'); |
| 61 | + } |
| 62 | + }, [isLoading, activeCategory, searchTerm]); |
| 63 | + |
52 | 64 | useEffect(() => { |
53 | 65 | async function load() { |
54 | 66 | setIsLoading(true); |
@@ -127,7 +139,10 @@ const NewsPage: React.FC = () => { |
127 | 139 |
|
128 | 140 | const hasMore = filteredPosts.length > displayCount; |
129 | 141 |
|
130 | | - const handleCategoryClick = (cat: string) => setActiveCategory(cat); |
| 142 | + const handleCategoryClick = (cat: string) => { |
| 143 | + sessionStorage.setItem('newsScrollY', String(window.scrollY)); |
| 144 | + setActiveCategory(cat); |
| 145 | + }; |
131 | 146 |
|
132 | 147 | const handleShowMore = () => { |
133 | 148 | const total = filteredPosts.length; |
@@ -296,6 +311,10 @@ const NewsPage: React.FC = () => { |
296 | 311 | value={searchTerm} |
297 | 312 | onChange={(e) => { |
298 | 313 | const value = e.target.value; |
| 314 | + sessionStorage.setItem( |
| 315 | + 'newsScrollY', |
| 316 | + String(window.scrollY), |
| 317 | + ); |
299 | 318 | setSearchTerm(value); |
300 | 319 | const catPath = |
301 | 320 | activeCategory === 'All' |
|
0 commit comments