All checks were successful
Build & Test / build-run (push) Successful in 45s
73 lines
2.5 KiB
TypeScript
73 lines
2.5 KiB
TypeScript
import { AddInstanceDialog } from "./add_an_instance.mjs";
|
|
import { dialogDetailsToInstance, InstanceDetailsDialog, InstanceDetailsDialogData } from "./confirm_instance_details.mjs";
|
|
import { Dialog } from "./dialog.mjs";
|
|
import storageManager, { Instance } from "./storage_manager.mjs";
|
|
|
|
export class AddInstanceFlow {
|
|
addDialog: AddInstanceDialog;
|
|
spinnerDialog: Dialog;
|
|
detailsDialog: InstanceDetailsDialog;
|
|
|
|
constructor(
|
|
addDialog: AddInstanceDialog | HTMLDialogElement,
|
|
spinnerDialog: HTMLDialogElement,
|
|
detailsDialog: InstanceDetailsDialog | HTMLDialogElement,
|
|
) {
|
|
if (addDialog instanceof AddInstanceDialog)
|
|
this.addDialog = addDialog;
|
|
else
|
|
this.addDialog = new AddInstanceDialog(addDialog, true);
|
|
|
|
this.spinnerDialog = new Dialog(spinnerDialog);
|
|
|
|
if (detailsDialog instanceof InstanceDetailsDialog)
|
|
this.detailsDialog = detailsDialog;
|
|
else
|
|
this.detailsDialog = new InstanceDetailsDialog(detailsDialog, true);
|
|
}
|
|
|
|
async start(autoSave: boolean) {
|
|
const {
|
|
autoQueryMetadata,
|
|
host,
|
|
secure,
|
|
} = await this.addDialog.present();
|
|
|
|
const detailsDialogData: InstanceDetailsDialogData = {
|
|
name: host,
|
|
host,
|
|
hostSecure: secure,
|
|
software: "",
|
|
iconURL: null,
|
|
preferredFor: []
|
|
};
|
|
|
|
try {
|
|
if (!autoQueryMetadata) throw null; // Skip to catch block
|
|
|
|
this.spinnerDialog.open();
|
|
|
|
const { name, software, iconURL } =
|
|
await fetch(`/api/instance_info/${secure}/${encodeURIComponent(host)}`)
|
|
.then(r => r.json());
|
|
if (
|
|
typeof name !== "string"
|
|
|| typeof software !== "string"
|
|
|| !(typeof iconURL === "string" || iconURL === null) // I guess TS is too stupid to understand this?
|
|
)
|
|
throw new Error("Invalid API response");
|
|
|
|
detailsDialogData.name = name;
|
|
detailsDialogData.software = software;
|
|
detailsDialogData.iconURL = iconURL as string | null;
|
|
} catch { }
|
|
this.spinnerDialog.close();
|
|
|
|
const finalData = await this.detailsDialog.present(detailsDialogData);
|
|
const instance = dialogDetailsToInstance(finalData, {});
|
|
|
|
storageManager.storage.instances.push(instance);
|
|
if (autoSave) storageManager.save();
|
|
console.log("Successfully added new instance:", instance);
|
|
}
|
|
}
|