Skip to content

Commit 8b30d11

Browse files
committed
fix scrolling to prev page issue
1 parent d507019 commit 8b30d11

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/components/organisms/News.tsx

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, useRef } from 'react';
1+
import { FC, useEffect, useRef } from 'react';
22
import { Typography } from 'components/atoms';
33
import { Box, makeStyles } from '@material-ui/core';
44
import { useStore } from 'store/storeConfig';
@@ -41,23 +41,45 @@ const News: FC<InfiniteScrollProps> = ({ onScroll }) => {
4141
newsLoading: newsFlashStore.newsFlashLoading,
4242
});
4343

44+
const isNotFirstPage = newsFlashStore.newsFlashLastPrevPage > 1;
45+
const selectedItemIsFirst = newsId && newsFlashStore.newsFlashCollection.data[0]?.id === +newsId;
46+
const shouldShowSpacer = isNotFirstPage && selectedItemIsFirst;
47+
48+
useEffect(() => {
49+
if (shouldShowSpacer && containerRef.current) {
50+
// Use setTimeout to ensure DOM has updated
51+
setTimeout(() => {
52+
if (containerRef.current) {
53+
containerRef.current.scrollTop = 100;
54+
}
55+
}, 0);
56+
}
57+
}, [shouldShowSpacer, newsFlashStore.newsFlashCollection.data]);
58+
4459
return (
4560
<div ref={containerRef} className={classes.newsFeed}>
4661
{gpsId && <LocationSearchIndicator searchType={'gps'} />}
4762
{street && city && <LocationSearchIndicator searchType={'cityAndStreet'} />}
63+
{shouldShowSpacer && <div ref={firstItemRef} style={{ height: '100px', flexShrink: 0 }} />}
4864
{newsFlashStore.newsFlashCollection.data.length > 0 ? (
4965
newsFlashStore.newsFlashCollection.data.map((news, index) => {
5066
const isFirst = index === 0;
5167
const isLast = index === newsFlashStore.newsFlashCollection.data.length - 1;
5268
const selectedItem = news.id === +newsId ? selectedItemRef : undefined;
5369

5470
return (
55-
<div
56-
key={news.id}
57-
ref={combineRefs(isFirst ? firstItemRef : undefined, isLast ? lastItemRef : undefined, selectedItem)}
58-
>
59-
<NewsFlashComp news={news} />
60-
</div>
71+
<>
72+
<div
73+
key={news.id}
74+
ref={combineRefs(
75+
!shouldShowSpacer && isFirst ? firstItemRef : undefined,
76+
isLast ? lastItemRef : undefined,
77+
selectedItem,
78+
)}
79+
>
80+
<NewsFlashComp news={news} />
81+
</div>
82+
</>
6183
);
6284
})
6385
) : (

0 commit comments

Comments
 (0)