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.

manage-bot: Move API key section to a separate modal.

Sahil Batracda9dedf6c77
diff --git a/web/src/bot_helper.ts b/web/src/bot_helper.ts
index 940db1c3a56..6cf81274a7b 100644
--- a/web/src/bot_helper.ts
+++ b/web/src/bot_helper.ts
@@ -3,9 +3,13 @@ import $ from "jquery";
import assert from "minimalistic-assert";
import * as z from "zod/mini";
+import render_bot_api_key_details from "../templates/settings/bot_api_key_details.hbs";
+
import * as bot_data from "./bot_data.ts";
import * as channel from "./channel.ts";
import {show_copied_confirmation} from "./copied_tooltip.ts";
+import * as dialog_widget from "./dialog_widget.ts";
+import {$t_html} from "./i18n.ts";
import {realm} from "./state_data.ts";
export function validate_bot_short_name(value: string): boolean {
@@ -54,10 +58,10 @@ export function generate_zuliprc_content(bot: {
);
}
-export function initialize_clipboard_handlers(): void {
+function initialize_api_key_clipboard_handlers(): void {
new ClipboardJS("#copy-api-key-button", {
text(trigger) {
- const data = $(trigger).closest("span").attr("data-api-key") ?? "";
+ const data = $(trigger).closest(".bot-api-key-container").attr("data-api-key") ?? "";
return data;
},
}).on("success", (e) => {
@@ -66,7 +70,9 @@ export function initialize_clipboard_handlers(): void {
show_check_icon: true,
});
});
+}
+export function initialize_zuliprc_clipboard_handlers(): void {
new ClipboardJS("#copy-zuliprc-config", {
text(trigger) {
const $bot_info = $(trigger).closest("#bot-edit-form");
@@ -84,6 +90,28 @@ export function initialize_clipboard_handlers(): void {
});
}
+export function show_api_key_modal(bot_id: number): void {
+ const bot = bot_data.get(bot_id)!;
+ const modal_content_html = render_bot_api_key_details({
+ bot_id,
+ api_key: bot.api_key,
+ });
+ dialog_widget.launch({
+ modal_title_html: $t_html(
+ {defaultMessage: "API key for {bot_name}"},
+ {bot_name: bot.full_name},
+ ),
+ modal_content_html,
+ id: "api-key-modal",
+ single_footer_button: true,
+ modal_submit_button_text: $t_html({defaultMessage: "Close"}),
+ on_click() {
+ return;
+ },
+ post_render: initialize_api_key_clipboard_handlers,
+ });
+}
+
export function initialize_bot_click_handlers(): void {
$("body").on("click", "button.bot-modal-regenerate-bot-api-key", (e) => {
e.preventDefault();
@@ -103,7 +131,7 @@ export function initialize_bot_click_handlers(): void {
.parse(data);
$row.find(".api-key").val(parsed_data.api_key);
- $row.find("span[data-api-key]").attr("data-api-key", parsed_data.api_key);
+ $row.closest(".bot-api-key-container").attr("data-api-key", parsed_data.api_key);
$row.find(".bot-modal-api-key-error").hide();
},
error(xhr) {
diff --git a/web/src/user_profile.ts b/web/src/user_profile.ts
index 539dc882b3c..186dbc8a981 100644
--- a/web/src/user_profile.ts
+++ b/web/src/user_profile.ts
@@ -870,11 +870,9 @@ export function show_edit_bot_info_modal(user_id: number, $container: JQuery): v
disable_role_dropdown: !current_user.is_admin || (bot.is_owner && !current_user.is_owner),
bot_avatar_url: bot.avatar_url,
bot_type: settings_data.bot_type_id_to_string(bot.bot_type),
- api_key: bot_user.api_key,
is_incoming_webhook_bot: bot.bot_type === INCOMING_WEBHOOK_BOT_TYPE,
max_bot_name_length: people.MAX_USER_NAME_LENGTH,
realm_bot_domain: realm.realm_bot_domain,
- zuliprc: "zuliprc",
});
$container.append($(modal_content_html));
let avatar_widget: UploadWidget;
@@ -1121,6 +1119,13 @@ export function show_edit_bot_info_modal(user_id: number, $container: JQuery): v
assert(current_bot_data !== undefined);
integration_url_modal.show_generate_integration_url_modal(current_bot_data.api_key);
});
+
+ $("#bot-edit-form").on("click", ".show-api-key", (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ assert(bot !== undefined);
+ bot_helper.show_api_key_modal(bot.user_id);
+ });
}
}
@@ -1690,5 +1695,5 @@ export function initialize(): void {
});
});
- bot_helper.initialize_clipboard_handlers();
+ bot_helper.initialize_zuliprc_clipboard_handlers();
}
diff --git a/web/styles/settings.css b/web/styles/settings.css
index 3fd9e77c941..11340725e26 100644
--- a/web/styles/settings.css
+++ b/web/styles/settings.css
@@ -440,7 +440,8 @@ select.settings_select {
width: fit-content;
}
-#bot-edit-form {
+#bot-edit-form,
+#api-key-modal {
.api-key-details-container,
.zuliprc-container {
display: flex;
@@ -452,9 +453,19 @@ select.settings_select {
align-items: center;
}
}
+
+ .modal_text_input {
+ margin-bottom: 0;
+ }
}
#zuliprc-section {
+ /* Normally all input-group elements have bottom margin of 1.25em.
+ But here the buttons have bottom padding of 0.375em and thus there is
+ already some extra space between this and the section below it, so we
+ reduce the bottom margin by 0.375em. */
+ margin-bottom: 0.875em;
+
.modal-field-label {
margin-bottom: 0;
}
diff --git a/web/templates/settings/bot_api_key_details.hbs b/web/templates/settings/bot_api_key_details.hbs
new file mode 100644
index 00000000000..51230896959
--- /dev/null
+++ b/web/templates/settings/bot_api_key_details.hbs
@@ -0,0 +1,28 @@
+<div class="bot-api-key-container" data-api-key="{{api_key}}" data-user-id="{{bot_id}}">
+ <div class="input-group">
+ <label for="api-key" class="modal-field-label">{{t "API key"}}</label>
+ <div class="api-key-details-container">
+ <input type="text" autocomplete="off" name="api-key" class="modal_text_input api-key inline-block" value="{{ api_key }}" readonly/>
+ <div class="buttons-container">
+ <span data-user-id="{{bot_id}}">
+ {{> ../components/icon_button
+ icon="refresh-cw"
+ intent="brand"
+ custom_classes="bot-modal-regenerate-bot-api-key tippy-zulip-delayed-tooltip"
+ data-tippy-content=(t "Generate new API key")
+ }}
+ </span>
+ <span>
+ {{> ../components/icon_button
+ icon="copy"
+ intent="success"
+ id="copy-api-key-button"
+ custom_classes="copy-api-key tippy-zulip-delayed-tooltip"
+ data-tippy-content=(t "Copy API key")
+ }}
+ </span>
+ </div>
+ </div>
+ <div class="bot-modal-api-key-error text-error"></div>
+ </div>
+</div>
diff --git a/web/templates/settings/edit_bot_form.hbs b/web/templates/settings/edit_bot_form.hbs
index 6b47ec57130..80cbf35110b 100644
--- a/web/templates/settings/edit_bot_form.hbs
+++ b/web/templates/settings/edit_bot_form.hbs
@@ -82,7 +82,7 @@
<label class="modal-field-label">{{t "Zuliprc configuration" }} {{> ../help_link_widget link="/api/configuring-python-bindings" }}</label>
<div class="buttons-container">
<span>
- <a type="submit" download="{{zuliprc}}" data-email="{{email}}" data-user-id="{{user_id}}" class="hidden-zuliprc-download" hidden></a>
+ <a type="submit" download="zuliprc" data-email="{{email}}" data-user-id="{{user_id}}" class="hidden-zuliprc-download" hidden></a>
{{> ../components/icon_button
custom_classes="download-bot-zuliprc tippy-zulip-delayed-tooltip"
icon="download"
@@ -101,30 +101,12 @@
</div>
</div>
<div class="input-group">
- <label for="api-key" class="modal-field-label">{{t "API key"}}</label>
- <div class="api-key-details-container">
- <input type="text" autocomplete="off" name="api-key" class="modal_text_input api-key inline-block" value="{{ api_key }}" readonly/>
- <div class="buttons-container">
- <span data-user-id="{{user_id}}">
- {{> ../components/icon_button
- icon="refresh-cw"
- intent="brand"
- custom_classes="bot-modal-regenerate-bot-api-key tippy-zulip-delayed-tooltip"
- data-tippy-content=(t "Generate new API key")
- }}
- </span>
- <span data-api-key="{{api_key}}">
- {{> ../components/icon_button
- icon="copy"
- intent="success"
- id="copy-api-key-button"
- custom_classes="copy-api-key tippy-zulip-delayed-tooltip"
- data-tippy-content=(t "Copy API key")
- }}
- </span>
- </div>
- </div>
- <div class="bot-modal-api-key-error text-error"></div>
+ {{> ../components/action_button
+ label=(t "API key")
+ variant="subtle"
+ intent="neutral"
+ custom_classes="show-api-key"
+ }}
</div>
{{/if}}
<div class="input-group">