aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/app/views/timeline-common/TimelinePageCardTemplate.tsx
blob: 921f139019c4f46ce9c50fc3a1c889ffad763c0e (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
import React from "react";
import clsx from "clsx";
import { useTranslation } from "react-i18next";
import { Dropdown, Button } from "react-bootstrap";

import { getHttpHighlightClient } from "@/http/highlight";
import { getHttpBookmarkClient } from "@/http/bookmark";

import { useUser } from "@/services/user";
import { pushAlert } from "@/services/alert";
import { timelineVisibilityTooltipTranslationMap } from "@/services/timeline";

import { TimelinePageCardProps } from "./TimelinePageTemplate";

import CollapseButton from "./CollapseButton";
import { TimelineMemberDialog } from "./TimelineMember";
import TimelinePropertyChangeDialog from "./TimelinePropertyChangeDialog";

export interface TimelineCardTemplateProps extends TimelinePageCardProps {
  infoArea: React.ReactElement;
  manageArea:
    | { type: "member" }
    | {
        type: "manage";
        items: (
          | {
              type: "button";
              text: string;
              color?: string;
              onClick: () => void;
            }
          | { type: "divider" }
        )[];
      };
  dialog: string | "property" | "member" | null;
  setDialog: (dialog: "property" | "member" | null) => void;
}

const TimelinePageCardTemplate: React.FC<TimelineCardTemplateProps> = ({
  timeline,
  collapse,
  toggleCollapse,
  infoArea,
  manageArea,
  onReload,
  className,
  dialog,
  setDialog,
}) => {
  const { t } = useTranslation();

  const user = useUser();

  return (
    <>
      <div className={clsx("cru-card p-2 clearfix", className)}>
        <div className="float-right d-flex align-items-center">
          <CollapseButton collapse={collapse} onClick={toggleCollapse} />
        </div>
        <div style={{ display: collapse ? "none" : "block" }}>
          {infoArea}
          <p className="mb-0">{timeline.description}</p>
          <small className="mt-1 d-block">
            {t(timelineVisibilityTooltipTranslationMap[timeline.visibility])}
          </small>
          <div className="text-right mt-2">
            <i
              className={clsx(
                timeline.isHighlight ? "bi-star-fill" : "bi-star",
                "icon-button text-yellow mr-3"
              )}
              onClick={
                user != null &&
                user.hasHighlightTimelineAdministrationPermission
                  ? () => {
                      getHttpHighlightClient()
                        [timeline.isHighlight ? "delete" : "put"](timeline.name)
                        .catch(() => {
                          pushAlert({
                            message: timeline.isHighlight
                              ? "timeline.removeHighlightFail"
                              : "timeline.addHighlightFail",
                            type: "danger",
                          });
                        });
                    }
                  : undefined
              }
            />
            {user != null ? (
              <i
                className={clsx(
                  timeline.isBookmark ? "bi-bookmark-fill" : "bi-bookmark",
                  "icon-button text-yellow mr-3"
                )}
                onClick={() => {
                  getHttpBookmarkClient()
                    [timeline.isBookmark ? "delete" : "put"](timeline.name)
                    .catch(() => {
                      pushAlert({
                        message: timeline.isBookmark
                          ? "timeline.removeBookmarkFail"
                          : "timeline.addBookmarkFail",
                        type: "danger",
                      });
                    });
                }}
              />
            ) : null}
            {manageArea.type === "manage" ? (
              <Dropdown className="d-inline-block">
                <Dropdown.Toggle variant="outline-primary">
                  {t("timeline.manage")}
                </Dropdown.Toggle>
                <Dropdown.Menu>
                  {manageArea.items.map((item, index) => {
                    if (item.type === "divider") {
                      return <Dropdown.Divider key={index} />;
                    } else {
                      return (
                        <Dropdown.Item
                          key={index}
                          onClick={item.onClick}
                          className={
                            item.color != null
                              ? "text-" + item.color
                              : undefined
                          }
                        >
                          {t(item.text)}
                        </Dropdown.Item>
                      );
                    }
                  })}
                </Dropdown.Menu>
              </Dropdown>
            ) : (
              <Button
                variant="outline-primary"
                onClick={() => setDialog("member")}
              >
                {t("timeline.memberButton")}
              </Button>
            )}
          </div>
        </div>
      </div>
      {(() => {
        if (dialog === "member") {
          return (
            <TimelineMemberDialog
              timeline={timeline}
              onClose={() => setDialog(null)}
              open
              onChange={onReload}
            />
          );
        } else if (dialog === "property") {
          return (
            <TimelinePropertyChangeDialog
              timeline={timeline}
              close={() => setDialog(null)}
              open
              onChange={onReload}
            />
          );
        }
      })()}
    </>
  );
};

export default TimelinePageCardTemplate;