|
1 |
| -import { FC, useRef } from 'react'; |
| 1 | +import { FC, useEffect, useRef } from 'react'; |
2 | 2 | import { Typography } from 'components/atoms';
|
3 | 3 | import { Box, makeStyles } from '@material-ui/core';
|
4 | 4 | import { useStore } from 'store/storeConfig';
|
@@ -41,23 +41,45 @@ const News: FC<InfiniteScrollProps> = ({ onScroll }) => {
|
41 | 41 | newsLoading: newsFlashStore.newsFlashLoading,
|
42 | 42 | });
|
43 | 43 |
|
| 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 | + |
44 | 59 | return (
|
45 | 60 | <div ref={containerRef} className={classes.newsFeed}>
|
46 | 61 | {gpsId && <LocationSearchIndicator searchType={'gps'} />}
|
47 | 62 | {street && city && <LocationSearchIndicator searchType={'cityAndStreet'} />}
|
| 63 | + {shouldShowSpacer && <div ref={firstItemRef} style={{ height: '100px', flexShrink: 0 }} />} |
48 | 64 | {newsFlashStore.newsFlashCollection.data.length > 0 ? (
|
49 | 65 | newsFlashStore.newsFlashCollection.data.map((news, index) => {
|
50 | 66 | const isFirst = index === 0;
|
51 | 67 | const isLast = index === newsFlashStore.newsFlashCollection.data.length - 1;
|
52 | 68 | const selectedItem = news.id === +newsId ? selectedItemRef : undefined;
|
53 | 69 |
|
54 | 70 | 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 | + </> |
61 | 83 | );
|
62 | 84 | })
|
63 | 85 | ) : (
|
|
0 commit comments