diff --git a/static/crossroad.mts b/static/crossroad.mts index beb6e88..e7daae0 100644 --- a/static/crossroad.mts +++ b/static/crossroad.mts @@ -5,20 +5,42 @@ import storageManager from "./storage_manager.mjs"; const radioButtonName = "instanceSelect"; -export function getMainDialog(): HTMLDialogElement { - return document.getElementById('mainDialog') as HTMLDialogElement; -} - const detailsDialog = findDialogOrFail(document.body, "#instanceDetails"); const addDialog = findDialogOrFail(document.body, "#addInstance"); const instanceSelectForm = findFormOrFail(document.body, "#instanceSelectForm"); const redirectButton = findButtonOrFail(document.body, "#redirect"); const redirectAlwaysButton = findButtonOrFail(document.body, "#redirectAlways"); +redirectButton.addEventListener("click", e => { + // Can be assumed to not fail because the button is disabled if there are no options and the first one is selected by default + redirect(getSelectedOption()!); +}); + +redirectAlwaysButton.addEventListener("click", e => { + // Can be assumed to not fail because the button is disabled if there are no options and the first one is selected by default + const option = getSelectedOption()!; + setAutoRedirect(option); + redirect(option); +}); + +export const getMainDialog = () => findDialogOrFail(document.body, "#mainDialog"); + export const { showAddInstanceDialog, hideAddInstanceDialog -} = initializeAddInstanceFlow(detailsDialog, addDialog); +} = ((): { + showAddInstanceDialog: () => void, + hideAddInstanceDialog: () => void +} => { + // Don't bother initializing if we're performing autoredirect + if (autoRedirect()) return { + showAddInstanceDialog: () => { }, + hideAddInstanceDialog: () => { } + } + createInstanceSelectOptions(); + storageManager.addSaveCallback(createInstanceSelectOptions); + return initializeAddInstanceFlow(detailsDialog, addDialog) +})(); function createInstanceSelectOptions() { instanceSelectForm.replaceChildren(); // Erase all child nodes @@ -53,15 +75,12 @@ function createInstanceSelectOptions() { setRedirectButtonState(firstInput !== null); } -createInstanceSelectOptions(); -storageManager.addSaveCallback(createInstanceSelectOptions); - function setRedirectButtonState(enabled: boolean) { redirectButton.disabled = !enabled; redirectAlwaysButton.disabled = !enabled; } -export function getTargetSoftwareOrGroup(): string { +function getTargetSoftwareOrGroup(): string { const currentURL = URL.parse(location.href)!; const target = currentURL.pathname.match(/\/+([^\/]*)\/?/)?.[1]; if (target == null) throw new Error("Crossroad was served on an invalid path (likely a backend routing mistake)"); @@ -85,14 +104,16 @@ function getSelectedOption(): string | null { } } -function autoRedirect() { +function autoRedirect(): boolean { const targetSoftware = getTargetSoftwareOrGroup(); const preferredFor = storageManager.storage.instances.find(instance => instance.preferredFor?.includes(targetSoftware)); - if (preferredFor) redirect(preferredFor.origin); + if (preferredFor) { + redirect(preferredFor.origin); + return true; + } + return false; } -autoRedirect(); - function setAutoRedirect(option: string) { const instance = storageManager.storage.instances.find(e => e.origin === option); if (!instance) throw new Error("Invalid argument"); @@ -101,21 +122,9 @@ function setAutoRedirect(option: string) { storageManager.save(); } -redirectAlwaysButton.addEventListener("click", e => { - // Can be assumed to not fail because the button is disabled if there are no options and the first one is selected by default - const option = getSelectedOption()!; - setAutoRedirect(option); - redirect(option); -}); - function redirect(to: string) { const url = URL.parse(to); if (url === null) throw new Error("Couldn't parse destination"); url.pathname = getTargetPath(); location.href = url.toString(); } - -redirectButton.addEventListener("click", e => { - // Can be assumed to not fail because the button is disabled if there are no options and the first one is selected by default - redirect(getSelectedOption()!); -});