From a63bb82935336a7f8efb5bc7e75cc2f6463d0a3c Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Tue, 11 Feb 2025 09:04:20 +0800 Subject: [PATCH] feat(mobile): entry list by feed or category; pagination (#2717) * feat(mobile): entry list pagination support, remove deprecated views * feat: entry list by feed * feat: support category * back title --- apps/mobile/src/constants/views.tsx | 16 --- .../modules/entry-list/entry-list-gird.tsx | 11 +- .../src/modules/entry-list/entry-list.tsx | 117 +++++++++++------- apps/mobile/src/modules/entry-list/index.tsx | 1 + apps/mobile/src/modules/feed-drawer/atoms.ts | 101 ++++++++++++--- .../src/modules/feed-drawer/feed-panel.tsx | 2 + .../src/modules/feed-drawer/view-selector.tsx | 13 +- .../modules/subscription/CategoryGrouped.tsx | 2 + .../modules/subscription/items/InboxItem.tsx | 4 +- .../items/ListSubscriptionItem.tsx | 4 +- .../subscription/items/SubscriptionItem.tsx | 2 + .../screens/(stack)/feeds/[feedId]/index.tsx | 42 ++----- apps/mobile/src/store/entry/hooks.ts | 20 ++- apps/mobile/src/store/entry/store.ts | 41 +++--- 14 files changed, 231 insertions(+), 145 deletions(-) diff --git a/apps/mobile/src/constants/views.tsx b/apps/mobile/src/constants/views.tsx index 18a61d48b..6a66c4e28 100644 --- a/apps/mobile/src/constants/views.tsx +++ b/apps/mobile/src/constants/views.tsx @@ -2,8 +2,6 @@ import { FeedViewType } from "@follow/constants" import type * as React from "react" import colors from "tailwindcss/colors" -import { AnnouncementCuteFiIcon } from "../icons/announcement_cute_fi" -import { MicCuteFiIcon } from "../icons/mic_cute_fi" import { PaperCuteFiIcon } from "../icons/paper_cute_fi" import { PicCuteFiIcon } from "../icons/pic_cute_fi" import { TwitterCuteFiIcon } from "../icons/twitter_cute_fi" @@ -48,18 +46,4 @@ export const views: ViewDefinition[] = [ translation: "title", view: FeedViewType.Videos, }, - { - name: "Audios", - icon: MicCuteFiIcon, - activeColor: colors.purple[500], - translation: "title", - view: FeedViewType.Audios, - }, - { - name: "Notifications", - icon: AnnouncementCuteFiIcon, - activeColor: colors.yellow[500], - translation: "title", - view: FeedViewType.Notifications, - }, ] diff --git a/apps/mobile/src/modules/entry-list/entry-list-gird.tsx b/apps/mobile/src/modules/entry-list/entry-list-gird.tsx index d33e548ed..f0ac71b64 100644 --- a/apps/mobile/src/modules/entry-list/entry-list-gird.tsx +++ b/apps/mobile/src/modules/entry-list/entry-list-gird.tsx @@ -16,7 +16,7 @@ import { ItemPressable } from "@/src/components/ui/pressable/item-pressable" import { useEntry } from "@/src/store/entry/hooks" import { debouncedFetchEntryContentByStream } from "@/src/store/entry/store" -import { useSelectedFeed } from "../feed-drawer/atoms" +import { useFetchEntriesControls, useSelectedView } from "../feed-drawer/atoms" export function EntryListContentGrid({ entryIds, @@ -28,6 +28,9 @@ export function EntryListContentGrid({ const tabBarHeight = useBottomTabBarHeight() const headerHeight = useHeaderHeight() const { scrollY } = useContext(NavigationContext)! + + const { fetchNextPage } = useFetchEntriesControls() + return ( { debouncedFetchEntryContentByStream(viewableItems.map((item) => item.key)) }} + onEndReached={() => { + fetchNextPage() + }} numColumns={2} onScroll={useTypeScriptHappyCallback( (e) => { @@ -60,8 +66,7 @@ export function EntryListContentGrid({ } function RenderEntryItem({ id }: { id: string }) { - const selectedFeed = useSelectedFeed() - const view = selectedFeed.type === "view" ? selectedFeed.viewId : null + const view = useSelectedView() const item = useEntry(id) if (!item) { return null diff --git a/apps/mobile/src/modules/entry-list/entry-list.tsx b/apps/mobile/src/modules/entry-list/entry-list.tsx index 02fa3b4b9..4d4218881 100644 --- a/apps/mobile/src/modules/entry-list/entry-list.tsx +++ b/apps/mobile/src/modules/entry-list/entry-list.tsx @@ -1,10 +1,11 @@ import { FeedViewType } from "@follow/constants" -import { useTypeScriptHappyCallback } from "@follow/hooks" -import { useBottomTabBarHeight } from "@react-navigation/bottom-tabs" +import { BottomTabBarHeightContext } from "@react-navigation/bottom-tabs" +import type { ListRenderItemInfo } from "@shopify/flash-list" import { FlashList } from "@shopify/flash-list" import { Image } from "expo-image" import { router } from "expo-router" import { useCallback, useContext, useMemo } from "react" +import type { NativeScrollEvent, NativeSyntheticEvent } from "react-native" import { StyleSheet, Text, useAnimatedValue, View } from "react-native" import { useSafeAreaInsets } from "react-native-safe-area-context" @@ -15,7 +16,12 @@ import { import { setWebViewEntry } from "@/src/components/native/webview/EntryContentWebView" import { ItemPressable } from "@/src/components/ui/pressable/item-pressable" import { useDefaultHeaderHeight } from "@/src/hooks/useDefaultHeaderHeight" -import { useSelectedFeed, useSelectedFeedTitle } from "@/src/modules/feed-drawer/atoms" +import { + useEntryListContext, + useFetchEntriesControls, + useSelectedFeedTitle, + useSelectedView, +} from "@/src/modules/feed-drawer/atoms" import { useEntry } from "@/src/store/entry/hooks" import { debouncedFetchEntryContentByStream } from "@/src/store/entry/store" @@ -28,29 +34,28 @@ const headerHideableBottomHeight = 58 export function EntryListScreen({ entryIds }: { entryIds: string[] }) { const scrollY = useAnimatedValue(0) - const selectedFeed = useSelectedFeed() - const view = selectedFeed.type === "view" ? selectedFeed.viewId : null + const view = useSelectedView() const viewTitle = useSelectedFeedTitle() + const screenType = useEntryListContext().type return ( ({ scrollY }), [scrollY])}> ( - - ), - [], + () => (screenType === "timeline" ? : null), + [screenType], )} headerRight={useCallback( - () => ( - - ), - [], + () => (screenType === "timeline" ? : null), + [screenType], )} - headerHideableBottomHeight={headerHideableBottomHeight} - headerHideableBottom={ViewSelector} + headerHideableBottomHeight={ + screenType === "timeline" ? headerHideableBottomHeight : undefined + } + headerHideableBottom={screenType === "timeline" ? ViewSelector : undefined} /> {view === FeedViewType.Pictures || view === FeedViewType.Videos ? ( @@ -61,42 +66,60 @@ export function EntryListScreen({ entryIds }: { entryIds: string[] }) { ) } -function EntryListContent({ entryIds }: { entryIds: string[] }) { +export function EntryListContent({ entryIds }: { entryIds: string[] }) { + const screenType = useEntryListContext().type + const insets = useSafeAreaInsets() - const tabBarHeight = useBottomTabBarHeight() + const originalDefaultHeaderHeight = useDefaultHeaderHeight() - const headerHeight = originalDefaultHeaderHeight + headerHideableBottomHeight - const { scrollY } = useContext(NavigationContext)! + const headerHeight = + screenType === "timeline" + ? originalDefaultHeaderHeight + headerHideableBottomHeight + : originalDefaultHeaderHeight + const scrollY = useContext(NavigationContext)?.scrollY + + const { fetchNextPage, isFetchingNextPage } = useFetchEntriesControls() + + const onScroll = useCallback( + (e: NativeSyntheticEvent) => { + scrollY?.setValue(e.nativeEvent.contentOffset.y) + }, + [scrollY], + ) + + const renderItem = useCallback( + ({ item: id }: ListRenderItemInfo) => , + [], + ) + return ( - { - scrollY.setValue(e.nativeEvent.contentOffset.y) - }, - [scrollY], + + {(tabBarHeight) => ( + id} + onEndReached={() => { + fetchNextPage() + }} + onViewableItemsChanged={({ viewableItems }) => { + debouncedFetchEntryContentByStream(viewableItems.map((item) => item.key)) + }} + scrollIndicatorInsets={{ + top: headerHeight - insets.top, + bottom: tabBarHeight ? tabBarHeight - insets.bottom : undefined, + }} + estimatedItemSize={100} + contentContainerStyle={{ + paddingTop: headerHeight, + paddingBottom: tabBarHeight, + }} + ItemSeparatorComponent={ItemSeparator} + ListFooterComponent={isFetchingNextPage ? : null} + /> )} - data={entryIds} - renderItem={useTypeScriptHappyCallback( - ({ item: id }) => ( - - ), - [], - )} - keyExtractor={(id) => id} - onViewableItemsChanged={({ viewableItems }) => { - debouncedFetchEntryContentByStream(viewableItems.map((item) => item.key)) - }} - scrollIndicatorInsets={{ - top: headerHeight - insets.top, - bottom: tabBarHeight - insets.bottom, - }} - estimatedItemSize={100} - contentContainerStyle={{ - paddingTop: headerHeight, - paddingBottom: tabBarHeight, - }} - ItemSeparatorComponent={ItemSeparator} - /> + ) } diff --git a/apps/mobile/src/modules/entry-list/index.tsx b/apps/mobile/src/modules/entry-list/index.tsx index eb29a68b7..6d8054670 100644 --- a/apps/mobile/src/modules/entry-list/index.tsx +++ b/apps/mobile/src/modules/entry-list/index.tsx @@ -25,6 +25,7 @@ export function EntryList() { }, [setDrawerSwipeDisabled, isFocused]) const selectedFeed = useSelectedFeed() + if (!selectedFeed) return null switch (selectedFeed.type) { case "view": { diff --git a/apps/mobile/src/modules/feed-drawer/atoms.ts b/apps/mobile/src/modules/feed-drawer/atoms.ts index 411c747d1..e7a1466dd 100644 --- a/apps/mobile/src/modules/feed-drawer/atoms.ts +++ b/apps/mobile/src/modules/feed-drawer/atoms.ts @@ -1,7 +1,7 @@ import { FeedViewType } from "@follow/constants" import { jotaiStore } from "@follow/utils" import { atom, useAtom, useAtomValue, useSetAtom } from "jotai" -import { useCallback, useMemo } from "react" +import { createContext, useCallback, useContext, useMemo } from "react" import { views } from "@/src/constants/views" import { usePrefetchEntries } from "@/src/store/entry/hooks" @@ -11,6 +11,7 @@ import { useFeed } from "@/src/store/feed/hooks" import { useInbox } from "@/src/store/inbox/hooks" import { useList } from "@/src/store/list/hooks" import { getSubscriptionByCategory } from "@/src/store/subscription/getter" +import { useSubscription } from "@/src/store/subscription/hooks" // drawer open state @@ -64,25 +65,17 @@ export function useSelectedCollection() { export const selectCollection = (state: CollectionPanelState) => { jotaiStore.set(collectionPanelStateAtom, state) if (state.type === "view" || state.type === "list") { - jotaiStore.set(selectedFeedAtom, state) + jotaiStore.set(selectedTimelineAtom, state) } } // feed panel selected state -export type SelectedFeed = +export type SelectedTimeline = | { type: "view" viewId: FeedViewType } - | { - type: "feed" - feedId: string - } - | { - type: "category" - categoryName: string - } | { type: "list" listId: string @@ -92,13 +85,55 @@ export type SelectedFeed = inboxId: string } -const selectedFeedAtom = atom({ +export type SelectedFeed = + | { + type: "feed" + feedId: string + } + | { + type: "category" + categoryName: string + } + | null + +const selectedTimelineAtom = atom({ type: "view", viewId: FeedViewType.Articles, }) -export function useSelectedFeed() { +const selectedFeedAtom = atom(null) + +export const EntryListContext = createContext<{ type: "timeline" | "feed" }>({ type: "timeline" }) +export const useEntryListContext = () => { + return useContext(EntryListContext) +} + +export function useSelectedView() { + const selectedTimeLine = useAtomValue(selectedTimelineAtom) const selectedFeed = useAtomValue(selectedFeedAtom) + + const list = useList(selectedTimeLine.type === "list" ? selectedTimeLine.listId : "") + const subscription = useSubscription( + selectedFeed && selectedFeed.type === "feed" ? selectedFeed.feedId : "", + ) + const view = + selectedTimeLine.type === "view" + ? selectedTimeLine.viewId + : selectedTimeLine.type === "list" + ? list?.view + : selectedFeed?.type === "feed" + ? subscription?.view + : undefined + return view +} + +function getFetchEntryPayload( + selectedFeed: SelectedTimeline | SelectedFeed, +): FetchEntriesProps | null { + if (!selectedFeed) { + return null + } + let payload: FetchEntriesProps = {} switch (selectedFeed.type) { case "view": { @@ -123,16 +158,42 @@ export function useSelectedFeed() { } // No default } + return payload +} + +export function useSelectedFeed() { + const entryListContext = useEntryListContext() + + const selectedTimeline = useAtomValue(selectedTimelineAtom) + const selectedFeed = useAtomValue(selectedFeedAtom) + + const payload = getFetchEntryPayload( + entryListContext.type === "feed" ? selectedFeed : selectedTimeline, + ) usePrefetchEntries(payload) - return selectedFeed + + return entryListContext.type === "feed" ? selectedFeed : selectedTimeline +} + +export function useFetchEntriesControls() { + const selectedFeed = useSelectedFeed() + const payload = getFetchEntryPayload(selectedFeed) + return usePrefetchEntries(payload) } export const useSelectedFeedTitle = () => { const selectedFeed = useSelectedFeed() - const viewDef = useViewDefinition(selectedFeed.type === "view" ? selectedFeed.viewId : undefined) - const feed = useFeed(selectedFeed.type === "feed" ? selectedFeed.feedId : "") - const list = useList(selectedFeed.type === "list" ? selectedFeed.listId : "") - const inbox = useInbox(selectedFeed.type === "inbox" ? selectedFeed.inboxId : "") + + const viewDef = useViewDefinition( + selectedFeed && selectedFeed.type === "view" ? selectedFeed.viewId : undefined, + ) + const feed = useFeed(selectedFeed && selectedFeed.type === "feed" ? selectedFeed.feedId : "") + const list = useList(selectedFeed && selectedFeed.type === "list" ? selectedFeed.listId : "") + const inbox = useInbox(selectedFeed && selectedFeed.type === "inbox" ? selectedFeed.inboxId : "") + + if (!selectedFeed) { + return "" + } switch (selectedFeed.type) { case "view": { @@ -153,6 +214,10 @@ export const useSelectedFeedTitle = () => { } } +export const selectTimeline = (state: SelectedTimeline) => { + jotaiStore.set(selectedTimelineAtom, state) +} + export const selectFeed = (state: SelectedFeed) => { jotaiStore.set(selectedFeedAtom, state) } diff --git a/apps/mobile/src/modules/feed-drawer/feed-panel.tsx b/apps/mobile/src/modules/feed-drawer/feed-panel.tsx index c6ab8bf36..38cd32a8e 100644 --- a/apps/mobile/src/modules/feed-drawer/feed-panel.tsx +++ b/apps/mobile/src/modules/feed-drawer/feed-panel.tsx @@ -1,6 +1,7 @@ import { cn } from "@follow/utils" import { BottomTabBarHeightContext } from "@react-navigation/bottom-tabs" import { HeaderHeightContext } from "@react-navigation/elements" +import { router } from "expo-router" import type { FC } from "react" import { createContext, memo, useContext, useState } from "react" import { @@ -155,6 +156,7 @@ const CategoryGrouped = memo( categoryName: category, }) closeDrawer() + router.push(`/feeds/${category}`) }} className="h-12 flex-row items-center px-3" > diff --git a/apps/mobile/src/modules/feed-drawer/view-selector.tsx b/apps/mobile/src/modules/feed-drawer/view-selector.tsx index 79f059c19..0781fa424 100644 --- a/apps/mobile/src/modules/feed-drawer/view-selector.tsx +++ b/apps/mobile/src/modules/feed-drawer/view-selector.tsx @@ -9,7 +9,7 @@ import { useList } from "@/src/store/list/hooks" import { useAllListSubscription } from "@/src/store/subscription/hooks" import { useColor } from "@/src/theme/colors" -import { selectFeed, useSelectedFeed } from "../feed-drawer/atoms" +import { selectTimeline, useSelectedFeed } from "../feed-drawer/atoms" export function ViewSelector() { const lists = useAllListSubscription() @@ -33,15 +33,15 @@ export function ViewSelector() { } function ViewItem({ view }: { view: ViewDefinition }) { - const selectedFeed = useSelectedFeed() - const isActive = selectedFeed.type === "view" && selectedFeed.viewId === view.view - const bgColor = useColor("gray2") + const selectedFeed = useSelectedFeed() + if (!selectedFeed) return null + const isActive = selectedFeed.type === "view" && selectedFeed.viewId === view.view return ( selectFeed({ type: "view", viewId: view.view })} + onPress={() => selectTimeline({ type: "view", viewId: view.view })} style={{ backgroundColor: isActive ? view.activeColor : bgColor, }} @@ -54,6 +54,7 @@ function ViewItem({ view }: { view: ViewDefinition }) { function ListItem({ listId }: { listId: string }) { const list = useList(listId) const selectedFeed = useSelectedFeed() + if (!selectedFeed) return null const isActive = selectedFeed.type === "list" && selectedFeed.listId === listId if (!list) return null @@ -61,7 +62,7 @@ function ListItem({ listId }: { listId: string }) { return ( selectFeed({ type: "list", listId })} + onPress={() => selectTimeline({ type: "list", listId })} > {list.image ? ( isActive ? ( diff --git a/apps/mobile/src/modules/subscription/CategoryGrouped.tsx b/apps/mobile/src/modules/subscription/CategoryGrouped.tsx index fe993ac7f..bde8253ba 100644 --- a/apps/mobile/src/modules/subscription/CategoryGrouped.tsx +++ b/apps/mobile/src/modules/subscription/CategoryGrouped.tsx @@ -1,3 +1,4 @@ +import { router } from "expo-router" import { memo, useState } from "react" import { Text, TouchableOpacity } from "react-native" import Animated, { useAnimatedStyle, useSharedValue, withSpring } from "react-native-reanimated" @@ -49,6 +50,7 @@ export const CategoryGrouped = memo( categoryName: category, }) closeDrawer() + router.push(`/feeds/${category}`) }} className="h-12 flex-row items-center px-3" > diff --git a/apps/mobile/src/modules/subscription/items/InboxItem.tsx b/apps/mobile/src/modules/subscription/items/InboxItem.tsx index a65fd52eb..0c986855b 100644 --- a/apps/mobile/src/modules/subscription/items/InboxItem.tsx +++ b/apps/mobile/src/modules/subscription/items/InboxItem.tsx @@ -9,7 +9,7 @@ import { useSubscription } from "@/src/store/subscription/hooks" import { getInboxStoreId } from "@/src/store/subscription/utils" import { useUnreadCount } from "@/src/store/unread/hooks" -import { closeDrawer, selectFeed } from "../../feed-drawer/atoms" +import { closeDrawer, selectTimeline } from "../../feed-drawer/atoms" export const InboxItem = memo(({ id }: { id: string }) => { const subscription = useSubscription(getInboxStoreId(id)) @@ -21,7 +21,7 @@ export const InboxItem = memo(({ id }: { id: string }) => { { - selectFeed({ type: "inbox", inboxId: id }) + selectTimeline({ type: "inbox", inboxId: id }) closeDrawer() }} > diff --git a/apps/mobile/src/modules/subscription/items/ListSubscriptionItem.tsx b/apps/mobile/src/modules/subscription/items/ListSubscriptionItem.tsx index 24eb824a8..2062671eb 100644 --- a/apps/mobile/src/modules/subscription/items/ListSubscriptionItem.tsx +++ b/apps/mobile/src/modules/subscription/items/ListSubscriptionItem.tsx @@ -8,7 +8,7 @@ import { useList } from "@/src/store/list/hooks" import { useUnreadCount } from "@/src/store/unread/hooks" import { SubscriptionListItemContextMenu } from "../../context-menu/lists" -import { closeDrawer, selectFeed } from "../../feed-drawer/atoms" +import { closeDrawer, selectTimeline } from "../../feed-drawer/atoms" export const ListSubscriptionItem = memo(({ id }: { id: string; className?: string }) => { const list = useList(id) @@ -20,7 +20,7 @@ export const ListSubscriptionItem = memo(({ id }: { id: string; className?: stri { - selectFeed({ + selectTimeline({ type: "list", listId: id, }) diff --git a/apps/mobile/src/modules/subscription/items/SubscriptionItem.tsx b/apps/mobile/src/modules/subscription/items/SubscriptionItem.tsx index 3adf2d4d9..77e040718 100644 --- a/apps/mobile/src/modules/subscription/items/SubscriptionItem.tsx +++ b/apps/mobile/src/modules/subscription/items/SubscriptionItem.tsx @@ -1,4 +1,5 @@ import { cn } from "@follow/utils" +import { router } from "expo-router" import { memo, useContext } from "react" import { ActivityIndicator, Text, View } from "react-native" import Animated, { FadeOutUp } from "react-native-reanimated" @@ -92,6 +93,7 @@ export const SubscriptionItem = memo(({ id, className }: { id: string; className feedId: id, }) closeDrawer() + router.push(`/feeds/${id}`) }} > diff --git a/apps/mobile/src/screens/(stack)/feeds/[feedId]/index.tsx b/apps/mobile/src/screens/(stack)/feeds/[feedId]/index.tsx index 81704ad8e..67ed5045c 100644 --- a/apps/mobile/src/screens/(stack)/feeds/[feedId]/index.tsx +++ b/apps/mobile/src/screens/(stack)/feeds/[feedId]/index.tsx @@ -1,37 +1,19 @@ -import { Stack, useLocalSearchParams } from "expo-router" -import { ScrollView, Text, View } from "react-native" +import { useLocalSearchParams } from "expo-router" +import { useMemo } from "react" -import { BlurEffect } from "@/src/components/common/BlurEffect" +import { EntryListScreen } from "@/src/modules/entry-list/entry-list" +import { EntryListContext } from "@/src/modules/feed-drawer/atoms" +import { useEntryIdsByCategory, useEntryIdsByFeedId } from "@/src/store/entry/hooks" export default function Feed() { - const { feedId } = useLocalSearchParams() - + const { feedId: feedIdOrCategory } = useLocalSearchParams() + const entryIdsByFeedId = useEntryIdsByFeedId(feedIdOrCategory as string) + const entryIdsByCategory = useEntryIdsByCategory(feedIdOrCategory as string) return ( - - ({ type: "feed" }), [])}> + 0 ? entryIdsByFeedId : entryIdsByCategory} /> - - Feed {feedId} - Feed {feedId} - Feed {feedId} - Feed {feedId} - Feed {feedId} - Feed {feedId} - Feed {feedId} - Feed {feedId} - Feed {feedId} - Feed {feedId} - Feed {feedId} - Feed {feedId} - - + ) } diff --git a/apps/mobile/src/store/entry/hooks.ts b/apps/mobile/src/store/entry/hooks.ts index 2fd8560fe..13f415313 100644 --- a/apps/mobile/src/store/entry/hooks.ts +++ b/apps/mobile/src/store/entry/hooks.ts @@ -1,16 +1,24 @@ import type { FeedViewType } from "@follow/constants" -import { useMutation, useQuery } from "@tanstack/react-query" +import { useInfiniteQuery, useMutation, useQuery } from "@tanstack/react-query" import { useCallback, useEffect } from "react" import { getEntry } from "./getter" import { entrySyncServices, useEntryStore } from "./store" import type { EntryModel, FetchEntriesProps } from "./types" -export const usePrefetchEntries = (props: FetchEntriesProps) => { - const { feedId, inboxId, listId, view, read, limit, pageParam, isArchived } = props - return useQuery({ - queryKey: ["entries", feedId, inboxId, listId, view, read, limit, pageParam, isArchived], - queryFn: () => entrySyncServices.fetchEntries(props), +export const usePrefetchEntries = (props: Omit | null) => { + const { feedId, inboxId, listId, view, read, limit, isArchived } = props || {} + return useInfiniteQuery({ + queryKey: ["entries", feedId, inboxId, listId, view, read, limit, isArchived], + queryFn: ({ pageParam }) => entrySyncServices.fetchEntries({ ...props, pageParam }), + getNextPageParam: (lastPage) => + inboxId || listId + ? lastPage.data?.at(-1)?.entries.insertedAt + : lastPage.data?.at(-1)?.entries.publishedAt, + initialPageParam: undefined as undefined | string, + refetchOnWindowFocus: false, + refetchOnReconnect: false, + enabled: !!props, }) } export const usePrefetchEntryContent = (entryId: string) => { diff --git a/apps/mobile/src/store/entry/store.ts b/apps/mobile/src/store/entry/store.ts index 8d94d439d..00278415e 100644 --- a/apps/mobile/src/store/entry/store.ts +++ b/apps/mobile/src/store/entry/store.ts @@ -45,7 +45,6 @@ const defaultState: EntryState = { export const useEntryStore = createZustandStore("entry")(() => defaultState) -const set = useEntryStore.setState const immerSet = createImmerSetter(useEntryStore) class EntryActions { @@ -155,19 +154,16 @@ class EntryActions { await tx.run() } - reset(entries: EntryModel[] = []) { - if (entries.length > 0) { - immerSet((draft) => { - // remove all entries from draft.data not in entries - for (const existingEntry of Object.values(draft.data)) { - if (!entries.some((e) => e.id === existingEntry.id)) { - delete draft.data[existingEntry.id] - } - } - }) - } else { - set(defaultState) - } + resetByView({ view, entries }: { view: FeedViewType; entries: EntryModel[] }) { + immerSet((draft) => { + draft.entryIdByView[view] = new Set(entries.map((e) => e.id)) + }) + } + + resetByCategory({ category, entries }: { category: Category; entries: EntryModel[] }) { + immerSet((draft) => { + draft.entryIdByCategory[category] = new Set(entries.map((e) => e.id)) + }) } } @@ -206,7 +202,22 @@ class EntrySyncServices { entryIds: entries.map((e) => e.id), }) } - return entries + + // After initial fetch, we can reset the state to prefer the entries data from the server + if (!pageParam) { + if (view !== undefined) { + entryActions.resetByView({ view, entries }) + } + + if (params.feedIdList && params.feedIdList.length > 0) { + const category = getSubscription(params.feedIdList[0]!)?.category + if (category) { + entryActions.resetByCategory({ category, entries }) + } + } + } + + return res } async fetchEntryContent(entryId: EntryId) {