Skip to content

Commit bbdc51b

Browse files
committed
ipc: Use id for switch-server-tab and set tabId to server id.
We want to start using server id to communicate the identity of server in events. This commit does that for `switch-server-tab`. It also requires us to set tabId to server id instead of an incremental number. We try to make changes in this commit minimal and pass index to the functions that switch-server-tab calls. We will replace that index with tabId in further commits incrementally.
1 parent d0f0a66 commit bbdc51b

File tree

9 files changed

+59
-32
lines changed

9 files changed

+59
-32
lines changed

app/common/typed-ipc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type MainMessage = {
1414
"realm-name-changed": (serverURL: string, realmName: string) => void;
1515
"reload-full-app": () => void;
1616
"save-last-tab": (index: number) => void;
17-
"switch-server-tab": (index: number) => void;
17+
"switch-server-tab": (serverId: string) => void;
1818
"toggle-app": () => void;
1919
"toggle-badge-option": (newValue: boolean) => void;
2020
"toggle-menubar": (showMenubar: boolean) => void;
@@ -63,7 +63,7 @@ export type RendererMessage = {
6363
"set-idle": () => void;
6464
"show-keyboard-shortcuts": () => void;
6565
"show-notification-settings": () => void;
66-
"switch-server-tab": (index: number) => void;
66+
"switch-server-tab": (serverId: string) => void;
6767
"tab-devtools": () => void;
6868
"toggle-autohide-menubar": (
6969
autoHideMenubar: boolean,

app/common/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ export type TabData = {
3030
page?: TabPage;
3131
label: string;
3232
index: number;
33+
id: string;
34+
serverId?: string;
3335
};

app/main/menu.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ function getWindowSubmenu(
329329
checked: tab.index === activeTabIndex,
330330
click(_item, focusedWindow) {
331331
if (focusedWindow) {
332-
sendAction("switch-server-tab", tab.index);
332+
sendAction("switch-server-tab", tab.serverId!);
333333
}
334334
},
335335
type: "checkbox",
@@ -348,7 +348,7 @@ function getWindowSubmenu(
348348
if (focusedWindow) {
349349
sendAction(
350350
"switch-server-tab",
351-
getNextServer(tabs, activeTabIndex!),
351+
getNextServer(tabs, tabs[activeTabIndex!]),
352352
);
353353
}
354354
},
@@ -361,7 +361,7 @@ function getWindowSubmenu(
361361
if (focusedWindow) {
362362
sendAction(
363363
"switch-server-tab",
364-
getPreviousServer(tabs, activeTabIndex!),
364+
getPreviousServer(tabs, tabs[activeTabIndex!]),
365365
);
366366
}
367367
},
@@ -704,20 +704,22 @@ async function checkForUpdate(): Promise<void> {
704704
await appUpdater(true);
705705
}
706706

707-
function getNextServer(tabs: TabData[], activeTabIndex: number): number {
707+
function getNextServer(tabs: TabData[], activeTab: TabData): string {
708+
let {index} = activeTab;
708709
do {
709-
activeTabIndex = (activeTabIndex + 1) % tabs.length;
710-
} while (tabs[activeTabIndex]?.role !== "server");
710+
index = (index + 1) % tabs.length;
711+
} while (tabs[index]?.role !== "server");
711712

712-
return activeTabIndex;
713+
return tabs[index].serverId!;
713714
}
714715

715-
function getPreviousServer(tabs: TabData[], activeTabIndex: number): number {
716+
function getPreviousServer(tabs: TabData[], activeTab: TabData): string {
717+
let {index} = activeTab;
716718
do {
717-
activeTabIndex = (activeTabIndex - 1 + tabs.length) % tabs.length;
718-
} while (tabs[activeTabIndex]?.role !== "server");
719+
index = (index - 1 + tabs.length) % tabs.length;
720+
} while (tabs[index]?.role !== "server");
719721

720-
return activeTabIndex;
722+
return tabs[index].serverId!;
721723
}
722724

723725
export function setMenu(properties: MenuProperties): void {

app/renderer/js/components/server-tab.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ import type WebView from "./webview.ts";
99

1010
export type ServerTabProperties = {
1111
webview: Promise<WebView>;
12+
serverId: string;
1213
} & TabProperties;
1314

1415
export default class ServerTab extends Tab {
1516
webview: Promise<WebView>;
17+
serverId: string;
1618
$el: Element;
1719
$name: Element;
1820
$icon: HTMLImageElement;
1921
$badge: Element;
2022

21-
constructor({webview, ...properties}: ServerTabProperties) {
23+
constructor({webview, serverId, ...properties}: ServerTabProperties) {
2224
super(properties);
2325

2426
this.webview = webview;
27+
this.serverId = serverId;
2528
this.$el = generateNodeFromHtml(this.templateHtml());
2629
this.properties.$root.append(this.$el);
2730
this.registerListeners();
@@ -84,7 +87,7 @@ export default class ServerTab extends Tab {
8487
const shownIndex = this.properties.index + 1;
8588

8689
// Array index == Shown index - 1
87-
ipcRenderer.send("switch-server-tab", shownIndex - 1);
90+
ipcRenderer.send("switch-server-tab", this.serverId);
8891

8992
return process.platform === "darwin"
9093
? `⌘${shownIndex}`

app/renderer/js/components/tab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type TabProperties = {
88
$root: Element;
99
onClick: () => void;
1010
index: number;
11-
tabId: number;
11+
tabId: string;
1212
onHover?: () => void;
1313
onHoverOut?: () => void;
1414
materialIcon?: string;

app/renderer/js/components/webview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type WebViewProperties = {
2222
$root: Element;
2323
rootWebContents: WebContents;
2424
index: number;
25-
tabId: number;
25+
tabId: string;
2626
url: string;
2727
role: TabRole;
2828
isActive: () => boolean;

app/renderer/js/main.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ export class ServerManagerView {
8686
activeTabIndex: number;
8787
tabs: ServerOrFunctionalTab[];
8888
functionalTabs: Map<TabPage, number>;
89-
tabId: number;
9089
presetOrgs: string[];
9190
preferenceView?: PreferenceView;
9291
constructor() {
@@ -132,7 +131,6 @@ export class ServerManagerView {
132131
this.tabs = [];
133132
this.presetOrgs = [];
134133
this.functionalTabs = new Map();
135-
this.tabId = 0;
136134
}
137135

138136
async init(): Promise<void> {
@@ -375,7 +373,7 @@ export class ServerManagerView {
375373
}
376374

377375
initServer(server: ServerConfig, index: number): ServerTab {
378-
const tabId = this.gettabId();
376+
const tabId = this.generateTabId();
379377
const tab: ServerTab = new ServerTab({
380378
role: "server",
381379
icon: DomainUtil.iconAsUrl(server.icon),
@@ -384,6 +382,7 @@ export class ServerManagerView {
384382
onClick: this.activateLastTab.bind(this, index),
385383
index,
386384
tabId,
385+
serverId: server.id,
387386
onHover: this.onHover.bind(this, index),
388387
onHoverOut: this.onHoverOut.bind(this, index),
389388
webview: WebView.create({
@@ -483,10 +482,8 @@ export class ServerManagerView {
483482
this.toggleDndButton(dnd);
484483
}
485484

486-
gettabId(): number {
487-
const currentIndex = this.tabId;
488-
this.tabId++;
489-
return currentIndex;
485+
generateTabId(): string {
486+
return DomainUtil.generateDomainId();
490487
}
491488

492489
async getCurrentActiveServer(): Promise<string> {
@@ -574,7 +571,7 @@ export class ServerManagerView {
574571
const index = this.tabs.length;
575572
this.functionalTabs.set(tabProperties.page, index);
576573

577-
const tabId = this.gettabId();
574+
const tabId = this.generateTabId();
578575
const $view = await tabProperties.makeView();
579576
this.$webviewsContainer.append($view);
580577

@@ -669,6 +666,7 @@ export class ServerManagerView {
669666
page: tab.properties.page,
670667
label: tab.properties.label,
671668
index: tab.properties.index,
669+
id: tab.properties.tabId,
672670
}));
673671
}
674672

@@ -811,14 +809,21 @@ export class ServerManagerView {
811809
}
812810
}
813811

814-
async isLoggedIn(tabId: number): Promise<boolean> {
815-
const tab = this.tabs[tabId];
812+
async isLoggedIn(index: number): Promise<boolean> {
813+
const tab = this.tabs[index];
816814
if (!(tab instanceof ServerTab)) return false;
817815
const webview = await tab.webview;
818816
const url = webview.getWebContents().getURL();
819817
return !(url.endsWith("/login/") || webview.loading);
820818
}
821819

820+
getTabByServerId(serverId: string): ServerTab | undefined {
821+
return this.tabs.find(
822+
(tab): tab is ServerTab =>
823+
tab instanceof ServerTab && tab.serverId === serverId,
824+
);
825+
}
826+
822827
addContextMenu($serverImg: HTMLElement, index: number): void {
823828
$serverImg.addEventListener("contextmenu", async (event) => {
824829
event.preventDefault();
@@ -1005,8 +1010,9 @@ export class ServerManagerView {
10051010
ipcRenderer.send("reload-full-app");
10061011
});
10071012

1008-
ipcRenderer.on("switch-server-tab", async (event, index: number) => {
1009-
await this.activateLastTab(index);
1013+
ipcRenderer.on("switch-server-tab", async (event, serverId: string) => {
1014+
const tab = this.getTabByServerId(serverId)!;
1015+
await this.activateLastTab(tab.properties.index);
10101016
});
10111017

10121018
ipcRenderer.on("open-org-tab", async () => {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export function initConnectedOrgSection({
5353
$root: $serverInfoContainer,
5454
server,
5555
index: i,
56+
serverId: server.id,
5657
onChange: reloadApp,
5758
});
5859
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type ServerInfoFormProperties = {
1212
$root: Element;
1313
server: ServerConfig;
1414
index: number;
15+
serverId: string;
1516
onChange: () => void;
1617
};
1718

@@ -70,14 +71,26 @@ export function initServerInfoForm(properties: ServerInfoFormProperties): void {
7071
});
7172

7273
$openServerButton.addEventListener("click", () => {
73-
ipcRenderer.send("forward-message", "switch-server-tab", properties.index);
74+
ipcRenderer.send(
75+
"forward-message",
76+
"switch-server-tab",
77+
properties.serverId,
78+
);
7479
});
7580

7681
$serverInfoAlias.addEventListener("click", () => {
77-
ipcRenderer.send("forward-message", "switch-server-tab", properties.index);
82+
ipcRenderer.send(
83+
"forward-message",
84+
"switch-server-tab",
85+
properties.serverId,
86+
);
7887
});
7988

8089
$serverIcon.addEventListener("click", () => {
81-
ipcRenderer.send("forward-message", "switch-server-tab", properties.index);
90+
ipcRenderer.send(
91+
"forward-message",
92+
"switch-server-tab",
93+
properties.serverId,
94+
);
8295
});
8396
}

0 commit comments

Comments
 (0)