Skip to content

Commit 74edaef

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 74edaef

File tree

9 files changed

+37
-30
lines changed

9 files changed

+37
-30
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": (id: 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": (id: string) => void;
6767
"tab-devtools": () => void;
6868
"toggle-autohide-menubar": (
6969
autoHideMenubar: boolean,

app/common/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ export type TabData = {
3030
page?: TabPage;
3131
label: string;
3232
index: number;
33+
id: string;
3334
};

app/main/menu.ts

Lines changed: 11 additions & 9 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.id);
333333
}
334334
},
335335
type: "checkbox",
@@ -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].id;
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].id;
721723
}
722724

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default class ServerTab extends Tab {
8484
const shownIndex = this.properties.index + 1;
8585

8686
// Array index == Shown index - 1
87-
ipcRenderer.send("switch-server-tab", shownIndex - 1);
87+
ipcRenderer.send("switch-server-tab", this.properties.tabId);
8888

8989
return process.platform === "darwin"
9090
? `⌘${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: 15 additions & 13 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 = server.id;
379377
const tab: ServerTab = new ServerTab({
380378
role: "server",
381379
icon: DomainUtil.iconAsUrl(server.icon),
@@ -483,10 +481,8 @@ export class ServerManagerView {
483481
this.toggleDndButton(dnd);
484482
}
485483

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

492488
async getCurrentActiveServer(): Promise<string> {
@@ -574,7 +570,7 @@ export class ServerManagerView {
574570
const index = this.tabs.length;
575571
this.functionalTabs.set(tabProperties.page, index);
576572

577-
const tabId = this.gettabId();
573+
const tabId = this.generateTabId();
578574
const $view = await tabProperties.makeView();
579575
this.$webviewsContainer.append($view);
580576

@@ -669,7 +665,8 @@ export class ServerManagerView {
669665
page: tab.properties.page,
670666
label: tab.properties.label,
671667
index: tab.properties.index,
672-
}));
668+
id: tab.properties.tabId,
669+
}));
673670
}
674671

675672
async activateTab(index: number, hideOldTab = true): Promise<void> {
@@ -811,14 +808,18 @@ export class ServerManagerView {
811808
}
812809
}
813810

814-
async isLoggedIn(tabId: number): Promise<boolean> {
815-
const tab = this.tabs[tabId];
811+
async isLoggedIn(index: number): Promise<boolean> {
812+
const tab = this.tabs[index];
816813
if (!(tab instanceof ServerTab)) return false;
817814
const webview = await tab.webview;
818815
const url = webview.getWebContents().getURL();
819816
return !(url.endsWith("/login/") || webview.loading);
820817
}
821818

819+
getTabById(tabId: string): ServerOrFunctionalTab | undefined {
820+
return this.tabs.find((tab) => tab.properties.tabId === tabId);
821+
}
822+
822823
addContextMenu($serverImg: HTMLElement, index: number): void {
823824
$serverImg.addEventListener("contextmenu", async (event) => {
824825
event.preventDefault();
@@ -1005,8 +1006,9 @@ export class ServerManagerView {
10051006
ipcRenderer.send("reload-full-app");
10061007
});
10071008

1008-
ipcRenderer.on("switch-server-tab", async (event, index: number) => {
1009-
await this.activateLastTab(index);
1009+
ipcRenderer.on("switch-server-tab", async (event, tabId: string) => {
1010+
const tab = this.getTabById(tabId)!;
1011+
await this.activateLastTab(tab.properties.index);
10101012
});
10111013

10121014
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+
id: server.id,
5657
onChange: reloadApp,
5758
});
5859
}

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

Lines changed: 4 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+
id: string;
1516
onChange: () => void;
1617
};
1718

@@ -70,14 +71,14 @@ 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("forward-message", "switch-server-tab", properties.id);
7475
});
7576

7677
$serverInfoAlias.addEventListener("click", () => {
77-
ipcRenderer.send("forward-message", "switch-server-tab", properties.index);
78+
ipcRenderer.send("forward-message", "switch-server-tab", properties.id);
7879
});
7980

8081
$serverIcon.addEventListener("click", () => {
81-
ipcRenderer.send("forward-message", "switch-server-tab", properties.index);
82+
ipcRenderer.send("forward-message", "switch-server-tab", properties.id);
8283
});
8384
}

0 commit comments

Comments
 (0)