zcommand: Convert module to TypeScript.

This commit is contained in:
afeefuddin 2024-03-05 01:15:57 +05:30 committed by Tim Abbott
parent b4d53c7930
commit 6314139d4a
2 changed files with 17 additions and 10 deletions

View File

@ -283,7 +283,7 @@ EXEMPT_FILES = make_set(
"web/src/user_topics.ts", "web/src/user_topics.ts",
"web/src/user_topics_ui.js", "web/src/user_topics_ui.js",
"web/src/views_util.js", "web/src/views_util.js",
"web/src/zcommand.js", "web/src/zcommand.ts",
"web/src/zform.js", "web/src/zform.js",
"web/src/zulip.js", "web/src/zulip.js",
"web/src/zulip_test.js", "web/src/zulip_test.js",

View File

@ -1,4 +1,5 @@
import $ from "jquery"; import $ from "jquery";
import {z} from "zod";
import * as channel from "./channel"; import * as channel from "./channel";
import * as compose_banner from "./compose_banner"; 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 command = opts.command;
const on_success = opts.on_success; const on_success = opts.on_success;
const data = { const data = {
command, command,
}; };
channel.post({ void channel.post({
url: "/json/zcommand", url: "/json/zcommand",
data, data,
success(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 // This is a bit hacky, but we don't have a super easy API now
// for just telling users stuff. // for just telling users stuff.
compose_banner.show_error_message( 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({ send({
command: "/day", command: "/day",
on_success(data) { on_success(data) {
const clean_data = data_schema.parse(data);
requestAnimationFrame(() => { requestAnimationFrame(() => {
dark_theme.disable(); dark_theme.disable();
message_lists.update_recipient_bar_background_color(); message_lists.update_recipient_bar_background_color();
}); });
feedback_widget.show({ feedback_widget.show({
populate($container) { 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); $container.html(rendered_msg);
}, },
on_undo() { 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({ send({
command: "/night", command: "/night",
on_success(data) { on_success(data) {
const clean_data = data_schema.parse(data);
requestAnimationFrame(() => { requestAnimationFrame(() => {
dark_theme.enable(); dark_theme.enable();
message_lists.update_recipient_bar_background_color(); message_lists.update_recipient_bar_background_color();
}); });
feedback_widget.show({ feedback_widget.show({
populate($container) { 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); $container.html(rendered_msg);
}, },
on_undo() { 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(); const content = message_content.trim();
if (content === "/ping") { if (content === "/ping") {
@ -117,7 +124,7 @@ export function process(message_content) {
command: content, command: content,
on_success() { on_success() {
const end_time = new Date(); const end_time = new Date();
let diff = end_time - start_time; let diff = end_time.getTime() - start_time.getTime();
diff = Math.round(diff); diff = Math.round(diff);
const msg = "ping time: " + diff + "ms"; const msg = "ping time: " + diff + "ms";
tell_user(msg); tell_user(msg);