import { sendToMain } from "../../renderer/sendToMain.js"; import { dispatchNotificationEvent, type NotificationEventPayload } from "../../renderer/notificationEventHandler.js"; import { AbstractStateSubscriberWebComponent } from "../util/AbstractStateSubscriberWebComponent.js"; import { registerElement } from "../../renderer/registerElement.js"; import { registerShortcut, shortcutString } from "../../renderer/registerShortcut.js"; export type ImportSimBriefWebComponentState = { simBriefUserName: string; useSimBriefWeather: number; }; export class ImportSimBriefWebComponent extends AbstractStateSubscriberWebComponent { private isInitialized = false; private shortcut: (() => void) | undefined = undefined; private readonly shortcutKey = "b"; private elements!: { simBriefUserName: HTMLInputElement; importSimBrief: HTMLButtonElement; useSimBriefWeather: HTMLSelectElement; dialog: HTMLDialogElement; }; private initialize() { this.classList.add("d-flex", "form-group"); this.innerHTML = `\

Flight plan import

Remember to generate a flight plan using the SimBrief Dispatch before importing.

`; this.elements = { simBriefUserName: this.querySelector("#settings-simbriefusername") as HTMLInputElement, importSimBrief: this.querySelector("#import-simbrief") as HTMLButtonElement, useSimBriefWeather: this.querySelector("#setting-simbrief-weather") as HTMLSelectElement, dialog: this.querySelector("dialog") as HTMLDialogElement, }; } get state(): ImportSimBriefWebComponentState { return { simBriefUserName: this.elements.simBriefUserName.value, useSimBriefWeather: this.elements.useSimBriefWeather.selectedIndex - 1, }; } connectedCallback() { if (!this.isInitialized) { this.initialize(); this.isInitialized = true; } this.subscribeToStateUpdates((state) => { this.elements.simBriefUserName.value = state.config.simBriefUserName; this.elements.useSimBriefWeather.selectedIndex = state.config.useSimBriefWeather + 1; }); this.elements.importSimBrief.addEventListener("click", this.handleClick); this.shortcut = registerShortcut(this.shortcutKey, () => { this.elements.dialog.showModal(); }); } disconnectedCallback(): void { super.disconnectedCallback(); this.removeEventListener("click", this.handleClick); if (this.shortcut) { this.shortcut(); } } private handleClick = async () => { const state = this.state; if (!state.simBriefUserName) { dispatchNotificationEvent(document.body, `Please enter a valid SimBrief username`, "error"); return; } this.elements.dialog.close(); dispatchNotificationEvent( document.body, `Fetching SimBrief settings for user ${state.simBriefUserName}`, "waiting", ); const response = await sendToMain>("flightplan:import-simbrief", state); dispatchNotificationEvent(document.body, response.message, response.type); if (response.type === "success") { dispatchNotificationEvent( document.body, "Please remember to set the initial starting position of your aircraft in the simulator.", "info", ); } }; static registerElement() { registerElement("startgeraet-import-simbrief", ImportSimBriefWebComponent); } }