Skip to content

Commit

Permalink
refactor: instance management
Browse files Browse the repository at this point in the history
  • Loading branch information
Belar committed Nov 29, 2024
1 parent 41e053f commit b282df2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
21 changes: 17 additions & 4 deletions src/base/scripts/electron-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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() {
Expand Down
23 changes: 19 additions & 4 deletions src/base/scripts/instance.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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!");
Expand All @@ -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;
}
Expand Down
16 changes: 13 additions & 3 deletions src/process/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@ const ALLOWED_EXTERNAL_URLS = Object.freeze([
"https://github.com/penpot/penpot",
]);

/** @type {string[]} */
const userInstances = [];
/** @type {Set<string>} */
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 }) => {
Expand Down
1 change: 1 addition & 0 deletions src/process/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ contextBridge.exposeInMainWorld(
"OpenHelp",
"OpenOffline",
"registerInstance",
"removeInstance",
];

if (validChannels.includes(channel)) {
Expand Down

0 comments on commit b282df2

Please sign in to comment.