Skip to content

Commit f17e01f

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 50e0074 commit f17e01f

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
@@ -441,14 +441,14 @@ export class ServerManagerView {
441441
initServerActions(): void {
442442
const $serverImgs: NodeListOf<HTMLImageElement> =
443443
document.querySelectorAll(".server-icons");
444-
for (const [index, $serverImg] of $serverImgs.entries()) {
445-
this.addContextMenu($serverImg, index);
444+
for (const $serverImg of $serverImgs) {
445+
this.addContextMenu($serverImg);
446446
if ($serverImg.src === defaultIcon) {
447-
this.displayInitialCharLogo($serverImg, index);
447+
this.displayInitialCharLogo($serverImg);
448448
}
449449

450450
$serverImg.addEventListener("error", () => {
451-
this.displayInitialCharLogo($serverImg, index);
451+
this.displayInitialCharLogo($serverImg);
452452
});
453453
}
454454
}
@@ -500,11 +500,7 @@ export class ServerManagerView {
500500
return tab instanceof ServerTab ? (await tab.webview).properties.url : "";
501501
}
502502

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

528-
this.addContextMenu($altIcon, index);
524+
this.addContextMenu($altIcon);
529525
}
530526

531527
sidebarHoverEvent(
@@ -818,8 +814,8 @@ export class ServerManagerView {
818814
}
819815
}
820816

821-
async isLoggedIn(index: number): Promise<boolean> {
822-
const tab = this.tabs[index];
817+
async isLoggedIn(tabId: string): Promise<boolean> {
818+
const tab = this.getTabById(tabId);
823819
if (!(tab instanceof ServerTab)) return false;
824820
const webview = await tab.webview;
825821
const url = webview.getWebContents().getURL();
@@ -830,9 +826,16 @@ export class ServerManagerView {
830826
return this.tabs.find((tab) => tab.properties.tabId === tabId);
831827
}
832828

833-
addContextMenu($serverImg: HTMLElement, index: number): void {
829+
addContextMenu($serverImg: HTMLElement): void {
834830
$serverImg.addEventListener("contextmenu", async (event) => {
835831
event.preventDefault();
832+
const tabElement = $serverImg.closest(".tab");
833+
const tabId =
834+
tabElement instanceof HTMLElement
835+
? tabElement.dataset.tabId
836+
: undefined;
837+
if (tabId === undefined) return;
838+
836839
const template = [
837840
{
838841
label: t.__("Disconnect organization"),
@@ -846,11 +849,11 @@ export class ServerManagerView {
846849
),
847850
});
848851
if (response === 0) {
849-
if (DomainUtil.removeDomain(index)) {
852+
if (DomainUtil.removeDomainById(tabId)) {
850853
ipcRenderer.send("reload-full-app");
851854
} else {
852855
const {title, content} = Messages.orgRemovalError(
853-
DomainUtil.getDomain(index).url,
856+
DomainUtil.getDomainById(tabId)!.url,
854857
);
855858
dialog.showErrorBox(title, content);
856859
}
@@ -859,19 +862,19 @@ export class ServerManagerView {
859862
},
860863
{
861864
label: t.__("Notification settings"),
862-
enabled: await this.isLoggedIn(index),
865+
enabled: await this.isLoggedIn(tabId),
863866
click: async () => {
864867
// Switch to tab whose icon was right-clicked
865-
const tab = this.tabs[index];
866-
await this.activateTab(tab.properties.tabId);
868+
const tab = this.getTabById(tabId);
869+
await this.activateTab(tabId);
867870
if (tab instanceof ServerTab)
868871
(await tab.webview).showNotificationSettings();
869872
},
870873
},
871874
{
872875
label: t.__("Copy Zulip URL"),
873876
click() {
874-
clipboard.writeText(DomainUtil.getDomain(index).url);
877+
clipboard.writeText(DomainUtil.getDomainById(tabId)!.url);
875878
},
876879
},
877880
];

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: 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)