diff --git a/web/src/reactions.ts b/web/src/reactions.ts index 19b0a21417..fa6df71a61 100644 --- a/web/src/reactions.ts +++ b/web/src/reactions.ts @@ -3,6 +3,7 @@ import assert from "minimalistic-assert"; import {z} from "zod"; import render_message_reaction from "../templates/message_reaction.hbs"; +import render_message_reactions from "../templates/message_reactions.hbs"; import * as blueslip from "./blueslip"; import * as channel from "./channel"; @@ -369,11 +370,23 @@ export function insert_new_reaction( class: reaction_class, }; - const $new_reaction = $(render_message_reaction(context)); - - // Now insert it before the add button. - const $reaction_button_element = get_add_reaction_button(message.id); - $new_reaction.insertBefore($reaction_button_element); + // If the given reaction is the first reaction in a message, then we add + // the whole message reactions section along with the new reaction. + // Else, we insert the new reaction before the add reaction button. + if (message.clean_reactions.size - 1 === 0) { + const $rows = message_lists.all_rendered_row_for_message_id(message.id); + const reaction_section_context = { + msg: { + message_reactions: [context], + }, + }; + const $msg_reaction_section = render_message_reactions(reaction_section_context); + $rows.find(".messagebox-content").append($msg_reaction_section); + } else { + const $new_reaction = $(render_message_reaction(context)); + const $reaction_button_element = get_add_reaction_button(message.id); + $new_reaction.insertBefore($reaction_button_element); + } update_vote_text_on_message(message); } @@ -424,6 +437,14 @@ export function remove_reaction_from_view( const $reaction = find_reaction(message.id, local_id); const reaction_count = clean_reaction_object.user_ids.length; + // Cleanup: If the reaction being removed is the last reaction on the + // message, we remove the whole message reaction section and exit. + if (message.clean_reactions.size === 0) { + const $msg_reaction_section = get_reaction_sections(message.id); + $msg_reaction_section.remove(); + return; + } + if (reaction_count === 0) { // If this user was the only one reacting for this emoji, we simply // remove the reaction and exit. diff --git a/web/styles/reactions.css b/web/styles/reactions.css index 4f4deed425..55b02cb072 100644 --- a/web/styles/reactions.css +++ b/web/styles/reactions.css @@ -121,15 +121,6 @@ color: var(--color-message-reaction-button-text-hover); } - /* Configure the reaction button to appear if and only if there's an - existing reaction to the message. We reference first-child - rather than only-child because tooltips may be appended to - the DOM after this element, whereas actual reactions are - appended before it. */ - &:first-child { - display: none; - } - &:hover { color: var(--color-message-reaction-button-text-hover); background-color: var( diff --git a/web/templates/message_body.hbs b/web/templates/message_body.hbs index 1d888bac0c..10d16b8ff8 100644 --- a/web/templates/message_body.hbs +++ b/web/templates/message_body.hbs @@ -69,6 +69,6 @@
-{{#unless is_hidden}} - -{{/unless}} +{{#if (and (not is_hidden) msg.message_reactions)}} + {{> message_reactions }} +{{/if}} diff --git a/web/templates/message_reactions.hbs b/web/templates/message_reactions.hbs index 878f0883db..7828520d98 100644 --- a/web/templates/message_reactions.hbs +++ b/web/templates/message_reactions.hbs @@ -1,9 +1,11 @@ -{{#each this/msg/message_reactions}} - {{> message_reaction}} -{{/each}} -