From 24e4911f531db0fde0c3e27be5009727b2bb112a Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 30 May 2024 02:32:09 +0800 Subject: [PATCH] feat: use react-intersection-observer to check inview and set delay --- package.json | 1 + pnpm-lock.yaml | 18 ++++++++++++++++++ .../components/entry-column/item-wrapper.tsx | 16 +++++++++------- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index e261439c0..cdc6edc33 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4c8f889d3..61f35710b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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): diff --git a/src/renderer/src/components/entry-column/item-wrapper.tsx b/src/renderer/src/components/entry-column/item-wrapper.tsx index e0a398276..7a3fcac15 100644 --- a/src/renderer/src/components/entry-column/item-wrapper.tsx +++ b/src/renderer/src/components/entry-column/item-wrapper.tsx @@ -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(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)