aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx')
-rw-r--r--FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx62
1 files changed, 62 insertions, 0 deletions
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;