Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

web: Show proper error messages for existing channel names.

Rakshit Chauhanb5e217f89688
diff --git a/web/src/stream_create.ts b/web/src/stream_create.ts
index 1d3013b85ba..703c003fbe7 100644
--- a/web/src/stream_create.ts
+++ b/web/src/stream_create.ts
@@ -3,6 +3,7 @@ import assert from "minimalistic-assert";
import * as z from "zod/mini";
import render_subscription_invites_warning_modal from "../templates/confirm_dialog/confirm_subscription_invites_warning.hbs";
+import render_channel_name_conflict_error from "../templates/stream_settings/channel_name_conflict_error.hbs";
import * as channel from "./channel.ts";
import * as confirm_dialog from "./confirm_dialog.ts";
@@ -98,16 +99,13 @@ class StreamSubscriptionError {
const stream_subscription_error = new StreamSubscriptionError();
class StreamNameError {
- report_already_exists(error?: string): void {
- const error_message =
- error ?? $t({defaultMessage: "A channel with this name already exists."});
- $("#stream_name_error").text(error_message);
+ report_already_exists(rendered_error: string): void {
+ $("#stream_name_error").html(rendered_error);
$("#stream_name_error").show();
}
clear_errors(): void {
$("#stream_name_error").hide();
- $("#archived_stream_rename").hide();
}
report_empty_stream(): void {
@@ -119,12 +117,6 @@ class StreamNameError {
$("#create_stream_name").trigger("focus").trigger("select");
}
- rename_archived_stream(stream_id: number): void {
- $("#archived_stream_rename").text($t({defaultMessage: "Rename archived channel"}));
- $("#archived_stream_rename").attr("data-stream-id", stream_id);
- $("#archived_stream_rename").show();
- }
-
pre_validate(stream_name: string): void {
// Don't worry about empty strings...we just want to call this
// to warn users early before they start doing too much work
@@ -135,14 +127,17 @@ class StreamNameError {
// realize the stream already exists, I may want to cancel.)
const stream = stream_data.get_sub(stream_name);
if (stream_name && stream) {
- let error;
- if (stream.is_archived) {
- error = $t({defaultMessage: "An archived channel with this name already exists."});
- if (stream_settings_data.get_sub_for_settings(stream).can_change_name_description) {
- this.rename_archived_stream(stream.stream_id);
- }
- }
- this.report_already_exists(error);
+ const can_rename =
+ stream.is_archived &&
+ stream_settings_data.get_sub_for_settings(stream).can_change_name_description;
+
+ const rendered_error = render_channel_name_conflict_error({
+ stream_id: stream.stream_id,
+ is_archived: stream.is_archived,
+ show_rename: can_rename,
+ can_view_channel: true,
+ });
+ this.report_already_exists(rendered_error);
return;
}
@@ -158,11 +153,17 @@ class StreamNameError {
const stream = stream_data.get_sub(stream_name);
if (stream) {
- let error;
- if (stream.is_archived) {
- error = $t({defaultMessage: "An archived channel with this name already exists."});
- }
- this.report_already_exists(error);
+ const can_rename =
+ stream.is_archived &&
+ stream_settings_data.get_sub_for_settings(stream).can_change_name_description;
+
+ const rendered_error = render_channel_name_conflict_error({
+ stream_id: stream.stream_id,
+ is_archived: stream.is_archived,
+ show_rename: can_rename,
+ can_view_channel: true,
+ });
+ this.report_already_exists(rendered_error);
this.select();
return false;
}
@@ -434,14 +435,18 @@ function create_stream(): void {
// with i18n. And likely we should be reporting the
// error text directly rather than turning it into
// "Error creating channel"?
- stream_name_error.report_already_exists();
- stream_name_error.select();
- const message = $t_html({
- defaultMessage:
- "Error creating channel: A channel with this name already exists.",
+ const rendered_error = render_channel_name_conflict_error({
+ stream_id: undefined,
+ is_archived: false,
+ show_rename: false,
+ can_view_channel: false,
});
+ stream_name_error.report_already_exists(rendered_error);
+ stream_name_error.select();
- ui_report.error(message, undefined, $(".stream_create_info"));
+ stream_settings_components.show_subs_pane.create_stream(
+ "configure_channel_settings",
+ );
} else {
ui_report.error(
$t_html({defaultMessage: "Error creating channel"}),
diff --git a/web/src/stream_edit.ts b/web/src/stream_edit.ts
index 6b16f85e7c4..e123c2b25e7 100644
--- a/web/src/stream_edit.ts
+++ b/web/src/stream_edit.ts
@@ -8,6 +8,7 @@ import render_settings_deactivation_stream_modal from "../templates/confirm_dial
import render_settings_reactivation_stream_modal from "../templates/confirm_dialog/confirm_reactivate_stream.hbs";
import render_inline_decorated_channel_name from "../templates/inline_decorated_channel_name.hbs";
import render_change_stream_info_modal from "../templates/stream_settings/change_stream_info_modal.hbs";
+import render_channel_name_conflict_error from "../templates/stream_settings/channel_name_conflict_error.hbs";
import render_confirm_stream_privacy_change_modal from "../templates/stream_settings/confirm_stream_privacy_change_modal.hbs";
import render_copy_email_address_modal from "../templates/stream_settings/copy_email_address_modal.hbs";
import render_stream_description from "../templates/stream_settings/stream_description.hbs";
@@ -150,7 +151,43 @@ export function save_stream_info(): void {
data.description = new_description;
}
- dialog_widget.submit_api_request(channel.patch, url, data);
+ dialog_widget.submit_api_request(channel.patch, url, data, {
+ error_continuation(xhr) {
+ const {code} = z.object({code: z.string()}).parse(xhr.responseJSON);
+
+ if (code === "CHANNEL_ALREADY_EXISTS") {
+ $("#dialog_error").hide().empty();
+
+ assert(data.new_name !== undefined);
+ const existing_stream = stream_data.get_sub_by_name(data.new_name);
+
+ if (existing_stream) {
+ const can_rename =
+ existing_stream.is_archived &&
+ stream_settings_data.get_sub_for_settings(existing_stream)
+ .can_change_name_description;
+
+ const rendered_error = render_channel_name_conflict_error({
+ stream_id: existing_stream.stream_id,
+ is_archived: existing_stream.is_archived,
+ show_rename: can_rename,
+ can_view_channel: true,
+ });
+
+ $("#change_stream_name_error").html(rendered_error).show();
+ } else {
+ const rendered_error = render_channel_name_conflict_error({
+ stream_id: undefined,
+ is_archived: false,
+ show_rename: false,
+ can_view_channel: false,
+ });
+ $("#change_stream_name_error").html(rendered_error).show();
+ }
+ $("#change_stream_name").trigger("focus");
+ }
+ },
+ });
}
export function get_display_text_for_realm_message_retention_setting(): string {
@@ -590,6 +627,16 @@ export function initialize(): void {
},
);
+ $("body").on("click", "#change_stream_info_modal #archived_stream_rename", (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+
+ const stream_id = Number.parseInt($(e.currentTarget).attr("data-stream-id")!, 10);
+
+ dialog_widget.close();
+ open_stream_edit_modal(stream_id);
+ });
+
$("#channels_overlay_container").on("keydown", "#change_stream_description", (e) => {
// Stream descriptions cannot be multiline, so disable enter key
// to prevent new line
diff --git a/web/styles/subscriptions.css b/web/styles/subscriptions.css
index 1d1486c8439..0a7d14e668c 100644
--- a/web/styles/subscriptions.css
+++ b/web/styles/subscriptions.css
@@ -116,6 +116,23 @@
color: hsl(0deg 100% 50%);
}
+.stream_creation_error a,
+#change_stream_name_error a {
+ text-decoration: underline;
+}
+
+#change_stream_name {
+ margin-bottom: 0;
+}
+
+#change_stream_name_error {
+ color: hsl(0deg 100% 50%);
+}
+
+.change-stream-name-container {
+ margin-bottom: 20px;
+}
+
/* TODO: Unify with settings.css definition */
h3.stream_setting_subsection_title,
h3.user_group_setting_subsection_title {
diff --git a/web/templates/stream_settings/change_stream_info_modal.hbs b/web/templates/stream_settings/change_stream_info_modal.hbs
index 40e5412f058..c79cb35bd4e 100644
--- a/web/templates/stream_settings/change_stream_info_modal.hbs
+++ b/web/templates/stream_settings/change_stream_info_modal.hbs
@@ -1,8 +1,9 @@
-<div>
+<div class="change-stream-name-container">
<label for="change_stream_name" class="modal-field-label">
{{t 'Channel name' }}
</label>
<input type="text" id="change_stream_name" class="modal_text_input" name="stream_name" value="{{ stream_name }}" maxlength="{{ max_stream_name_length }}" />
+ <div id="change_stream_name_error"></div>
</div>
<div>
<label for="change_stream_description" class="modal-field-label">
diff --git a/web/templates/stream_settings/channel_name_conflict_error.hbs b/web/templates/stream_settings/channel_name_conflict_error.hbs
new file mode 100644
index 00000000000..82201e623e0
--- /dev/null
+++ b/web/templates/stream_settings/channel_name_conflict_error.hbs
@@ -0,0 +1,22 @@
+{{#if is_archived}}
+ {{#if can_view_channel}}
+ {{#tr}}
+ An <z-link>archived channel</z-link> with this name already exists.
+ {{#*inline "z-link"}}<a href="#channels/{{stream_id}}/general" class="stream-settings-link">{{> @partial-block}}</a>{{/inline}}
+ {{/tr}}
+ {{else}}
+ {{t "An archived channel with this name already exists." }}
+ {{/if}}
+ {{#if show_rename}}
+ <a id="archived_stream_rename" data-stream-id="{{stream_id}}">{{t "Rename it" }}</a>
+ {{/if}}
+{{else}}
+ {{#if can_view_channel}}
+ {{#tr}}
+ A <z-link>channel</z-link> with this name already exists.
+ {{#*inline "z-link"}}<a href="#channels/{{stream_id}}/general" class="stream-settings-link">{{> @partial-block}}</a>{{/inline}}
+ {{/tr}}
+ {{else}}
+ {{t "A channel with this name already exists." }}
+ {{/if}}
+{{/if}}
diff --git a/web/templates/stream_settings/stream_creation_form.hbs b/web/templates/stream_settings/stream_creation_form.hbs
index 6e8b5d6baed..323380ac3cb 100644
--- a/web/templates/stream_settings/stream_creation_form.hbs
+++ b/web/templates/stream_settings/stream_creation_form.hbs
@@ -13,7 +13,6 @@
<input type="text" name="stream_name" id="create_stream_name" class="settings_text_input"
placeholder="{{t 'Channel name' }}" value="" autocomplete="off" maxlength="{{ max_stream_name_length }}" />
<div id="stream_name_error" class="stream_creation_error"></div>
- <a id="archived_stream_rename"></a>
</section>
<section id="create_stream_description_container">
<label for="create_stream_description" class="settings-field-label">