feat: use react-intersection-observer to check inview and set delay

This commit is contained in:
DIYgod
2024-05-30 02:32:09 +08:00
parent 737ebeaf61
commit 24e4911f53
3 changed files with 28 additions and 7 deletions
+1
View File
@@ -62,6 +62,7 @@
"lucide-react": "0.379.0",
"ofetch": "1.3.4",
"react-hook-form": "7.51.5",
"react-intersection-observer": "9.10.2",
"react-router-dom": "6.23.1",
"rehype-infer-description-meta": "2.0.0",
"rehype-parse": "9.0.0",
+18
View File
@@ -125,6 +125,9 @@ importers:
react-hook-form:
specifier: 7.51.5
version: 7.51.5(react@18.3.1)
react-intersection-observer:
specifier: 9.10.2
version: 9.10.2(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
react-router-dom:
specifier: 6.23.1
version: 6.23.1(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
@@ -4262,6 +4265,15 @@ packages:
peerDependencies:
react: ^16.8.0 || ^17 || ^18
react-intersection-observer@9.10.2:
resolution: {integrity: sha512-j2hGADK2hCbAlfaq6L3tVLb4iqngoN7B1fT16MwJ4J16YW/vWLcmAIinLsw0lgpZeMi4UDUWtHC9QDde0/P1yQ==}
peerDependencies:
react: ^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0
react-dom: ^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0
peerDependenciesMeta:
react-dom:
optional: true
react-refresh@0.14.2:
resolution: {integrity: sha512-jCvmsr+1IUSMUyzOkRcvnVbX3ZYC6g9TDrDbFuFmRDq7PD4yaGbLKNQL6k2jnArV8hjYxh7hVhAZB6s9HDGpZA==}
engines: {node: '>=0.10.0'}
@@ -9805,6 +9817,12 @@ snapshots:
dependencies:
react: 18.3.1
react-intersection-observer@9.10.2(react-dom@18.3.1(react@18.3.1))(react@18.3.1):
dependencies:
react: 18.3.1
optionalDependencies:
react-dom: 18.3.1(react@18.3.1)
react-refresh@0.14.2: {}
react-remove-scroll-bar@2.3.6(@types/react@18.3.3)(react@18.3.1):
@@ -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)