Skip to content

Commit 82483c0

Browse files
committed
domain: Add getDomainById and updateDomainById.
Use id instead of index to get and update domain. We remove updateDomain in this commit but not getDomain since it is being used in other places, where it will be easier to remove when changing the encompassing functions to use id instead of index.
1 parent bbdc51b commit 82483c0

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

app/renderer/js/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export class ServerManagerView {
332332
(async () => {
333333
const serverConfig = await DomainUtil.updateSavedServer(
334334
server.url,
335-
i,
335+
server.id,
336336
);
337337
tab.setLabel(serverConfig.alias);
338338
tab.setIcon(DomainUtil.iconAsUrl(serverConfig.icon));
@@ -1076,7 +1076,7 @@ export class ServerManagerView {
10761076
const tab = this.tabs[index];
10771077
if (tab instanceof ServerTab) tab.setLabel(realmName);
10781078
domain.alias = realmName;
1079-
DomainUtil.updateDomain(index, domain);
1079+
DomainUtil.updateDomainById(domain.id, domain);
10801080
// Update the realm name also on the Window menu
10811081
ipcRenderer.send("update-menu", {
10821082
tabs: this.tabsForIpc,
@@ -1098,7 +1098,7 @@ export class ServerManagerView {
10981098
if (tab instanceof ServerTab)
10991099
tab.setIcon(DomainUtil.iconAsUrl(localIconPath));
11001100
domain.icon = localIconPath;
1101-
DomainUtil.updateDomain(index, domain);
1101+
DomainUtil.updateDomainById(domain.id, domain);
11021102
}
11031103
}),
11041104
);

app/renderer/js/utils/domain-util.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,13 @@ export function getDomain(index: number): ServerConfig {
8989
);
9090
}
9191

92-
export function updateDomain(index: number, server: ServerConfig): void {
93-
reloadDatabase();
94-
serverConfigSchema.parse(server);
92+
export function getDomainById(id: string): ServerConfig | undefined {
93+
return getDomains().find((server) => server.id === id);
94+
}
95+
96+
export function updateDomainById(id: string, server: ServerConfig): void {
97+
const index = getDomains().findIndex((domain) => domain.id === id);
98+
assert.ok(index !== -1, `Domain with id ${id} not found`);
9599
database.push(`/domains[${index}]`, server, true);
96100
}
97101

@@ -164,10 +168,10 @@ export async function saveServerIcon(iconURL: string): Promise<string> {
164168

165169
export async function updateSavedServer(
166170
url: string,
167-
index: number,
171+
id: string,
168172
): Promise<ServerConfig> {
169173
// Does not promise successful update
170-
const serverConfig = getDomain(index);
174+
const serverConfig = getDomainById(id)!;
171175
const oldIcon = serverConfig.icon;
172176
try {
173177
const newServerSetting = await checkDomain(url, true);
@@ -178,7 +182,7 @@ export async function updateSavedServer(
178182
const localIconUrl = await saveServerIcon(newServerConfig.icon);
179183
if (!oldIcon || localIconUrl !== defaultIconSentinel) {
180184
newServerConfig.icon = localIconUrl;
181-
updateDomain(index, newServerConfig);
185+
updateDomainById(id, newServerConfig);
182186
reloadDatabase();
183187
}
184188

0 commit comments

Comments
 (0)