aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/app/views/timeline-common/TimelinePagedPostListView.tsx
blob: ee52f4c0426dc5663bbcea4a798d2f3ce29dea3a (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
54
55
56
57
58
59
60
61
62
63
64
65
66
import React from "react";
import { fromEvent } from "rxjs";
import { filter, throttleTime } from "rxjs/operators";

import { HttpTimelinePostInfo } from "@/http/timeline";

import TimelinePostListView from "./TimelinePostListView";
import { setRecordDisabled } from "@/utilities/useReverseScrollPositionRemember";

export interface TimelinePagedPostListViewProps {
  className?: string;
  style?: React.CSSProperties;
  posts: HttpTimelinePostInfo[];
  onReload: () => void;
}

const TimelinePagedPostListView: React.FC<TimelinePagedPostListViewProps> = (
  props
) => {
  const { className, style, posts, onReload } = props;

  const [lastViewCount, setLastViewCount] = React.useState<number>(10);

  const viewingPosts = React.useMemo(() => {
    return lastViewCount >= posts.length
      ? posts.slice()
      : posts.slice(-lastViewCount);
  }, [posts, lastViewCount]);

  const scrollTopHandler = React.useRef<() => void>();

  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?.();
      });
    return () => subscription.unsubscribe();
  }, []);

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

export default TimelinePagedPostListView;