Skip to content

Commit 54c924a

Browse files
committed
domain: getDomainById and updateDomainById.
1 parent b8e3733 commit 54c924a

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));
@@ -1072,7 +1072,7 @@ export class ServerManagerView {
10721072
const tab = this.tabs[index];
10731073
if (tab instanceof ServerTab) tab.setLabel(realmName);
10741074
domain.alias = realmName;
1075-
DomainUtil.updateDomain(index, domain);
1075+
DomainUtil.updateDomainById(domain.id, domain);
10761076
// Update the realm name also on the Window menu
10771077
ipcRenderer.send("update-menu", {
10781078
tabs: this.tabsForIpc,
@@ -1094,7 +1094,7 @@ export class ServerManagerView {
10941094
if (tab instanceof ServerTab)
10951095
tab.setIcon(DomainUtil.iconAsUrl(localIconPath));
10961096
domain.icon = localIconPath;
1097-
DomainUtil.updateDomain(index, domain);
1097+
DomainUtil.updateDomainById(domain.id, domain);
10981098
}
10991099
}),
11001100
);

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)