Skip to content

Commit 152323f

Browse files
committed
main: addContextMenu should not require index.
1 parent 49039fe commit 152323f

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(
@@ -813,8 +809,8 @@ export class ServerManagerView {
813809
}
814810
}
815811

816-
async isLoggedIn(index: number): Promise<boolean> {
817-
const tab = this.tabs[index];
812+
async isLoggedIn(tabId: string): Promise<boolean> {
813+
const tab = this.getTabById(tabId);
818814
if (!(tab instanceof ServerTab)) return false;
819815
const webview = await tab.webview;
820816
const url = webview.getWebContents().getURL();
@@ -825,9 +821,16 @@ export class ServerManagerView {
825821
return this.tabs.find((tab) => tab.properties.tabId === tabId);
826822
}
827823

828-
addContextMenu($serverImg: HTMLElement, index: number): void {
824+
addContextMenu($serverImg: HTMLElement): void {
829825
$serverImg.addEventListener("contextmenu", async (event) => {
830826
event.preventDefault();
827+
const tabElement = $serverImg.closest(".tab");
828+
const tabId =
829+
tabElement instanceof HTMLElement
830+
? tabElement.dataset.tabId
831+
: undefined;
832+
if (tabId === undefined) return;
833+
831834
const template = [
832835
{
833836
label: t.__("Disconnect organization"),
@@ -841,11 +844,11 @@ export class ServerManagerView {
841844
),
842845
});
843846
if (response === 0) {
844-
if (DomainUtil.removeDomain(index)) {
847+
if (DomainUtil.removeDomainById(tabId)) {
845848
ipcRenderer.send("reload-full-app");
846849
} else {
847850
const {title, content} = Messages.orgRemovalError(
848-
DomainUtil.getDomain(index).url,
851+
DomainUtil.getDomainById(tabId)!.url,
849852
);
850853
dialog.showErrorBox(title, content);
851854
}
@@ -854,19 +857,19 @@ export class ServerManagerView {
854857
},
855858
{
856859
label: t.__("Notification settings"),
857-
enabled: await this.isLoggedIn(index),
860+
enabled: await this.isLoggedIn(tabId),
858861
click: async () => {
859862
// Switch to tab whose icon was right-clicked
860-
const tab = this.tabs[index];
861-
await this.activateTab(tab.properties.tabId);
863+
const tab = this.getTabById(tabId);
864+
await this.activateTab(tabId);
862865
if (tab instanceof ServerTab)
863866
(await tab.webview).showNotificationSettings();
864867
},
865868
},
866869
{
867870
label: t.__("Copy Zulip URL"),
868871
click() {
869-
clipboard.writeText(DomainUtil.getDomain(index).url);
872+
clipboard.writeText(DomainUtil.getDomainById(tabId)!.url);
870873
},
871874
},
872875
];

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)