diff options
author | crupest <crupest@outlook.com> | 2021-04-03 21:55:46 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-03 21:55:46 +0800 |
commit | 3ddbbd4c2719dbd6ba00d2d574a5d3f472e9d8ff (patch) | |
tree | 37c0b8957058f650d30456a9db3f780bf1ea5398 /FrontEnd/src | |
parent | d9a303d4e832dfcc7599ae750b8a4258f80ea677 (diff) | |
download | timeline-3ddbbd4c2719dbd6ba00d2d574a5d3f472e9d8ff.tar.gz timeline-3ddbbd4c2719dbd6ba00d2d574a5d3f472e9d8ff.tar.bz2 timeline-3ddbbd4c2719dbd6ba00d2d574a5d3f472e9d8ff.zip |
feat: Paged timeline posts.
Diffstat (limited to 'FrontEnd/src')
3 files changed, 67 insertions, 6 deletions
diff --git a/FrontEnd/src/app/views/timeline-common/Timeline.tsx b/FrontEnd/src/app/views/timeline-common/Timeline.tsx index 3f2cbfb5..9b67b90b 100644 --- a/FrontEnd/src/app/views/timeline-common/Timeline.tsx +++ b/FrontEnd/src/app/views/timeline-common/Timeline.tsx @@ -7,7 +7,7 @@ import { } from "@/http/common"; import { getHttpTimelineClient, HttpTimelinePostInfo } from "@/http/timeline"; -import TimelinePostListView from "./TimelinePostListView"; +import TimelinePagedPostListView from "./TimelinePagedPostListView"; export interface TimelineProps { className?: string; @@ -16,7 +16,6 @@ export interface TimelineProps { timelineName: string; reloadKey: number; onReload: () => void; - additionalPosts?: HttpTimelinePostInfo[]; onLoad?: () => void; } @@ -28,7 +27,6 @@ const Timeline: React.FC<TimelineProps> = (props) => { top, reloadKey, onReload, - additionalPosts, onLoad, } = props; @@ -77,7 +75,7 @@ const Timeline: React.FC<TimelineProps> = (props) => { if (Array.isArray(posts)) { onLoad?.(); } - }, [posts, additionalPosts, onLoad]); + }, [posts, onLoad]); switch (posts) { case "loading": @@ -112,8 +110,8 @@ const Timeline: React.FC<TimelineProps> = (props) => { ); default: return ( - <TimelinePostListView - posts={[...posts, ...(additionalPosts ?? [])]} + <TimelinePagedPostListView + posts={posts} top={top} onReload={onReload} /> diff --git a/FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx b/FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx new file mode 100644 index 00000000..b91357dd --- /dev/null +++ b/FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx @@ -0,0 +1,62 @@ +import React from "react"; + +import { HttpTimelinePostInfo } from "@/http/timeline"; +import TimelinePostListView from "./TimelinePostListView"; + +export interface TimelinePagedPostListViewProps { + className?: string; + style?: React.CSSProperties; + top?: string | number; + posts: HttpTimelinePostInfo[]; + onReload: () => void; +} + +const TimelinePagedPostListView: React.FC<TimelinePagedPostListViewProps> = ( + props +) => { + const { className, style, top, posts, onReload } = props; + + const [lastViewCount, setLastViewCount] = React.useState<number>(10); + + const viewingPosts = React.useMemo(() => { + if (lastViewCount >= posts.length) { + return posts; + } else { + return posts.slice(-lastViewCount, -1); + } + }, [posts, lastViewCount]); + + React.useEffect(() => { + if (lastViewCount < posts.length) { + const listener = (): void => { + if (window.scrollY === 0 && lastViewCount < posts.length) { + setLastViewCount(lastViewCount + 10); + } + }; + window.addEventListener("scroll", listener); + return () => window.removeEventListener("scroll", listener); + } + }, [lastViewCount, posts]); + + React.useEffect(() => { + if (lastViewCount !== 10) { + document + .getElementById( + `timeline-post-${posts[posts.length - (lastViewCount - 10)].id}` + ) + ?.scrollIntoView(true); + } + }, [lastViewCount, posts]); + + return ( + <TimelinePostListView + className={className} + style={style} + top={top} + posts={viewingPosts} + onReload={onReload} + /> + ); +}; + +export default TimelinePagedPostListView; diff --git a/FrontEnd/src/app/views/timeline-common/TimelinePostView.tsx b/FrontEnd/src/app/views/timeline-common/TimelinePostView.tsx index d01ecfa8..3d075f0e 100644 --- a/FrontEnd/src/app/views/timeline-common/TimelinePostView.tsx +++ b/FrontEnd/src/app/views/timeline-common/TimelinePostView.tsx @@ -31,6 +31,7 @@ const TimelinePostView: React.FC<TimelinePostViewProps> = (props) => { return ( <div + id={`timeline-post-${post.id}`} className={clsx("timeline-item", current && "current", className)} style={style} > |