From 06e3848b48af72b87948f739ef2d464e12b94af1 Mon Sep 17 00:00:00 2001 From: teodorus-nathaniel Date: Mon, 29 Jan 2024 20:27:55 +0700 Subject: [PATCH] fix: scroll restoration for feed --- src/components/activity/InnerActivities.tsx | 28 +++++++++----------- src/components/activity/MyFeed.tsx | 29 ++++++++++++++++++--- src/components/activity/types.ts | 2 ++ 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/components/activity/InnerActivities.tsx b/src/components/activity/InnerActivities.tsx index 43fb1c206..d81717010 100644 --- a/src/components/activity/InnerActivities.tsx +++ b/src/components/activity/InnerActivities.tsx @@ -41,21 +41,19 @@ export function InnerActivities(props: InnerActivitiesProps) { const loadMoreFn = (page: number, size: number) => loadMore({ subsocial, dispatch, address, myAddress, page, size, ids }) - const List = useCallback( - () => - loading ? ( - - ) : ( - - ), - [loading, address, total, totalCount], - ) + const List = useCallback(() => { + return loading ? ( + + ) : ( + + ) + }, [loading, address, total, totalCount]) return } diff --git a/src/components/activity/MyFeed.tsx b/src/components/activity/MyFeed.tsx index 024bb93ff..29d643077 100644 --- a/src/components/activity/MyFeed.tsx +++ b/src/components/activity/MyFeed.tsx @@ -7,30 +7,53 @@ import NotAuthorized from '../auth/NotAuthorized' import WriteSomething from '../posts/WriteSomething' import Section from '../utils/Section' import { createLoadMorePosts, FeedActivities, onchainLoadMorePosts } from './FeedActivities' -import { BaseActivityProps } from './types' +import { BaseActivityProps, LoadMoreProps } from './types' const enableOnchainActivities = config.enableOnchainActivities const loadingLabel = 'Loading your feed...' +const sessionPageAndDataMap: Map< + string, + { initialPage: number; dataSource: Record } +> = new Map() +const getSessionKey = ({ address }: { address: string | undefined }) => address ?? '' const InnerMyFeed = (props: BaseActivityProps) => { + const { address } = props const getNewsFeeds = useGetNewsFeeds() const offchainLoadMoreFeed = createLoadMorePosts(getNewsFeeds) const getNewsFeedsCount = useGetNewsFeedsCount() const feedPostIds = useAppSelector(state => - enableOnchainActivities ? selectFeedByAccount(state, props.address) : [], + enableOnchainActivities ? selectFeedByAccount(state, address) : [], ) + const currentSessionKey = getSessionKey({ address }) const loadMoreFn = enableOnchainActivities ? onchainLoadMorePosts : offchainLoadMoreFeed + const augmentedLoadMoreFn = async (props: LoadMoreProps) => { + const res = await loadMoreFn(props) + let { dataSource } = sessionPageAndDataMap.get(currentSessionKey) || {} + if (!dataSource) dataSource = {} + dataSource[props.page] = res + + sessionPageAndDataMap.set(currentSessionKey, { + initialPage: props.page + 1, + dataSource, + }) + return res + } + + const currentSessionData = sessionPageAndDataMap.get(currentSessionKey) return ( = BaseActivityProps & { + initialPage?: number + dataSource?: T[] loadMore: (props: LoadMoreProps) => Promise getCount?: GetCountFn noDataDesc?: string