aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/app/views/timeline-common/TimelinePageTemplate.tsx
blob: baaad1395489fe16f0ecb8b09454a74ec6b5de6e (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React from "react";
import { useTranslation } from "react-i18next";
import { Container } from "react-bootstrap";

import { HttpNetworkError, HttpNotFoundError } from "@/http/common";
import { getHttpTimelineClient, HttpTimelineInfo } from "@/http/timeline";

import { getAlertHost } from "@/services/alert";

import Timeline from "./Timeline";
import TimelinePostEdit from "./TimelinePostEdit";

import useReverseScrollPositionRemember from "@/utilities/useReverseScrollPositionRemember";
import { generatePalette, setPalette } from "@/palette";

export interface TimelinePageCardProps {
  timeline: HttpTimelineInfo;
  collapse: boolean;
  toggleCollapse: () => void;
  className?: string;
  onReload: () => void;
}

export interface TimelinePageTemplateProps {
  timelineName: string;
  notFoundI18nKey: string;
  reloadKey: number;
  onReload: () => void;
  CardComponent: React.ComponentType<TimelinePageCardProps>;
}

const TimelinePageTemplate: React.FC<TimelinePageTemplateProps> = (props) => {
  const { timelineName, reloadKey, onReload, CardComponent } = props;

  const { t } = useTranslation();

  const [state, setState] = React.useState<
    "loading" | "done" | "offline" | "notexist" | "error"
  >("loading");
  const [timeline, setTimeline] = React.useState<HttpTimelineInfo | null>(null);

  useReverseScrollPositionRemember();

  React.useEffect(() => {
    setState("loading");
    setTimeline(null);
  }, [timelineName]);

  React.useEffect(() => {
    let subscribe = true;
    void getHttpTimelineClient()
      .getTimeline(timelineName)
      .then(
        (data) => {
          if (subscribe) {
            setState("done");
            setTimeline(data);
          }
        },
        (error) => {
          if (subscribe) {
            if (error instanceof HttpNetworkError) {
              setState("offline");
            } else if (error instanceof HttpNotFoundError) {
              setState("notexist");
            } else {
              console.error(error);
              setState("error");
            }
            setTimeline(null);
          }
        }
      );
    return () => {
      subscribe = false;
    };
  }, [timelineName, reloadKey]);

  React.useEffect(() => {
    if (timeline != null && timeline.color != null) {
      return setPalette(generatePalette({ primary: timeline.color }));
    }
  }, [timeline]);

  const [bottomSpaceHeight, setBottomSpaceHeight] = React.useState<number>(0);

  const [timelineReloadKey, setTimelineReloadKey] = React.useState<number>(0);

  const reloadTimeline = (): void => {
    setTimelineReloadKey((old) => old + 1);
  };

  const onPostEditHeightChange = React.useCallback((height: number): void => {
    setBottomSpaceHeight(height);
    if (height === 0) {
      const alertHost = getAlertHost();
      if (alertHost != null) {
        alertHost.style.removeProperty("margin-bottom");
      }
    } else {
      const alertHost = getAlertHost();
      if (alertHost != null) {
        alertHost.style.marginBottom = `${height}px`;
      }
    }
  }, []);

  const cardCollapseLocalStorageKey = `timeline.${timelineName}.cardCollapse`;

  const [cardCollapse, setCardCollapse] = React.useState<boolean>(true);
  React.useEffect(() => {
    const savedCollapse =
      window.localStorage.getItem(cardCollapseLocalStorageKey) === "true";
    setCardCollapse(savedCollapse);
  }, [cardCollapseLocalStorageKey]);

  const toggleCardCollapse = (): void => {
    const newState = !cardCollapse;
    setCardCollapse(newState);
    window.localStorage.setItem(
      cardCollapseLocalStorageKey,
      newState.toString()
    );
  };

  return (
    <>
      {timeline != null ? (
        <CardComponent
          className="timeline-template-card"
          timeline={timeline}
          collapse={cardCollapse}
          toggleCollapse={toggleCardCollapse}
          onReload={onReload}
        />
      ) : null}
      <Container
        className="px-0"
        style={{
          minHeight: `calc(100vh - ${56 + bottomSpaceHeight}px)`,
        }}
      >
        {(() => {
          if (state === "offline") {
            // TODO: i18n
            return <p className="text-danger">Offline!</p>;
          } else if (state === "notexist") {
            return <p className="text-danger">{t(props.notFoundI18nKey)}</p>;
          } else if (state === "error") {
            // TODO: i18n
            return <p className="text-danger">Error!</p>;
          } else {
            return (
              <Timeline
                timelineName={timeline?.name}
                reloadKey={timelineReloadKey}
                onReload={reloadTimeline}
              />
            );
          }
        })()}
      </Container>
      {timeline != null && timeline.postable ? (
        <>
          <div
            style={{ height: bottomSpaceHeight }}
            className="flex-fix-length"
          />
          <TimelinePostEdit
            className="fixed-bottom"
            timeline={timeline}
            onHeightChange={onPostEditHeightChange}
            onPosted={reloadTimeline}
          />
        </>
      ) : null}
    </>
  );
};

export default TimelinePageTemplate;