From 180073b86867ad4eae06dec5671d8c64a9c76378 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 8 May 2021 09:56:03 +0800 Subject: fix: Try to fix scroll to top problem. --- .../timeline-common/TimelinePagedPostListView.tsx | 44 ++++++++++------------ 1 file changed, 20 insertions(+), 24 deletions(-) (limited to 'FrontEnd/src/app/views/timeline-common') diff --git a/FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx b/FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx index ee52f4c0..9c43434d 100644 --- a/FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx +++ b/FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx @@ -1,11 +1,14 @@ import React from "react"; import { fromEvent } from "rxjs"; -import { filter, throttleTime } from "rxjs/operators"; import { HttpTimelinePostInfo } from "@/http/timeline"; +import { + getReverseScrollPosition, + scrollToReverseScrollPosition, +} from "@/utilities/useReverseScrollPositionRemember"; + import TimelinePostListView from "./TimelinePostListView"; -import { setRecordDisabled } from "@/utilities/useReverseScrollPositionRemember"; export interface TimelinePagedPostListViewProps { className?: string; @@ -27,31 +30,24 @@ const TimelinePagedPostListView: React.FC = ( : posts.slice(-lastViewCount); }, [posts, lastViewCount]); - const scrollTopHandler = React.useRef<() => void>(); + const lastScrollPosition = React.useRef(null); React.useEffect(() => { - scrollTopHandler.current = () => { - if (lastViewCount < posts.length) { - setRecordDisabled(true); - setLastViewCount(lastViewCount + 10); - setTimeout(() => { - setRecordDisabled(false); - }, 500); - } - }; - }, [lastViewCount, posts]); - - React.useEffect(() => { - const subscription = fromEvent(window, "scroll") - .pipe( - filter(() => window.scrollY === 0), - throttleTime(800) - ) - .subscribe(() => { - scrollTopHandler.current?.(); + if (lastScrollPosition.current != null) { + scrollToReverseScrollPosition(lastScrollPosition.current); + lastScrollPosition.current = null; + } + + if (lastViewCount < posts.length) { + const subscription = fromEvent(window, "scroll").subscribe(() => { + if (window.scrollY === 0) { + lastScrollPosition.current = getReverseScrollPosition(); + setLastViewCount(lastViewCount + 10); + } }); - return () => subscription.unsubscribe(); - }, []); + return () => subscription.unsubscribe(); + } + }, [lastViewCount, posts]); return (