Skip to content

Commit acdff44

Browse files
committed
main: addContextMenu should not require index.
1 parent 8e7a78e commit acdff44

File tree

4 files changed

+35
-28
lines changed

4 files changed

+35
-28
lines changed

app/renderer/js/main.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,14 @@ export class ServerManagerView {
436436
initServerActions(): void {
437437
const $serverImgs: NodeListOf<HTMLImageElement> =
438438
document.querySelectorAll(".server-icons");
439-
for (const [index, $serverImg] of $serverImgs.entries()) {
440-
this.addContextMenu($serverImg, index);
439+
for (const $serverImg of $serverImgs) {
440+
this.addContextMenu($serverImg);
441441
if ($serverImg.src === defaultIcon) {
442-
this.displayInitialCharLogo($serverImg, index);
442+
this.displayInitialCharLogo($serverImg);
443443
}
444444

445445
$serverImg.addEventListener("error", () => {
446-
this.displayInitialCharLogo($serverImg, index);
446+
this.displayInitialCharLogo($serverImg);
447447
});
448448
}
449449
}
@@ -495,11 +495,7 @@ export class ServerManagerView {
495495
return tab instanceof ServerTab ? (await tab.webview).properties.url : "";
496496
}
497497

498-
displayInitialCharLogo($img: HTMLImageElement, index: number): void {
499-
// The index parameter is needed because webview[data-tab-id] can
500-
// increment beyond the size of the sidebar org array and throw an
501-
// error
502-
498+
displayInitialCharLogo($img: HTMLImageElement): void {
503499
const $altIcon = document.createElement("div");
504500
const $parent = $img.parentElement!;
505501
const $container = $parent.parentElement!;
@@ -520,7 +516,7 @@ export class ServerManagerView {
520516
$img.remove();
521517
$parent.append($altIcon);
522518

523-
this.addContextMenu($altIcon, index);
519+
this.addContextMenu($altIcon);
524520
}
525521

526522
sidebarHoverEvent(
@@ -824,8 +820,8 @@ export class ServerManagerView {
824820
}
825821
}
826822

827-
async isLoggedIn(index: number): Promise<boolean> {
828-
const tab = this.tabs[index];
823+
async isLoggedIn(tabId: string): Promise<boolean> {
824+
const tab = this.getTabById(tabId);
829825
if (!(tab instanceof ServerTab)) return false;
830826
const webview = await tab.webview;
831827
const url = webview.getWebContents().getURL();
@@ -836,9 +832,16 @@ export class ServerManagerView {
836832
return this.tabs.find((tab) => tab.properties.tabId === tabId);
837833
}
838834

839-
addContextMenu($serverImg: HTMLElement, index: number): void {
835+
addContextMenu($serverImg: HTMLElement): void {
840836
$serverImg.addEventListener("contextmenu", async (event) => {
841837
event.preventDefault();
838+
const tabElement = $serverImg.closest(".tab");
839+
const tabId =
840+
tabElement instanceof HTMLElement
841+
? tabElement.dataset.tabId
842+
: undefined;
843+
if (tabId === undefined) return;
844+
842845
const template = [
843846
{
844847
label: t.__("Disconnect organization"),
@@ -852,11 +855,11 @@ export class ServerManagerView {
852855
),
853856
});
854857
if (response === 0) {
855-
if (DomainUtil.removeDomain(index)) {
858+
if (DomainUtil.removeDomainById(tabId)) {
856859
ipcRenderer.send("reload-full-app");
857860
} else {
858861
const {title, content} = Messages.orgRemovalError(
859-
DomainUtil.getDomain(index).url,
862+
DomainUtil.getDomainById(tabId)!.url,
860863
);
861864
dialog.showErrorBox(title, content);
862865
}
@@ -865,19 +868,19 @@ export class ServerManagerView {
865868
},
866869
{
867870
label: t.__("Notification settings"),
868-
enabled: await this.isLoggedIn(index),
871+
enabled: await this.isLoggedIn(tabId),
869872
click: async () => {
870873
// Switch to tab whose icon was right-clicked
871-
const tab = this.tabs[index];
872-
await this.activateTab(tab.properties.tabId);
874+
const tab = this.getTabById(tabId);
875+
await this.activateTab(tabId);
873876
if (tab instanceof ServerTab)
874877
(await tab.webview).showNotificationSettings();
875878
},
876879
},
877880
{
878881
label: t.__("Copy Zulip URL"),
879882
click() {
880-
clipboard.writeText(DomainUtil.getDomain(index).url);
883+
clipboard.writeText(DomainUtil.getDomainById(tabId)!.url);
881884
},
882885
},
883886
];

app/renderer/js/pages/preference/connected-org-section.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ export function initConnectedOrgSection({
4848
// Show noServerText if no servers are there otherwise hide it
4949
$existingServers.textContent = servers.length === 0 ? noServerText : "";
5050

51-
for (const [i, server] of servers.entries()) {
51+
for (const server of servers) {
5252
initServerInfoForm({
5353
$root: $serverInfoContainer,
5454
server,
55-
index: i,
5655
id: server.id,
5756
onChange: reloadApp,
5857
});

app/renderer/js/pages/preference/server-info-form.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import * as DomainUtil from "../../utils/domain-util.ts";
1111
type ServerInfoFormProperties = {
1212
$root: Element;
1313
server: ServerConfig;
14-
index: number;
1514
id: string;
1615
onChange: () => void;
1716
};
@@ -59,13 +58,14 @@ export function initServerInfoForm(properties: ServerInfoFormProperties): void {
5958
message: t.__("Are you sure you want to disconnect this organization?"),
6059
});
6160
if (response === 0) {
62-
if (DomainUtil.removeDomain(properties.index)) {
61+
if (DomainUtil.removeDomainById(properties.id)) {
6362
ipcRenderer.send("reload-full-app");
6463
} else {
65-
const {title, content} = Messages.orgRemovalError(
66-
DomainUtil.getDomain(properties.index).url,
67-
);
68-
dialog.showErrorBox(title, content);
64+
const domain = DomainUtil.getDomainById(properties.id);
65+
if (domain) {
66+
const {title, content} = Messages.orgRemovalError(domain.url);
67+
dialog.showErrorBox(title, content);
68+
}
6969
}
7070
}
7171
});

app/renderer/js/utils/domain-util.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ export function removeDomains(): void {
121121
reloadDatabase();
122122
}
123123

124-
export function removeDomain(index: number): boolean {
124+
export function removeDomainById(id: string): boolean {
125+
const index = getDomains().findIndex((domain) => domain.id === id);
126+
if (index === -1) {
127+
return false;
128+
}
129+
125130
if (EnterpriseUtil.isPresetOrg(getDomain(index).url)) {
126131
return false;
127132
}

0 commit comments

Comments
 (0)