Skip to content

Commit

Permalink
refactor(hooks): make useViewed() reusable (#12067)
Browse files Browse the repository at this point in the history
* Add intersectionObserverOptions for useViewed

* fix(hooks): disable IntersectionObserver on view

* Revert "fix(hooks): disable IntersectionObserver on view"

This reverts commit 75918ea.

---------

Co-authored-by: Florian Dieminger <[email protected]>
  • Loading branch information
caugner and fiji-flo authored Nov 4, 2024
1 parent fdbd7ad commit c9377b7
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions client/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,18 @@ interface ViewedTimer {
timeout: number | null;
}

export function useViewed(callback: Function) {
const timer = useRef<ViewedTimer>({ timeout: null });
const isVisible = usePageVisibility();
const [node, setNode] = useState<HTMLElement>();
const isIntersecting = useIsIntersecting(node, {
export function useViewed(
callback: Function,
intersectionObserverOptions: IntersectionObserverInit = {
root: null,
rootMargin: "0px",
threshold: 0.5,
});
}
) {
const timer = useRef<ViewedTimer>({ timeout: null });
const isVisible = usePageVisibility();
const [node, setNode] = useState<HTMLElement>();
const isIntersecting = useIsIntersecting(node, intersectionObserverOptions);

useEffect(() => {
if (timer.current.timeout !== -1) {
Expand Down

0 comments on commit c9377b7

Please sign in to comment.