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/app/connection-indicator.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 { useIntl } from "react-intl";
7
8
import {
9
CSS,
10
React,
11
useActions,
12
useTypedRedux,
13
} from "@cocalc/frontend/app-framework";
14
import { Icon } from "@cocalc/frontend/components";
15
import { labels } from "@cocalc/frontend/i18n";
16
import track from "@cocalc/frontend/user-tracking";
17
import { COLORS } from "@cocalc/util/theme";
18
import {
19
FONT_SIZE_ICONS_NORMAL,
20
PageStyle,
21
TOP_BAR_ELEMENT_CLASS,
22
} from "./top-nav-consts";
23
import { blur_active_element } from "./util";
24
25
interface Props {
26
height: number; // px
27
pageStyle: PageStyle;
28
on_click?: () => void;
29
}
30
31
const BASE_STYLE: CSS = {
32
fontSize: FONT_SIZE_ICONS_NORMAL,
33
display: "inline",
34
color: COLORS.GRAY_M,
35
} as const;
36
37
export const ConnectionIndicator: React.FC<Props> = React.memo(
38
(props: Props) => {
39
const { on_click, height, pageStyle } = props;
40
const { topPaddingIcons, sidePaddingIcons, fontSizeIcons } = pageStyle;
41
42
const intl = useIntl();
43
const connection_status = useTypedRedux("page", "connection_status");
44
const mesg_info = useTypedRedux("account", "mesg_info");
45
const actions = useActions("page");
46
47
const connecting_style: CSS = {
48
flex: "1",
49
color: "white",
50
overflow: "hidden",
51
margin: "auto 0",
52
} as const;
53
54
const outer_style: CSS = {
55
flex: "0 0 auto",
56
display: "flex",
57
alignItems: "center",
58
color: COLORS.GRAY_M,
59
cursor: "pointer",
60
height: `${height}px`,
61
padding: `${topPaddingIcons} ${sidePaddingIcons}`,
62
...(connection_status !== "connected"
63
? {
64
backgroundColor:
65
connection_status === "disconnected"
66
? COLORS.ANTD_RED_WARN
67
: COLORS.ORANGE_WARN,
68
}
69
: {}),
70
} as const;
71
72
function render_connection_status() {
73
if (connection_status === "connected") {
74
const icon_style: CSS = { ...BASE_STYLE, fontSize: fontSizeIcons };
75
if (mesg_info?.get("enqueued") ?? 0 > 6) {
76
// serious backlog of data!
77
icon_style.color = "red";
78
} else if (mesg_info?.get("count") ?? 0 > 2) {
79
// worrisome amount
80
icon_style.color = "#08e";
81
} else if (mesg_info?.get("count") ?? 0 > 0) {
82
// working well but doing something minimal
83
icon_style.color = "#00c";
84
}
85
return <Icon name="wifi" style={icon_style} />;
86
} else if (connection_status === "connecting") {
87
return (
88
<div style={connecting_style}>
89
{intl.formatMessage(labels.connecting)}...
90
</div>
91
);
92
} else if (connection_status === "disconnected") {
93
return (
94
<div style={connecting_style}>
95
{intl.formatMessage(labels.disconnected)}
96
</div>
97
);
98
}
99
}
100
101
function connection_click() {
102
actions.show_connection(true);
103
if (typeof on_click === "function") {
104
on_click();
105
}
106
blur_active_element(); // otherwise, it'll be highlighted even when closed again
107
track("top_nav", { name: "connection" });
108
}
109
110
return (
111
<div
112
className={TOP_BAR_ELEMENT_CLASS}
113
style={outer_style}
114
onClick={connection_click}
115
>
116
{render_connection_status()}
117
</div>
118
);
119
},
120
);
121
122