From d7f4bb6b9f388849565a7d8fc21d2eafc0610d08 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 3 Sep 2024 23:58:21 +0800 Subject: [PATCH] feat: load archived entries --- src/hono.ts | 2 + .../src/components/ui/button/variants.tsx | 2 +- .../src/modules/entry-column/hooks.ts | 9 +++- .../src/modules/entry-column/index.tsx | 49 ++++++++++++++++--- src/renderer/src/queries/entries.ts | 14 +++--- src/renderer/src/store/entry/store.ts | 3 ++ 6 files changed, 63 insertions(+), 16 deletions(-) diff --git a/src/hono.ts b/src/hono.ts index 5600f139a..759be7ff9 100644 --- a/src/hono.ts +++ b/src/hono.ts @@ -3279,6 +3279,8 @@ declare const _routes: hono_hono_base.HonoBase { scrollMarkUnread, ]) } -export const useEntriesByView = ({ onReset }: { onReset?: () => void }) => { +export const useEntriesByView = ({ + onReset, + isArchived, +}: { + onReset?: () => void + isArchived?: boolean +}) => { const routeParams = useRouteParms() const unreadOnly = useGeneralSettingKey("unreadOnly") @@ -71,6 +77,7 @@ export const useEntriesByView = ({ onReset }: { onReset?: () => void }) => { id: folderIds?.join(",") || feedId, view, ...(unreadOnly === true && { read: false }), + isArchived, } const query = useEntries(entriesOptions) diff --git a/src/renderer/src/modules/entry-column/index.tsx b/src/renderer/src/modules/entry-column/index.tsx index 31a68e97a..fdc7544b4 100644 --- a/src/renderer/src/modules/entry-column/index.tsx +++ b/src/renderer/src/modules/entry-column/index.tsx @@ -1,8 +1,10 @@ +import { useGeneralSettingKey } from "@renderer/atoms/settings/general" import { useUISettingKey } from "@renderer/atoms/settings/ui" import { m } from "@renderer/components/common/Motion" import { FeedFoundCanBeFollowError } from "@renderer/components/errors/FeedFoundCanBeFollowErrorFallback" import { FeedNotFound } from "@renderer/components/errors/FeedNotFound" import { AutoResizeHeight } from "@renderer/components/ui/auto-resize-height" +import { Button } from "@renderer/components/ui/button" import { LoadingCircle } from "@renderer/components/ui/loading" import { ScrollArea } from "@renderer/components/ui/scroll-area" import { @@ -22,7 +24,7 @@ import { useFeed } from "@renderer/queries/feed" import { entryActions, useEntry } from "@renderer/store/entry" import { useFeedByIdSelector } from "@renderer/store/feed" import { useSubscriptionByFeedId } from "@renderer/store/subscription" -import { useCallback, useEffect, useRef } from "react" +import { useCallback, useEffect, useRef, useState } from "react" import type { ScrollSeekConfiguration, VirtuosoGridProps, @@ -44,12 +46,15 @@ const scrollSeekConfiguration: ScrollSeekConfiguration = { } export function EntryColumn() { const virtuosoRef = useRef(null) + const [isArchived, setIsArchived] = useState(false) + const unreadOnly = useGeneralSettingKey("unreadOnly") const entries = useEntriesByView({ onReset: useCallback(() => { virtuosoRef.current?.scrollTo({ top: 0, }) }, []), + isArchived, }) const { entriesIds, isFetchingNextPage, groupedCounts } = entries @@ -79,14 +84,41 @@ export function EntryColumn() { const handleMarkReadInRange = useEntryMarkReadHandler(entriesIds) + useEffect(() => { + if (isArchived) { + if (entries.hasNextPage) { + entries.fetchNextPage() + } else { + entries.refetch() + } + } + }, [isArchived]) + + const showArchivedButton = !isArchived && !unreadOnly && !isCollection && routeFeedId !== ROUTE_FEED_PENDING && entries.totalCount < 40 const scrollRef = useRef(null) const virtuosoOptions = { components: { List: EntryListContent, Footer: useCallback(() => { - if (!isFetchingNextPage) return null - return - }, [isFetchingNextPage, view]), + if (!isFetchingNextPage) { + if (showArchivedButton) { + return ( +
+ +
+ ) + } else { + return null + } + } else { + return + } + }, [isFetchingNextPage, view, unreadOnly, isArchived, entries]), ScrollSeekPlaceholder: useCallback( () => , [view], @@ -102,8 +134,11 @@ export function EntryColumn() { totalCount: entries.totalCount, endReached: useCallback(async () => { - if (!entries.isFetchingNextPage && entries.hasNextPage) { - await entries.fetchNextPage() + if (!entries.isFetchingNextPage) { + const remaining = entries.data?.pages.at(-1)?.remaining + if (entries.hasNextPage && remaining) { + await entries.fetchNextPage() + } } }, [entries]), data: entriesIds, @@ -169,7 +204,7 @@ export function EntryColumn() { rootClassName="h-full" viewportClassName="[&>div]:grow flex" > - {virtuosoOptions.totalCount === 0 ? ( + {virtuosoOptions.totalCount === 0 && !showArchivedButton ? ( entries.isLoading ? null : ( diff --git a/src/renderer/src/queries/entries.ts b/src/renderer/src/queries/entries.ts index c274e01e6..7a1c2a1ee 100644 --- a/src/renderer/src/queries/entries.ts +++ b/src/renderer/src/queries/entries.ts @@ -11,11 +11,13 @@ export const entries = { view, read, limit, + isArchived, }: { id?: number | string view?: number read?: boolean limit?: number + isArchived?: boolean }) => defineQuery( ["entries", id, view, read, limit], @@ -26,6 +28,7 @@ export const entries = { read, limit, pageParam: pageParam as string, + isArchived, }), { rootKey: ["entries", id], @@ -109,19 +112,16 @@ export const useEntries = ({ id, view, read, + isArchived, }: { id?: number | string view?: number read?: boolean + isArchived?: boolean }) => - useAuthInfiniteQuery(entries.entries({ id, view, read }), { + useAuthInfiniteQuery(entries.entries({ id, view, read, isArchived }), { enabled: id !== undefined, - getNextPageParam: (lastPage) => { - if (!lastPage.data?.length || lastPage.remaining === 0) { - return null - } - return lastPage.data.at(-1)!.entries.publishedAt - }, + getNextPageParam: (lastPage) => lastPage.data?.at(-1)?.entries.publishedAt, initialPageParam: undefined, refetchOnWindowFocus: false, refetchOnReconnect: false, diff --git a/src/renderer/src/store/entry/store.ts b/src/renderer/src/store/entry/store.ts index 3c40c150d..54e81fe9a 100644 --- a/src/renderer/src/store/entry/store.ts +++ b/src/renderer/src/store/entry/store.ts @@ -85,18 +85,21 @@ class EntryActions { read, limit, pageParam, + isArchived, }: { id?: number | string view?: number read?: boolean limit?: number pageParam?: string + isArchived?: boolean }) { const data = await apiClient.entries.$post({ json: { publishedAfter: pageParam, read, limit, + isArchived, // withContent: true, ...getEntriesParams({ id,