aboutsummaryrefslogtreecommitdiff
path: root/Timeline/ClientApp/src/timeline/TimelineItem.tsx
blob: 7d488826bf970112cac77b6cab508913f0d3248c (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
import React from 'react';
import clsx from 'clsx';
import {
  Row,
  Col,
  Modal,
  ModalHeader,
  ModalBody,
  ModalFooter,
  Button,
} from 'reactstrap';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';

import { TimelinePostInfo } from '../data/timeline';
import { useAvatarUrlWithGivenVersion } from '../user/api';

const TimelinePostDeleteConfirmDialog: React.FC<{
  toggle: () => void;
  onConfirm: () => void;
}> = ({ toggle, onConfirm }) => {
  const { t } = useTranslation();

  return (
    <Modal toggle={toggle} isOpen centered>
      <ModalHeader className="text-danger">
        {t('timeline.post.deleteDialog.title')}
      </ModalHeader>
      <ModalBody>{t('timeline.post.deleteDialog.prompt')}</ModalBody>
      <ModalFooter>
        <Button color="secondary" onClick={toggle}>
          {t('operationDialog.cancel')}
        </Button>
        <Button
          color="danger"
          onClick={() => {
            onConfirm();
            toggle();
          }}
        >
          {t('operationDialog.confirm')}
        </Button>
      </ModalFooter>
    </Modal>
  );
};

export interface TimelineItemProps {
  post: TimelinePostInfo;
  current?: boolean;
  more?: {
    isOpen: boolean;
    toggle: () => void;
    onDelete: () => void;
  };
  onClick?: () => void;
  avatarVersion?: number;
}

const TimelineItem: React.FC<TimelineItemProps> = (props) => {
  const { i18n } = useTranslation();

  const current = props.current === true;

  const { more } = props;

  const avatarUrl = useAvatarUrlWithGivenVersion(
    props.avatarVersion,
    props.post.author._links.avatar
  );

  const [deleteDialog, setDeleteDialog] = React.useState<boolean>(false);
  const toggleDeleteDialog = React.useCallback(
    () => setDeleteDialog((old) => !old),
    []
  );

  return (
    <Row
      className={clsx('position-relative flex-nowrap', current && 'current')}
      onClick={props.onClick}
    >
      <Col className="timeline-line-area">
        <div className="timeline-line-segment start"></div>
        <div className="timeline-line-node-container">
          <div className="timeline-line-node"></div>
        </div>
        <div className="timeline-line-segment end"></div>
        {current && <div className="timeline-line-segment current-end" />}
      </Col>
      <Col className="timeline-pt-start">
        <Row className="flex-nowrap">
          <div className="col-auto flex-shrink-1 px-0">
            <Row className="ml-n3 mr-0 align-items-center">
              <span className="ml-3 text-primary white-space-no-wrap">
                {props.post.time.toLocaleString(i18n.languages)}
              </span>
              <small className="text-dark ml-3">
                {props.post.author.nickname}
              </small>
            </Row>
          </div>
          {more != null ? (
            <div className="col-auto px-2 d-flex justify-content-center align-items-center">
              <i
                className="fas fa-chevron-circle-down text-info icon-button"
                onClick={(e) => {
                  more.toggle();
                  e.stopPropagation();
                }}
              />
            </div>
          ) : null}
        </Row>
        <p className="row d-block timeline-content">
          <Link
            className="float-right float-sm-left mx-2"
            to={'/users/' + props.post.author.username}
          >
            <img src={avatarUrl} className="avatar rounded" />
          </Link>
          {(() => {
            const { content } = props.post;
            if (content.type === 'text') {
              return content.text;
            } else {
              return (
                <img src={content.url} className="timeline-content-image" />
              );
            }
          })()}
        </p>
      </Col>
      {more != null && more.isOpen ? (
        <>
          <div
            className="position-absolute position-lt w-100 h-100 mask d-flex justify-content-center align-items-center"
            onClick={more.toggle}
          >
            <i
              className="fas fa-trash text-danger large-icon"
              onClick={(e) => {
                toggleDeleteDialog();
                e.stopPropagation();
              }}
            />
          </div>
          {deleteDialog ? (
            <TimelinePostDeleteConfirmDialog
              toggle={() => {
                toggleDeleteDialog();
                more.toggle();
              }}
              onConfirm={more.onDelete}
            />
          ) : null}
        </>
      ) : null}
    </Row>
  );
};

export default TimelineItem;