CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/admin/system-notifications.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Map } from "immutable";
7
import { Alert, Button, Card, Input, Popconfirm, Space } from "antd";
8
import { Icon, Loading, Title } from "@cocalc/frontend/components";
9
import { plural } from "@cocalc/util/misc";
10
import { useState } from "react";
11
import { useActions, useRedux } from "@cocalc/frontend/app-framework";
12
13
export function SystemNotifications({}) {
14
const [state, setState] = useState<"view" | "edit">("view");
15
const [mesg, setMesg] = useState<string>("");
16
const notifications = useRedux("system_notifications", "notifications");
17
const actions = useActions("system_notifications");
18
19
function render_mark_done() {
20
if (!notifications) return;
21
let open = 0;
22
notifications.map((mesg: Map<string, any>) => {
23
if (mesg && !mesg.get("done")) {
24
open += 1;
25
}
26
});
27
if (open > 0) {
28
return (
29
<Button onClick={() => mark_all_done()}>
30
Mark {open} {plural(open, "Notification")} Done
31
</Button>
32
);
33
} else {
34
return <Button disabled={true}>No Outstanding Notifications</Button>;
35
}
36
}
37
38
function render_buttons() {
39
return (
40
<Space>
41
<Button
42
onClick={() => {
43
setState("edit");
44
setMesg("");
45
}}
46
>
47
Compose...
48
</Button>
49
{render_mark_done()}
50
</Space>
51
);
52
}
53
54
function render_editor() {
55
return (
56
<Card>
57
<Input.TextArea
58
autoFocus
59
value={mesg}
60
rows={3}
61
onChange={(e) => setMesg(e.target.value)}
62
/>
63
<Space style={{ marginTop: "15px" }}>
64
<Button onClick={() => setState("view")}>Cancel</Button>
65
<Popconfirm
66
title="Send notification?"
67
description={
68
<div style={{ width: "400px" }}>
69
Everyone that uses CoCalc will see the following notification
70
once in the upper right until you explicitly mark it done (they
71
can dismiss it).
72
<hr />
73
<Alert message={mesg} />
74
</div>
75
}
76
onConfirm={() => {
77
send();
78
}}
79
>
80
<Button danger>
81
<Icon name="paper-plane" /> Send
82
</Button>
83
</Popconfirm>
84
</Space>
85
</Card>
86
);
87
}
88
89
function send(): void {
90
setState("view");
91
if (!mesg) return;
92
actions.send_message({
93
text: mesg.trim(),
94
priority: "high",
95
});
96
}
97
98
function mark_all_done(): void {
99
actions.mark_all_done();
100
}
101
102
function render_body() {
103
if (notifications == null) {
104
return <Loading />;
105
}
106
switch (state) {
107
case "view":
108
return render_buttons();
109
case "edit":
110
return render_editor();
111
}
112
}
113
114
return (
115
<div>
116
<Title level={4}>System Notifications</Title>
117
{render_body()}
118
</div>
119
);
120
}
121
122