From 6f46c4830fec255f6279e260fb2d902203b7e543 Mon Sep 17 00:00:00 2001 From: Lalit Date: Wed, 5 Apr 2023 16:21:26 +0530 Subject: [PATCH] emoji: Refactor `get_emoji_details_by_name` function. This is a prep commit for typescript migration of `emoji.js`. This commit refactors the code for generating emoji rendering details such that we avoid writing an ugly code which will involve writing an incomplete type object when we migrate to TypeScript. --- web/src/emoji.js | 38 ++++++++++++++++++------------------- web/tests/emoji.test.js | 2 ++ web/tests/reactions.test.js | 1 + 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/web/src/emoji.js b/web/src/emoji.js index 5c443fbec9..b6ba07c0e0 100644 --- a/web/src/emoji.js +++ b/web/src/emoji.js @@ -217,29 +217,27 @@ export function get_emoji_details_by_name(emoji_name) { throw new Error("Emoji name must be passed."); } - const emoji_info = {emoji_name}; - if (active_realm_emojis.has(emoji_name)) { - if (emoji_name === "zulip") { - emoji_info.reaction_type = "zulip_extra_emoji"; - } else { - emoji_info.reaction_type = "realm_emoji"; - } const emoji_code_info = active_realm_emojis.get(emoji_name); - emoji_info.emoji_code = emoji_code_info.id; - emoji_info.url = emoji_code_info.emoji_url; - if (emoji_code_info.still_url) { - emoji_info.still_url = emoji_code_info.still_url; - } - } else { - const codepoint = get_emoji_codepoint(emoji_name); - if (codepoint === undefined) { - throw new Error("Bad emoji name: " + emoji_name); - } - emoji_info.reaction_type = "unicode_emoji"; - emoji_info.emoji_code = codepoint; + return { + emoji_name, + emoji_code: emoji_code_info.id, + url: emoji_code_info.emoji_url, + still_url: emoji_code_info.still_url, + reaction_type: emoji_name === "zulip" ? "zulip_extra_emoji" : "realm_emoji", + }; } - return emoji_info; + + const codepoint = get_emoji_codepoint(emoji_name); + if (codepoint === undefined) { + throw new Error("Bad emoji name: " + emoji_name); + } + + return { + emoji_name, + reaction_type: "unicode_emoji", + emoji_code: codepoint, + }; } export function get_emoji_details_for_rendering(opts) { diff --git a/web/tests/emoji.test.js b/web/tests/emoji.test.js index 9186d480c8..f84964be66 100644 --- a/web/tests/emoji.test.js +++ b/web/tests/emoji.test.js @@ -76,6 +76,7 @@ run_test("get_emoji_details_by_name", () => { reaction_type: "zulip_extra_emoji", emoji_code: "zulip", url: "/static/generated/emoji/images/emoji/unicode/zulip.png", + still_url: null, }); // Test adding realm emoji. @@ -97,6 +98,7 @@ run_test("get_emoji_details_by_name", () => { reaction_type: "realm_emoji", emoji_code: "102", url: "/some/path/to/emoji", + still_url: null, }); // Test sending without emoji name. diff --git a/web/tests/reactions.test.js b/web/tests/reactions.test.js index f9f30a2749..e4d966a8f4 100644 --- a/web/tests/reactions.test.js +++ b/web/tests/reactions.test.js @@ -358,6 +358,7 @@ test("sending", ({override, override_rewire}) => { reaction_type: "zulip_extra_emoji", emoji_name: "zulip", emoji_code: "zulip", + still_url: null, url: "/static/generated/emoji/images/emoji/unicode/zulip.png", }); }