-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1165 from data-for-change/1051-bug-coordinate-the…
…-infographics-main-screen-with-the-newsflash-list Apply pagination to news list + scroll observer
- Loading branch information
Showing
18 changed files
with
283 additions
and
128 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Direction } from 'models/ScrollObserver.model'; | ||
import { useEffect, useRef } from 'react'; | ||
import { useInView } from 'react-intersection-observer'; | ||
|
||
interface IProps { | ||
newsId: string; | ||
containerRef: React.RefObject<HTMLDivElement>; | ||
newsData: any[]; | ||
newsLoading: boolean; | ||
onScroll: (direction: Direction) => void; | ||
} | ||
|
||
export const useScrollObserver = ({ newsId, onScroll, containerRef, newsData, newsLoading }: IProps) => { | ||
const selectedItemRef = useRef<HTMLDivElement>(null); | ||
const isFirstRender = useRef(true); | ||
const isInViewFirstRender = useRef(true); | ||
|
||
useEffect(() => { | ||
if (isFirstRender.current && newsId && newsData.length > 0 && !newsLoading) { | ||
const itemIndex = newsData.findIndex((item) => item.id.toString() === newsId); | ||
|
||
if (itemIndex !== -1) { | ||
requestAnimationFrame(() => { | ||
selectedItemRef.current?.scrollIntoView({ | ||
behavior: 'smooth', | ||
block: 'center', | ||
}); | ||
}); | ||
} | ||
isFirstRender.current = false; | ||
} | ||
}, [newsId, newsData, newsLoading]); | ||
|
||
const [firstItemRef] = useInView({ | ||
threshold: 0.1, | ||
delay: 100, | ||
onChange: (inView) => { | ||
if (isInViewFirstRender.current) { | ||
isInViewFirstRender.current = false; | ||
return; | ||
} | ||
if (inView && !newsLoading) { | ||
const container = containerRef.current; | ||
if (!container) return; | ||
|
||
const oldScrollHeight = container.scrollHeight; | ||
const oldScrollTop = container.scrollTop; | ||
|
||
// Create mutation observer before fetching | ||
const observer = new MutationObserver(() => { | ||
const newScrollHeight = container.scrollHeight; | ||
const heightDiff = newScrollHeight - oldScrollHeight; | ||
|
||
// Adjust scroll position to prevent jump | ||
container.scrollTop = oldScrollTop + heightDiff; | ||
observer.disconnect(); | ||
}); | ||
// Start observing before fetch | ||
observer.observe(container, { childList: true, subtree: true }); | ||
|
||
onScroll(Direction.PREV); | ||
isInViewFirstRender.current = true; | ||
} | ||
}, | ||
}); | ||
|
||
const [lastItemRef] = useInView({ | ||
threshold: 0.1, | ||
delay: 100, | ||
onChange: (inView) => { | ||
if (isInViewFirstRender.current) { | ||
isInViewFirstRender.current = false; | ||
return; | ||
} | ||
if (inView && !newsLoading) { | ||
onScroll(Direction.NEXT); | ||
isInViewFirstRender.current = true; | ||
} | ||
}, | ||
}); | ||
|
||
return { | ||
firstItemRef, | ||
lastItemRef, | ||
selectedItemRef, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum Direction { | ||
PREV = 'PREV', | ||
NEXT = 'NEXT', | ||
} |
Oops, something went wrong.