Skip to content

Commit 8f63f9e

Browse files
committed
tab: Rename index to order.
After introducing id field to the server object, index was just an indicator of the ordering of tabs in the tab list. This commit changes the name to accurately represent the field's function. This commit should make it easier to change the order of tabs in the future. Since order is not the same as index anymore, we add an additional function called getTabByOrder to replace the leftover instances of us fetching a tab by it's index when it meant to fetch a tab by it's id. After this commit, we should not have any instances of accessing tabs directly by their index in the server list or the tab list.
1 parent f28afbb commit 8f63f9e

File tree

5 files changed

+38
-24
lines changed

5 files changed

+38
-24
lines changed

app/common/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export type TabData = {
2929
role: TabRole;
3030
page?: TabPage;
3131
label: string;
32-
index: number;
32+
order: number;
3333
id: string;
3434
serverId?: string;
3535
};

app/main/menu.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ function getWindowSubmenu(
323323
initialSubmenu.push({
324324
label: tab.label,
325325
accelerator:
326-
tab.role === "function" ? "" : `${shortcutKey} + ${tab.index + 1}`,
326+
tab.role === "function" ? "" : `${shortcutKey} + ${tab.order + 1}`,
327327
checked: tab.id === activeTabId,
328328
click(_item, focusedWindow) {
329329
if (focusedWindow) {
@@ -699,24 +699,28 @@ async function checkForUpdate(): Promise<void> {
699699
await appUpdater(true);
700700
}
701701

702+
function getTabByOrder(tabs: TabData[], order: number): TabData | undefined {
703+
return tabs.find((tab) => tab.order === order);
704+
}
705+
702706
function getNextServer(tabs: TabData[], activeTabId: string): string {
703707
const activeTab = tabs.find((tab) => tab.id === activeTabId)!;
704-
let {index} = activeTab;
708+
let {order} = activeTab;
705709
do {
706-
index = (index + 1) % tabs.length;
707-
} while (tabs[index]?.role !== "server");
710+
order = (order + 1) % tabs.length;
711+
} while (getTabByOrder(tabs, order)?.role !== "server");
708712

709-
return tabs[index].serverId!;
713+
return getTabByOrder(tabs, order)!.serverId!;
710714
}
711715

712716
function getPreviousServer(tabs: TabData[], activeTabId: string): string {
713717
const activeTab = tabs.find((tab) => tab.id === activeTabId)!;
714-
let {index} = activeTab;
718+
let {order} = activeTab;
715719
do {
716-
index = (index - 1 + tabs.length) % tabs.length;
717-
} while (tabs[index]?.role !== "server");
720+
order = (order - 1) % tabs.length;
721+
} while (getTabByOrder(tabs, order)?.role !== "server");
718722

719-
return tabs[index].serverId!;
723+
return getTabByOrder(tabs, order)!.serverId!;
720724
}
721725

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ export default class ServerTab extends Tab {
8080

8181
generateShortcutText(): string {
8282
// Only provide shortcuts for server [0..9]
83-
if (this.properties.index >= 9) {
83+
if (this.properties.order >= 9) {
8484
return "";
8585
}
8686

87-
const shownIndex = this.properties.index + 1;
87+
const shownIndex = this.properties.order + 1;
8888

89-
// Array index == Shown index - 1
89+
// Array index == Shown order - 1
9090
ipcRenderer.send("switch-server-tab", this.serverId);
9191

9292
return process.platform === "darwin"

app/renderer/js/components/tab.ts

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

app/renderer/js/main.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ export class ServerManagerView {
333333
}
334334

335335
// Open last active tab
336-
const firstTab = this.tabs[0];
336+
const firstTab = this.getTabByOrder(this.tabs, 0)!;
337337
let lastActiveTabId = ConfigUtil.getConfigItem(
338338
"lastActiveTabId",
339339
firstTab.properties.tabId,
@@ -343,22 +343,22 @@ export class ServerManagerView {
343343
// It will be undefined if user disconnected the server for lastActiveTab.
344344
if (
345345
lastActiveTab === undefined ||
346-
lastActiveTab.properties.index >= servers.length
346+
lastActiveTab.properties.order >= servers.length
347347
) {
348348
lastActiveTabId = firstTab.properties.tabId;
349349
}
350350

351351
// `webview.load()` for lastActiveTabId before the others
352352
await this.activateTab(lastActiveTabId);
353353
await Promise.all(
354-
servers.map(async (server, i) => {
354+
servers.map(async (server) => {
355355
// After the lastActiveTabId is activated, we load the others in the background
356356
// without activating them, to prevent flashing of server icons
357357
if (server.id === lastActiveTabId) {
358358
return;
359359
}
360360

361-
const tab = this.tabs[i];
361+
const tab = this.getTabByServerId(server.id);
362362
if (tab instanceof ServerTab) (await tab.webview).load();
363363
}),
364364
);
@@ -372,15 +372,15 @@ export class ServerManagerView {
372372
}
373373
}
374374

375-
initServer(server: ServerConfig, index: number): ServerTab {
375+
initServer(server: ServerConfig, order: number): ServerTab {
376376
const tabId = this.generateTabId();
377377
const tab: ServerTab = new ServerTab({
378378
role: "server",
379379
icon: DomainUtil.iconAsUrl(server.icon),
380380
label: server.alias,
381381
$root: this.$tabsContainer,
382382
onClick: this.activateLastTab.bind(this, tabId),
383-
index,
383+
order,
384384
tabId,
385385
serverId: server.id,
386386
onHover: this.onHover.bind(this, tabId),
@@ -561,7 +561,7 @@ export class ServerManagerView {
561561
return;
562562
}
563563

564-
const index = this.tabs.length;
564+
const order = this.tabs.length;
565565
const tabId = this.generateTabId();
566566
this.functionalTabs.set(tabProperties.page, tabId);
567567
const $view = await tabProperties.makeView();
@@ -574,7 +574,7 @@ export class ServerManagerView {
574574
label: tabProperties.label,
575575
page: tabProperties.page,
576576
$root: this.$tabsContainer,
577-
index,
577+
order,
578578
tabId,
579579
onClick: this.activateTab.bind(this, tabId),
580580
onDestroy: async () => {
@@ -657,12 +657,19 @@ export class ServerManagerView {
657657
role: tab.properties.role,
658658
page: tab.properties.page,
659659
label: tab.properties.label,
660-
index: tab.properties.index,
660+
order: tab.properties.order,
661661
id: tab.properties.tabId,
662662
serverId: tab instanceof ServerTab ? tab.serverId : undefined,
663663
}));
664664
}
665665

666+
getTabByOrder(
667+
tabs: ServerOrFunctionalTab[],
668+
order: number,
669+
): ServerOrFunctionalTab | undefined {
670+
return tabs.find((tab) => tab.properties.order === order);
671+
}
672+
666673
async activateTab(id: string, hideOldTab = true): Promise<void> {
667674
const tab = this.getTabById(id);
668675
if (!tab) {
@@ -737,7 +744,10 @@ export class ServerManagerView {
737744

738745
// Issue #188: If the functional tab was not focused, do not activate another tab.
739746
if (this.activeTabId === tabId) {
740-
await this.activateTab(this.tabs[0].properties.tabId, false);
747+
await this.activateTab(
748+
this.getTabByOrder(this.tabs, 0)!.properties.tabId,
749+
false,
750+
);
741751
}
742752
}
743753

0 commit comments

Comments
 (0)