Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

2.4 KB
Raw Blame
import * as z from "zod/mini";

import * as blueslip from "./blueslip.ts";
import * as dialog_widget from "./dialog_widget.ts";
import {$t, $t_html} from "./i18n.ts";
import {localstorage} from "./localstorage.ts";

export function get_hotkey_deprecation_notice(
    originalHotkey: string,
    replacementHotkey: string,
): string {
    return $t_html(
        {
            defaultMessage:
                'We\'ve replaced the "{originalHotkey}" hotkey with "{replacementHotkey}" to make this common shortcut easier to trigger.',
        },
        {originalHotkey, replacementHotkey},
    );
}

let shown_deprecation_notices: string[] = [];

export function maybe_show_deprecation_notice(key: string): void {
    let message;
    switch (key) {
        case "Shift + C":
            message = get_hotkey_deprecation_notice("Shift + C", "X");
            break;
        case "Shift + S":
            message = get_hotkey_deprecation_notice("Shift + S", "S");
            break;
        default:
            blueslip.error("Unexpected deprecation notice for hotkey:", {key});
            return;
    }

    // Here we handle the tracking for showing deprecation notices,
    // whether or not local storage is available.
    if (localstorage.supported()) {
        const notices_from_storage = localStorage.getItem("shown_deprecation_notices");
        if (notices_from_storage !== null) {
            const parsed_notices_from_storage = z
                .array(z.string())
                .parse(JSON.parse(notices_from_storage));

            shown_deprecation_notices = parsed_notices_from_storage;
        } else {
            shown_deprecation_notices = [];
        }
    }

    if (!shown_deprecation_notices.includes(key)) {
        dialog_widget.launch({
            modal_title_html: $t_html({defaultMessage: "Deprecation notice"}),
            modal_content_html: message,
            modal_submit_button_text: $t({defaultMessage: "Got it"}),
            is_compact: true,
            on_click() {
                return;
            },
            close_on_submit: true,
            focus_submit_on_open: true,
            single_footer_button: true,
        });

        shown_deprecation_notices.push(key);
        if (localstorage.supported()) {
            localStorage.setItem(
                "shown_deprecation_notices",
                JSON.stringify(shown_deprecation_notices),
            );
        }
    }
}