Skip to content

Commit 7c9f954

Browse files
Copilotgortazar
andcommitted
refactor: centralize add-channel button label and guard checks
Agent-Logs-Url: https://github.com/gortazar/thunderbird-slack-provider/sessions/a2b942e7-1eb1-40d8-a067-47480e6b0b9f Co-authored-by: gortazar <203272+gortazar@users.noreply.github.com>
1 parent 99cb0f9 commit 7c9f954

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/space.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ let rateLimitedMode = false;
1616
let workspaceName = "Slack";
1717
let addChannelChoices = [];
1818
let addChannelLoadId = 0;
19+
const ADD_CHANNEL_BUTTON_TEXT = "Add Channel";
1920

2021
// ---------------------------------------------------------------------------
2122
// Startup
@@ -383,7 +384,7 @@ function showAddChannelDialog() {
383384
select.innerHTML = '<option value="">Loading channels...</option>';
384385
select.disabled = true;
385386
confirmBtn.disabled = true;
386-
confirmBtn.textContent = "Add Channel";
387+
confirmBtn.textContent = ADD_CHANNEL_BUTTON_TEXT;
387388
errorEl.classList.add("hidden");
388389
dialog.classList.remove("hidden");
389390
cancelBtn.focus();
@@ -393,7 +394,8 @@ function showAddChannelDialog() {
393394
bg({ type: "get_channels" }),
394395
bg({ type: "get_watched_channels" }),
395396
]).then(([chanRes, watchedRes]) => {
396-
if (loadId !== addChannelLoadId || dialog.classList.contains("hidden")) { return; }
397+
if (loadId !== addChannelLoadId) { return; } // Ignore stale async responses
398+
if (dialog.classList.contains("hidden")) { return; } // Ignore updates for closed dialog
397399
if (chanRes.error) {
398400
errorEl.textContent = `Could not load channels: ${chanRes.error}`;
399401
errorEl.classList.remove("hidden");
@@ -425,7 +427,8 @@ function showAddChannelDialog() {
425427
confirmBtn.disabled = false;
426428
select.focus();
427429
}).catch((e) => {
428-
if (loadId !== addChannelLoadId || dialog.classList.contains("hidden")) { return; }
430+
if (loadId !== addChannelLoadId) { return; } // Ignore stale async responses
431+
if (dialog.classList.contains("hidden")) { return; } // Ignore updates for closed dialog
429432
errorEl.textContent = `Could not load channels: ${e.message}`;
430433
errorEl.classList.remove("hidden");
431434
select.innerHTML = '<option value="">No channels available</option>';
@@ -434,7 +437,7 @@ function showAddChannelDialog() {
434437

435438
function hideAddChannelDialog() {
436439
document.getElementById("add-channel-dialog").classList.add("hidden");
437-
document.getElementById("btn-add-channel-confirm").textContent = "Add Channel";
440+
document.getElementById("btn-add-channel-confirm").textContent = ADD_CHANNEL_BUTTON_TEXT;
438441
addChannelLoadId++;
439442
addChannelChoices = [];
440443
document.removeEventListener("keydown", _onDialogKeydown);
@@ -478,7 +481,7 @@ async function addChannel() {
478481
errorEl.classList.remove("hidden");
479482
} finally {
480483
confirmBtn.disabled = false;
481-
confirmBtn.textContent = "Add Channel";
484+
confirmBtn.textContent = ADD_CHANNEL_BUTTON_TEXT;
482485
}
483486
}
484487

0 commit comments

Comments
 (0)