From 737ebeaf616693153ceb8b3ac0bdccfbf020b140 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 30 May 2024 02:00:22 +0800 Subject: [PATCH] feat: out of view to read --- .../src/components/entry-column/index.tsx | 27 ++--------------- .../components/entry-column/item-wrapper.tsx | 29 +++++++------------ src/renderer/src/hooks/usePrevious.ts | 9 ++++++ 3 files changed, 21 insertions(+), 44 deletions(-) create mode 100644 src/renderer/src/hooks/usePrevious.ts diff --git a/src/renderer/src/components/entry-column/index.tsx b/src/renderer/src/components/entry-column/index.tsx index 067976750..52a0f42cf 100644 --- a/src/renderer/src/components/entry-column/index.tsx +++ b/src/renderer/src/components/entry-column/index.tsx @@ -2,9 +2,8 @@ import { Tabs, TabsList, TabsTrigger } from "@renderer/components/ui/tabs" import { cn } from "@renderer/lib/utils" import { useEntries } from "@renderer/queries/entries" import { useFeedStore } from "@renderer/store" -import { useHover } from "@use-gesture/react" import { m } from "framer-motion" -import { useRef, useState } from "react" +import { useState } from "react" import { ArticleItem } from "./article-item" import { EntryItemWrapper } from "./item-wrapper" @@ -17,7 +16,6 @@ const gridMode = new Set([2, 3]) export function EntryColumn() { const [filterTab, setFilterTab] = useState("unread") - const [focused, setFocused] = useState(false) const activeList = useFeedStore((state) => state.activeList) const entries = useEntries({ @@ -27,26 +25,6 @@ export function EntryColumn() { ...(filterTab === "unread" && { read: false }), }) - const listRef = useRef(null) - let focuseTimer: NodeJS.Timeout | null = null - useHover( - (state) => { - if (state.active) { - focuseTimer = setTimeout(() => { - setFocused(true) - }, 1000) - } else { - if (focuseTimer) { - clearTimeout(focuseTimer) - } - setFocused(false) - } - }, - { - target: listRef, - }, - ) - let Item switch (activeList?.view) { case 0: { @@ -92,7 +70,7 @@ export function EntryColumn() { -
+
{entries.data?.pages?.map((page) => ( diff --git a/src/renderer/src/components/entry-column/item-wrapper.tsx b/src/renderer/src/components/entry-column/item-wrapper.tsx index 06c599402..e0a398276 100644 --- a/src/renderer/src/components/entry-column/item-wrapper.tsx +++ b/src/renderer/src/components/entry-column/item-wrapper.tsx @@ -1,4 +1,5 @@ import { useEntryActions } from "@renderer/hooks/useEntryActions" +import { usePrevious } from "@renderer/hooks/usePrevious" import { useUpdateEntry } from "@renderer/hooks/useUpdateEntry" import { showNativeMenu } from "@renderer/lib/native-menu" import type { EntriesResponse, EntryResponse } from "@renderer/lib/types" @@ -6,19 +7,17 @@ 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 { useHover } from "@use-gesture/react" -import { useEffect, useRef, useState } from "react" +import { useInView } from "framer-motion" +import { useEffect, useRef } from "react" export function EntryItemWrapper({ entry, children, view, - hoverToRead, }: { entry: EntriesResponse[number] | EntryResponse children: React.ReactNode view?: number - hoverToRead: boolean }) { const { items } = useEntryActions({ view, @@ -48,24 +47,13 @@ export function EntryItemWrapper({ }) const itemRef = useRef(null) - const [hovered, setHovered] = useState(false) - useHover( - (state) => { - if (hoverToRead && state.active) { - read.mutate() - } - setHovered(state.active) - }, - { - target: itemRef, - enabled: !entry.read, - }, - ) + const isInView = useInView(itemRef) + const prevIsInView = usePrevious(isInView) useEffect(() => { - if (hoverToRead && hovered) { + if (prevIsInView && !isInView && !entry.read) { read.mutate() } - }, [hoverToRead, hovered, read]) + }, [entry.read, isInView, read, prevIsInView]) if (!entry?.entries.url || view === undefined) return children @@ -81,6 +69,9 @@ export function EntryItemWrapper({ onClick={(e) => { e.stopPropagation() feedActions.setActiveEntry(entry.entries.id) + if (!entry.read) { + read.mutate() + } }} onDoubleClick={() => entry.entries.url && window.open(entry.entries.url, "_blank")} diff --git a/src/renderer/src/hooks/usePrevious.ts b/src/renderer/src/hooks/usePrevious.ts new file mode 100644 index 000000000..ef9c70b42 --- /dev/null +++ b/src/renderer/src/hooks/usePrevious.ts @@ -0,0 +1,9 @@ +import { useEffect, useRef } from "react" + +export const usePrevious = (value: T): T | undefined => { + const ref = useRef() + useEffect(() => { + ref.current = value + }) + return ref.current +}