Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/app/connection-indicator.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { useIntl } from "react-intl";67import {8CSS,9React,10useActions,11useTypedRedux,12} from "@cocalc/frontend/app-framework";13import { Icon } from "@cocalc/frontend/components";14import { labels } from "@cocalc/frontend/i18n";15import track from "@cocalc/frontend/user-tracking";16import { COLORS } from "@cocalc/util/theme";17import {18FONT_SIZE_ICONS_NORMAL,19PageStyle,20TOP_BAR_ELEMENT_CLASS,21} from "./top-nav-consts";22import { blur_active_element } from "./util";2324interface Props {25height: number; // px26pageStyle: PageStyle;27on_click?: () => void;28}2930const BASE_STYLE: CSS = {31fontSize: FONT_SIZE_ICONS_NORMAL,32display: "inline",33color: COLORS.GRAY_M,34} as const;3536export const ConnectionIndicator: React.FC<Props> = React.memo(37(props: Props) => {38const { on_click, height, pageStyle } = props;39const { topPaddingIcons, sidePaddingIcons, fontSizeIcons } = pageStyle;4041const intl = useIntl();42const connection_status = useTypedRedux("page", "connection_status");43const mesg_info = useTypedRedux("account", "mesg_info");44const actions = useActions("page");4546const connecting_style: CSS = {47flex: "1",48color: "white",49overflow: "hidden",50margin: "auto 0",51} as const;5253const outer_style: CSS = {54flex: "0 0 auto",55display: "flex",56alignItems: "center",57color: COLORS.GRAY_M,58cursor: "pointer",59height: `${height}px`,60padding: `${topPaddingIcons} ${sidePaddingIcons}`,61...(connection_status !== "connected"62? {63backgroundColor:64connection_status === "disconnected"65? COLORS.ANTD_RED_WARN66: COLORS.ORANGE_WARN,67}68: {}),69} as const;7071function render_connection_status() {72if (connection_status === "connected") {73const icon_style: CSS = { ...BASE_STYLE, fontSize: fontSizeIcons };74if (mesg_info?.get("enqueued") ?? 0 > 6) {75// serious backlog of data!76icon_style.color = "red";77} else if (mesg_info?.get("count") ?? 0 > 2) {78// worrisome amount79icon_style.color = "#08e";80} else if (mesg_info?.get("count") ?? 0 > 0) {81// working well but doing something minimal82icon_style.color = "#00c";83}84return <Icon name="wifi" style={icon_style} />;85} else if (connection_status === "connecting") {86return (87<div style={connecting_style}>88{intl.formatMessage(labels.connecting)}...89</div>90);91} else if (connection_status === "disconnected") {92return (93<div style={connecting_style}>94{intl.formatMessage(labels.disconnected)}95</div>96);97}98}99100function connection_click() {101actions.show_connection(true);102if (typeof on_click === "function") {103on_click();104}105blur_active_element(); // otherwise, it'll be highlighted even when closed again106track("top_nav", { name: "connection" });107}108109return (110<div111className={TOP_BAR_ELEMENT_CLASS}112style={outer_style}113onClick={connection_click}114>115{render_connection_status()}116</div>117);118},119);120121122