feat: use react-intersection-observer to check inview and set delay
This commit is contained in:
@@ -7,8 +7,8 @@ import { cn } from "@renderer/lib/utils"
|
||||
import { apiFetch } from "@renderer/queries/api-fetch"
|
||||
import { feedActions, useFeedStore } from "@renderer/store"
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { useInView } from "framer-motion"
|
||||
import { useEffect, useRef } from "react"
|
||||
import { useInView } from "react-intersection-observer"
|
||||
|
||||
export function EntryItemWrapper({
|
||||
entry,
|
||||
@@ -46,14 +46,16 @@ export function EntryItemWrapper({
|
||||
},
|
||||
})
|
||||
|
||||
const itemRef = useRef<HTMLDivElement>(null)
|
||||
const isInView = useInView(itemRef)
|
||||
const prevIsInView = usePrevious(isInView)
|
||||
const { ref, inView } = useInView({
|
||||
threshold: 1,
|
||||
delay: 1000,
|
||||
})
|
||||
const prevInView = usePrevious(inView)
|
||||
useEffect(() => {
|
||||
if (prevIsInView && !isInView && !entry.read) {
|
||||
if (prevInView && !inView && !entry.read) {
|
||||
read.mutate()
|
||||
}
|
||||
}, [entry.read, isInView, read, prevIsInView])
|
||||
}, [entry.read, inView, read, prevInView])
|
||||
|
||||
if (!entry?.entries.url || view === undefined) return children
|
||||
|
||||
@@ -65,7 +67,7 @@ export function EntryItemWrapper({
|
||||
activeEntry === entry.entries.id && "bg-native-active/50",
|
||||
entry.read && "text-foreground/80",
|
||||
)}
|
||||
ref={itemRef}
|
||||
ref={ref}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
feedActions.setActiveEntry(entry.entries.id)
|
||||
|
||||
Reference in New Issue
Block a user