aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx
blob: e05346956194d8f066e34d63db744c7e4c6fd6c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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]);

  return (
    <TimelinePostListView
      className={className}
      style={style}
      top={top}
      posts={viewingPosts}
      onReload={onReload}
    />
  );
};

export default TimelinePagedPostListView;