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/alerts.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { notification } from "antd";6import { ReactElement } from "react";78import {9defaults,10hash_string,11server_seconds_ago,12server_time,13} from "@cocalc/util/misc";1415import { webapp_client } from "./webapp-client";1617type NotificationType = "error" | "default" | "success" | "info" | "warning";1819const default_timeout: { [key: string]: number } = {20error: 8,21default: 4,22success: 4,23info: 6,24};2526const last_shown = {};2728interface AlertMessageOptions {29type?: NotificationType;30title?: string | ReactElement<any>;31message?: string | ReactElement<any> | Error;32block?: boolean;33timeout?: number;34}3536export function alert_message(opts: AlertMessageOptions = {}) {37opts = defaults(opts, {38type: "default",39title: undefined,40message: "",41block: undefined,42timeout: undefined, // time in seconds43});44if (opts.type == null) throw Error("bug"); // make typescript happy.45if (opts.timeout == null) {46let t: number | undefined = default_timeout[opts.type];47if (t == null) {48t = 5;49}50opts.timeout = t;51}5253// Don't show the exact same alert message more than once per 5s.54// This prevents a screenful of identical useless messages, which55// is just annoying and useless.56if (opts.message instanceof Error) {57opts.message = `${opts.message}`;58} else if (opts.message === "string") {59const hash = hash_string(opts.message + opts.type);60if (last_shown[hash] >= server_seconds_ago(5)) {61return;62}63last_shown[hash] = server_time();64}6566const f =67opts.type == "default" ? notification.open : notification[opts.type];68if (f == null) {69alert(`BUG: Unknown alert_message type ${opts.type}.`);70return;71}72f({73message: opts.title != null ? opts.title : "",74description: opts.message,75duration: opts.block ? 0 : opts.timeout,76});7778if (opts.type === "error") {79// Send the same error message to the backend hub so80// that us developers know what errors people are hitting.81// There really should be no situation where users *regularly*82// get error alert messages.83webapp_client.tracking_client.log_error(opts.message);84}85}8687function check_for_clock_skew() {88const local_time = Date.now();89const s = Math.ceil(90Math.abs(91webapp_client.time_client.server_time().valueOf() - local_time.valueOf()92) / 100093);94if (s > 120) {95return alert_message({96type: "error",97timeout: 9999,98message: `Your computer's clock is off by about ${s} seconds! You MUST set it correctly then refresh your browser. Expect nothing to work until you fix this.`,99});100}101}102103// Wait until after the page is loaded and clock sync'd before checking for skew.104setTimeout(check_for_clock_skew, 60000);105106// for testing/development107/*108alert_message({ type: "error", message: "This is an error" });109alert_message({ type: "default", message: "This is a default alert" });110alert_message({ type: "warning", message: "This is a warning alert" });111alert_message({ type: "success", message: "This is a success alert" });112alert_message({ type: "info", message: "This is an info alert" });113*/114115116