Skip to content

Commit

Permalink
message move: Add warning moving topic/multiple messages to a pre-exi…
Browse files Browse the repository at this point in the history
…sting conversation

Only show the warning banner if submit button is enabled and the number of
 messages being moved are more than one.

Fixes: #31151
  • Loading branch information
heyiming committed Aug 16, 2024
1 parent a88445a commit de4f17a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
60 changes: 56 additions & 4 deletions web/src/stream_popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import $ from "jquery";
import assert from "minimalistic-assert";

import render_inline_decorated_stream_name from "../templates/inline_decorated_stream_name.hbs";
import render_message_moving_warning_banner from "../templates/modal_banner/message_moving_warning_banner.hbs";
import render_move_topic_to_stream from "../templates/move_topic_to_stream.hbs";
import render_left_sidebar_stream_actions_popover from "../templates/popovers/left_sidebar/left_sidebar_stream_actions_popover.hbs";

import * as blueslip from "./blueslip";
import * as browser_history from "./browser_history";
import * as compose_actions from "./compose_actions";
import * as compose_banner from "./compose_banner";
import * as composebox_typeahead from "./composebox_typeahead";
import * as dialog_widget from "./dialog_widget";
import * as dropdown_widget from "./dropdown_widget";
Expand All @@ -27,6 +29,7 @@ import * as stream_data from "./stream_data";
import * as stream_settings_api from "./stream_settings_api";
import * as stream_settings_components from "./stream_settings_components";
import * as stream_settings_ui from "./stream_settings_ui";
import * as stream_topic_history from "./stream_topic_history";
import * as sub_store from "./sub_store";
import * as ui_report from "./ui_report";
import * as ui_util from "./ui_util";
Expand Down Expand Up @@ -421,7 +424,7 @@ export async function build_move_topic_to_stream_popover(
);
}

function update_submit_button_disabled_state(select_stream_id) {
function update_submit_button_and_warning_banner_state(select_stream_id) {
const {current_stream_id, new_topic_name, old_topic_name} = get_params_from_form();

// Unlike most topic comparisons in Zulip, we intentionally do
Expand All @@ -434,6 +437,7 @@ export async function build_move_topic_to_stream_popover(
$("#move_topic_modal .dialog_submit_button")[0].disabled =
Number.parseInt(current_stream_id, 10) === Number.parseInt(select_stream_id, 10) &&
(new_topic_name === undefined || new_topic_name.trim() === old_topic_name.trim());
update_warning_banner();
}

function move_topic() {
Expand Down Expand Up @@ -557,7 +561,7 @@ export async function build_move_topic_to_stream_popover(
function move_topic_on_update(event, dropdown) {
stream_widget_value = $(event.currentTarget).attr("data-unique-id");

update_submit_button_disabled_state(stream_widget_value);
update_submit_button_and_warning_banner_state(stream_widget_value);
set_stream_topic_typeahead();
render_selected_stream();

Expand All @@ -566,9 +570,57 @@ export async function build_move_topic_to_stream_popover(
event.stopPropagation();
}

function localeSafeCaseInsensitiveInlucde(topics, current_topic) {
return topics.some( t => t.localeCompare(current_topic, undefined, { sensitivity: 'accent' }) === 0);
}

function update_warning_banner() {
// The warning banner only shows when
// 1) submit button is NOT disabled, and
// 2) target topic is an existing topic, and
// 3) propagate_mode is NOT change_one

// submit button
if ($("#move_topic_modal .dialog_submit_button")[0].disabled) {
$("#move_topic_modal .move_topic_form_warning_container").html("");
return;
}

// target topic
if (stream_widget_value !== undefined) {
const stream_topics = stream_topic_history.get_recent_topic_names(
Number.parseInt(stream_widget_value, 10),
);
const {new_topic_name} = get_params_from_form();
if (!localeSafeCaseInsensitiveInlucde(stream_topics, new_topic_name.trim())) {
$("#move_topic_modal .move_topic_form_warning_container").html("");
return;
}
}

// propagate_mode
if ($("#move_topic_modal select.message_edit_topic_propagate").val() === "change_one") {
$("#move_topic_modal .move_topic_form_warning_container").html("");
return;
}

$("#move_topic_modal .move_topic_form_warning_container").html(
render_message_moving_warning_banner({
banner_type: compose_banner.WARNING,
hide_close_button: true,
classname: "message_moving_warning_banner",
}),
);
}

function move_topic_post_render() {
$("#move_topic_modal .dialog_submit_button").prop("disabled", true);

$("#move_topic_modal select.message_edit_topic_propagate")[0]?.addEventListener(
"change",
() => update_warning_banner(),
);

const $topic_input = $("#move_topic_form .move_messages_edit_topic");
move_topic_to_stream_topic_typeahead = composebox_typeahead.initialize_topic_edit_typeahead(
$topic_input,
Expand All @@ -581,7 +633,7 @@ export async function build_move_topic_to_stream_popover(
// to edit stream in topic-edit only UI.
const select_stream_id = current_stream_id;
$topic_input.on("input", () => {
update_submit_button_disabled_state(select_stream_id);
update_submit_button_and_warning_banner_state(select_stream_id);
});
return;
}
Expand Down Expand Up @@ -609,7 +661,7 @@ export async function build_move_topic_to_stream_popover(
render_selected_stream();
$("#move_topic_to_stream_widget").prop("disabled", disable_stream_input);
$("#move_topic_modal .move_messages_edit_topic").on("input", () => {
update_submit_button_disabled_state(stream_widget_value);
update_submit_button_and_warning_banner_state(stream_widget_value);
});
}

Expand Down
7 changes: 7 additions & 0 deletions web/templates/modal_banner/message_moving_warning_banner.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{#> modal_banner }}
<p class="banner_message">
{{#tr}}
You are moving messages to a topic that already exists. Messages from these topics will be combined.
{{/tr}}
</p>
{{/modal_banner}}
1 change: 1 addition & 0 deletions web/templates/move_topic_to_stream.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<p class="white-space-preserve-wrap">{{#tr}}Move all messages in <strong>{topic_name}</strong>{{/tr}} to:</p>
{{/unless}}
<form class="new-style" id="move_topic_form">
<div class="move_topic_form_warning_container"></div>
{{#if only_topic_edit }}
<p>{{t "Rename topic to:" }}</p>
{{else if from_message_actions_popover}}
Expand Down
1 change: 1 addition & 0 deletions zulip
Submodule zulip added at 325ab8

0 comments on commit de4f17a

Please sign in to comment.