Path: blob/master/src/packages/frontend/app/connection-indicator.tsx
1496 views
/*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 actions = useActions("page");43const connection_status = useTypedRedux("page", "connection_status");4445const connecting_style: CSS = {46flex: "1",47color: "white",48overflow: "hidden",49margin: "auto 0",50} as const;5152const outer_style: CSS = {53flex: "0 0 auto",54display: "flex",55alignItems: "center",56color: COLORS.GRAY_M,57cursor: "pointer",58height: `${height}px`,59padding: `${topPaddingIcons} ${sidePaddingIcons}`,60...(connection_status !== "connected"61? {62backgroundColor:63connection_status === "disconnected"64? COLORS.ANTD_RED_WARN65: COLORS.ORANGE_WARN,66}67: {}),68} as const;6970function render_connection_status() {71if (connection_status === "connected") {72return (73<Icon74name="wifi"75style={{ ...BASE_STYLE, fontSize: fontSizeIcons }}76/>77);78} else if (connection_status === "connecting") {79return (80<div style={connecting_style}>81{intl.formatMessage(labels.connecting)}...82</div>83);84} else if (connection_status === "disconnected") {85return (86<div style={connecting_style}>87{intl.formatMessage(labels.disconnected)}88</div>89);90}91}9293function connection_click() {94actions.show_connection(true);95if (typeof on_click === "function") {96on_click();97}98blur_active_element(); // otherwise, it'll be highlighted even when closed again99track("top_nav", { name: "connection" });100}101102return (103<div104className={TOP_BAR_ELEMENT_CLASS}105style={outer_style}106onClick={connection_click}107>108{render_connection_status()}109</div>110);111},112);113114115116