diff --git a/src/base/scripts/electron-tabs.js b/src/base/scripts/electron-tabs.js index 4d85250..1102b03 100644 --- a/src/base/scripts/electron-tabs.js +++ b/src/base/scripts/electron-tabs.js @@ -22,12 +22,21 @@ window.addEventListener("DOMContentLoaded", async () => { tabGroup?.on("tab-removed", () => { ATWC(); }); + tabGroup?.on("tab-added", () => { + ATWC(); + }); prepareTabReloadButton(); }); window.api.onOpenTab(openTab); +async function resetTabs() { + const tabGroup = await getTabGroup(); + tabGroup?.eachTab((tab) => tab.close(false)); + openTab(); +} + /** * @param {string =} href */ @@ -46,10 +55,14 @@ async function setDefaultTab(href) { async function openTab(href) { const tabGroup = await getTabGroup(); - tabGroup?.addTab({ - ...DEFAULT_TAB_OPTIONS, - ...(href ? { src: href } : {}), - }); + tabGroup?.addTab( + href + ? { + ...DEFAULT_TAB_OPTIONS, + src: href, + } + : undefined + ); } async function prepareTabReloadButton() { diff --git a/src/base/scripts/instance.js b/src/base/scripts/instance.js index 85e006b..73e4622 100644 --- a/src/base/scripts/instance.js +++ b/src/base/scripts/instance.js @@ -1,4 +1,8 @@ const INSTANCE_STORE_KEY = "Instance"; +const INSTANCE_EVENTS = Object.freeze({ + REGISTER: "registerInstance", + REMOVE: "removeInstance", +}); window.addEventListener("DOMContentLoaded", async () => { const savedInstance = await registerSavedInstance(); @@ -24,14 +28,25 @@ async function prepareSaveButton() { async function saveInstance(trigger) { const isInputButton = trigger instanceof HTMLInputElement; const { instanceField } = await getInstanceSettingsForm(); - const instance = instanceField?.value; + if (instance) { localStorage.setItem(INSTANCE_STORE_KEY, instance); + window.api.send(INSTANCE_EVENTS.REGISTER, instance); + await setDefaultTab(instance); } else { + const savedInstance = localStorage.getItem(INSTANCE_STORE_KEY); + + if (savedInstance) { + window.api.send(INSTANCE_EVENTS.REMOVE, savedInstance); + } + localStorage.removeItem(INSTANCE_STORE_KEY); + await setDefaultTab(); } - + + resetTabs(); + if (isInputButton) { trigger.style.backgroundColor = "#00ff89"; trigger.setAttribute("value", "Saved!"); @@ -48,8 +63,8 @@ async function registerSavedInstance() { if (savedInstance) { const { instanceField } = await getInstanceSettingsForm(); - window.api.send("registerInstance", savedInstance); - + window.api.send(INSTANCE_EVENTS.REGISTER, savedInstance); + if (instanceField) { instanceField.value = savedInstance; } diff --git a/src/process/navigation.js b/src/process/navigation.js index 49fd04b..b682011 100644 --- a/src/process/navigation.js +++ b/src/process/navigation.js @@ -15,18 +15,28 @@ const ALLOWED_EXTERNAL_URLS = Object.freeze([ "https://github.com/penpot/penpot", ]); -/** @type {string[]} */ -const userInstances = []; +/** @type {Set} */ +const userInstances = new Set(); ipcMain.on("registerInstance", (event, instance) => { try { const url = new URL(instance); - userInstances.push(url.origin); + userInstances.clear(); + userInstances.add(url.origin); } catch (error) { console.error(`[ERROR] [IPC.registerInstance] Failed with: ${instance}`); } }); +ipcMain.on("removeInstance", (event, instance) => { + try { + const url = new URL(instance); + userInstances.delete(url.origin); + } catch (error) { + console.error(`[ERROR] [IPC.removeInstance] Failed with: ${instance}`); + } +}); + app.on("web-contents-created", (event, contents) => { // Open links in a new tab or a browser, instead of a new window contents.setWindowOpenHandler(({ url }) => { diff --git a/src/process/preload.js b/src/process/preload.js index ca2e367..5843922 100644 --- a/src/process/preload.js +++ b/src/process/preload.js @@ -13,6 +13,7 @@ contextBridge.exposeInMainWorld( "OpenHelp", "OpenOffline", "registerInstance", + "removeInstance", ]; if (validChannels.includes(channel)) {