Skip to content

Commit 7e97ff4

Browse files
committed
renderer: addContextMenu should not require index as an argument.
We also replace pending getDomain() removals in favour of getDomainById() in this commit. We add a new function called removeDomainById as well.
1 parent 465b006 commit 7e97ff4

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

app/renderer/js/main.ts

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

451451
$serverImg.addEventListener("error", () => {
452-
this.displayInitialCharLogo($serverImg, index);
452+
this.displayInitialCharLogo($serverImg);
453453
});
454454
}
455455
}
@@ -501,11 +501,7 @@ export class ServerManagerView {
501501
return tab instanceof ServerTab ? (await tab.webview).properties.url : "";
502502
}
503503

504-
displayInitialCharLogo($img: HTMLImageElement, index: number): void {
505-
// The index parameter is needed because webview[data-tab-id] can
506-
// increment beyond the size of the sidebar org array and throw an
507-
// error
508-
504+
displayInitialCharLogo($img: HTMLImageElement): void {
509505
const $altIcon = document.createElement("div");
510506
const $parent = $img.parentElement!;
511507
const $container = $parent.parentElement!;
@@ -526,7 +522,7 @@ export class ServerManagerView {
526522
$img.remove();
527523
$parent.append($altIcon);
528524

529-
this.addContextMenu($altIcon, index);
525+
this.addContextMenu($altIcon);
530526
}
531527

532528
sidebarHoverEvent(
@@ -820,8 +816,8 @@ export class ServerManagerView {
820816
}
821817
}
822818

823-
async isLoggedIn(index: number): Promise<boolean> {
824-
const tab = this.tabs[index];
819+
async isLoggedIn(tabId: string): Promise<boolean> {
820+
const tab = this.getTabById(tabId);
825821
if (!(tab instanceof ServerTab)) return false;
826822
const webview = await tab.webview;
827823
const url = webview.getWebContents().getURL();
@@ -839,9 +835,16 @@ export class ServerManagerView {
839835
return this.tabs.find((tab) => tab.properties.tabId === tabId);
840836
}
841837

842-
addContextMenu($serverImg: HTMLElement, index: number): void {
838+
addContextMenu($serverImg: HTMLElement): void {
843839
$serverImg.addEventListener("contextmenu", async (event) => {
844840
event.preventDefault();
841+
const tabElement = $serverImg.closest(".tab");
842+
const tabId =
843+
tabElement instanceof HTMLElement
844+
? tabElement.dataset.tabId
845+
: undefined;
846+
if (tabId === undefined) return;
847+
845848
const template = [
846849
{
847850
label: t.__("Disconnect organization"),
@@ -855,11 +858,11 @@ export class ServerManagerView {
855858
),
856859
});
857860
if (response === 0) {
858-
if (DomainUtil.removeDomain(index)) {
861+
if (DomainUtil.removeDomainById(tabId)) {
859862
ipcRenderer.send("reload-full-app");
860863
} else {
861864
const {title, content} = Messages.orgRemovalError(
862-
DomainUtil.getDomain(index).url,
865+
DomainUtil.getDomainById(tabId)!.url,
863866
);
864867
dialog.showErrorBox(title, content);
865868
}
@@ -868,19 +871,19 @@ export class ServerManagerView {
868871
},
869872
{
870873
label: t.__("Notification settings"),
871-
enabled: await this.isLoggedIn(index),
874+
enabled: await this.isLoggedIn(tabId),
872875
click: async () => {
873876
// Switch to tab whose icon was right-clicked
874-
const tab = this.tabs[index];
875-
await this.activateTab(tab.properties.tabId);
877+
const tab = this.getTabById(tabId);
878+
await this.activateTab(tabId);
876879
if (tab instanceof ServerTab)
877880
(await tab.webview).showNotificationSettings();
878881
},
879882
},
880883
{
881884
label: t.__("Copy Zulip URL"),
882885
click() {
883-
clipboard.writeText(DomainUtil.getDomain(index).url);
886+
clipboard.writeText(DomainUtil.getDomainById(tabId)!.url);
884887
},
885888
},
886889
];

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
serverId: 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
serverId: 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.serverId)) {
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.serverId);
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: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,6 @@ export function getDomains(): ServerConfig[] {
8282
}
8383
}
8484

85-
export function getDomain(index: number): ServerConfig {
86-
reloadDatabase();
87-
return serverConfigSchema.parse(
88-
database.getObject<unknown>(`/domains[${index}]`),
89-
);
90-
}
91-
9285
export function getDomainById(id: string): ServerConfig | undefined {
9386
return getDomains().find((server) => server.id === id);
9487
}
@@ -121,8 +114,13 @@ export function removeDomains(): void {
121114
reloadDatabase();
122115
}
123116

124-
export function removeDomain(index: number): boolean {
125-
if (EnterpriseUtil.isPresetOrg(getDomain(index).url)) {
117+
export function removeDomainById(id: string): boolean {
118+
const index = getDomains().findIndex((domain) => domain.id === id);
119+
if (index === -1) {
120+
return false;
121+
}
122+
123+
if (EnterpriseUtil.isPresetOrg(getDomainById(id)!.url)) {
126124
return false;
127125
}
128126

0 commit comments

Comments
 (0)