aboutsummaryrefslogtreecommitdiff
path: root/Timeline/ClientApp/src/app/home/BoardWithUser.tsx
blob: 3830104f37470391d05b8a3342eb59556b87f490 (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
import React from 'react';
import { Row, Col } from 'reactstrap';
import { useTranslation } from 'react-i18next';

import { UserWithToken } from '../data/user';
import { TimelineInfo } from '../data/timeline';

import { getHttpTimelineClient } from '../http/timeline';

import TimelineBoard from './TimelineBoard';
import OfflineBoard from './OfflineBoard';

const BoardWithUser: React.FC<{ user: UserWithToken }> = ({ user }) => {
  const { t } = useTranslation();

  const [ownTimelines, setOwnTimelines] = React.useState<
    TimelineInfo[] | 'offline' | 'loading'
  >('loading');
  const [joinTimelines, setJoinTimelines] = React.useState<
    TimelineInfo[] | 'offline' | 'loading'
  >('loading');

  React.useEffect(() => {
    let subscribe = true;
    if (ownTimelines === 'loading') {
      void getHttpTimelineClient()
        .listTimeline({ relate: user.username, relateType: 'own' })
        .then(
          (timelines) => {
            if (subscribe) {
              setOwnTimelines(timelines);
            }
          },
          () => {
            setOwnTimelines('offline');
          }
        );
    }
    return () => {
      subscribe = false;
    };
  }, [user, ownTimelines]);

  React.useEffect(() => {
    let subscribe = true;
    if (joinTimelines === 'loading') {
      void getHttpTimelineClient()
        .listTimeline({ relate: user.username, relateType: 'join' })
        .then(
          (timelines) => {
            if (subscribe) {
              setJoinTimelines(timelines);
            }
          },
          () => {
            setJoinTimelines('offline');
          }
        );
    }
    return () => {
      subscribe = false;
    };
  }, [user, joinTimelines]);

  return (
    <Row className="my-2 justify-content-center">
      {ownTimelines === 'offline' && joinTimelines === 'offline' ? (
        <Col className="py-2" sm="8" lg="6">
          <OfflineBoard
            onReload={() => {
              setOwnTimelines('loading');
              setJoinTimelines('loading');
            }}
          />
        </Col>
      ) : (
        <>
          <Col sm="6" lg="5" className="py-2">
            <TimelineBoard
              title={t('home.ownTimeline')}
              timelines={ownTimelines}
              onReload={() => {
                setOwnTimelines('loading');
              }}
            />
          </Col>
          <Col sm="6" lg="5" className="py-2">
            <TimelineBoard
              title={t('home.joinTimeline')}
              timelines={joinTimelines}
              onReload={() => {
                setJoinTimelines('loading');
              }}
            />
          </Col>
        </>
      )}
    </Row>
  );
};

export default BoardWithUser;