From 6314139d4acfc8235e090ed346f945255b0cf086 Mon Sep 17 00:00:00 2001 From: afeefuddin Date: Tue, 5 Mar 2024 01:15:57 +0530 Subject: [PATCH] zcommand: Convert module to TypeScript. --- tools/test-js-with-node | 2 +- web/src/{zcommand.js => zcommand.ts} | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) rename web/src/{zcommand.js => zcommand.ts} (85%) diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 4f587d0ef1..febc66673a 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -283,7 +283,7 @@ EXEMPT_FILES = make_set( "web/src/user_topics.ts", "web/src/user_topics_ui.js", "web/src/views_util.js", - "web/src/zcommand.js", + "web/src/zcommand.ts", "web/src/zform.js", "web/src/zulip.js", "web/src/zulip_test.js", diff --git a/web/src/zcommand.js b/web/src/zcommand.ts similarity index 85% rename from web/src/zcommand.js rename to web/src/zcommand.ts index cd7e2a5637..6282e8ae0c 100644 --- a/web/src/zcommand.js +++ b/web/src/zcommand.ts @@ -1,4 +1,5 @@ import $ from "jquery"; +import {z} from "zod"; import * as channel from "./channel"; import * as compose_banner from "./compose_banner"; @@ -26,14 +27,18 @@ What in the heck is a zcommand? */ -export function send(opts) { +const data_schema = z.object({ + msg: z.string(), +}); + +export function send(opts: {command: string; on_success?: (data: unknown) => void}): void { const command = opts.command; const on_success = opts.on_success; const data = { command, }; - channel.post({ + void channel.post({ url: "/json/zcommand", data, success(data) { @@ -47,7 +52,7 @@ export function send(opts) { }); } -export function tell_user(msg) { +export function tell_user(msg: string): void { // This is a bit hacky, but we don't have a super easy API now // for just telling users stuff. compose_banner.show_error_message( @@ -57,17 +62,18 @@ export function tell_user(msg) { ); } -export function switch_to_light_theme() { +export function switch_to_light_theme(): void { send({ command: "/day", on_success(data) { + const clean_data = data_schema.parse(data); requestAnimationFrame(() => { dark_theme.disable(); message_lists.update_recipient_bar_background_color(); }); feedback_widget.show({ populate($container) { - const rendered_msg = markdown.parse_non_message(data.msg); + const rendered_msg = markdown.parse_non_message(clean_data.msg); $container.html(rendered_msg); }, on_undo() { @@ -82,17 +88,18 @@ export function switch_to_light_theme() { }); } -export function switch_to_dark_theme() { +export function switch_to_dark_theme(): void { send({ command: "/night", on_success(data) { + const clean_data = data_schema.parse(data); requestAnimationFrame(() => { dark_theme.enable(); message_lists.update_recipient_bar_background_color(); }); feedback_widget.show({ populate($container) { - const rendered_msg = markdown.parse_non_message(data.msg); + const rendered_msg = markdown.parse_non_message(clean_data.msg); $container.html(rendered_msg); }, on_undo() { @@ -107,7 +114,7 @@ export function switch_to_dark_theme() { }); } -export function process(message_content) { +export function process(message_content: string): boolean { const content = message_content.trim(); if (content === "/ping") { @@ -117,7 +124,7 @@ export function process(message_content) { command: content, on_success() { const end_time = new Date(); - let diff = end_time - start_time; + let diff = end_time.getTime() - start_time.getTime(); diff = Math.round(diff); const msg = "ping time: " + diff + "ms"; tell_user(msg);