Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: persist last opened env per collection #2238

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/bruno-app/src/providers/App/useIpcEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ const useIpcEvents = () => {

const removeCollectionTreeUpdateListener = ipcRenderer.on('main:collection-tree-updated', _collectionTreeUpdated);

const removeOpenCollectionListener = ipcRenderer.on('main:collection-opened', (pathname, uid, brunoConfig) => {
dispatch(openCollectionEvent(uid, pathname, brunoConfig));
});
const removeOpenCollectionListener = ipcRenderer.on(
'main:collection-opened',
(pathname, uid, brunoConfig, environmentUid) => {
dispatch(openCollectionEvent(uid, pathname, brunoConfig, environmentUid));
}
);

const removeCollectionAlreadyOpenedListener = ipcRenderer.on('main:collection-already-opened', (pathname) => {
toast.success('Collection is already opened');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,8 @@ export const selectEnvironment = (environmentUid, collectionUid) => (dispatch, g
}

dispatch(_selectEnvironment({ environmentUid, collectionUid }));
resolve();

ipcRenderer.invoke('renderer:select-environment', collectionUid, environmentUid).then(resolve).catch(reject);
});
};

Expand Down Expand Up @@ -991,12 +992,13 @@ export const updateBrunoConfig = (brunoConfig, collectionUid) => (dispatch, getS
});
};

export const openCollectionEvent = (uid, pathname, brunoConfig) => (dispatch, getState) => {
export const openCollectionEvent = (uid, pathname, brunoConfig, environmentUid) => (dispatch, getState) => {
const collection = {
version: '1',
uid: uid,
name: brunoConfig.name,
pathname: pathname,
activeEnvironmentUid: environmentUid,
items: [],
collectionVariables: {},
brunoConfig: brunoConfig
Expand Down
6 changes: 4 additions & 2 deletions packages/bruno-electron/src/app/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { dialog, ipcMain } = require('electron');
const Yup = require('yup');
const { isDirectory, normalizeAndResolvePath } = require('../utils/filesystem');
const { generateUidBasedOnHash } = require('../utils/common');
const { getLastOpenedEnvironment } = require('../store/last-opened-environment');

// todo: bruno.json config schema validation errors must be propagated to the UI
const configSchema = Yup.object({
Expand Down Expand Up @@ -70,8 +71,9 @@ const openCollection = async (win, watcher, collectionPath, options = {}) => {
brunoConfig.ignore = ['node_modules', '.git'];
}

win.webContents.send('main:collection-opened', collectionPath, uid, brunoConfig);
ipcMain.emit('main:collection-opened', win, collectionPath, uid, brunoConfig);
const lastOpenedEnvironmentUid = getLastOpenedEnvironment(uid);
win.webContents.send('main:collection-opened', collectionPath, uid, brunoConfig, lastOpenedEnvironmentUid);
ipcMain.emit('main:collection-opened', win, collectionPath, uid, brunoConfig, lastOpenedEnvironmentUid);
} catch (err) {
if (!options.dontSendDisplayErrors) {
win.webContents.send('main:display-error', {
Expand Down
8 changes: 4 additions & 4 deletions packages/bruno-electron/src/app/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { hasBruExtension } = require('../utils/filesystem');
const { bruToEnvJson, bruToJson, collectionBruToJson } = require('../bru');
const { dotenvToJson } = require('@usebruno/lang');

const { uuid } = require('../utils/common');
const { uuid, generateUidBasedOnHash } = require('../utils/common');
const { getRequestUid } = require('../cache/requestUids');
const { decryptString } = require('../utils/encryption');
const { setDotEnvVars } = require('../store/process-env');
Expand Down Expand Up @@ -101,7 +101,7 @@ const addEnvironmentFile = async (win, pathname, collectionUid, collectionPath)

file.data = bruToEnvJson(bruContent);
file.data.name = basename.substring(0, basename.length - 4);
file.data.uid = getRequestUid(pathname);
file.data.uid = generateUidBasedOnHash(pathname);
Copy link
Contributor Author

@leoferreiralima leoferreiralima May 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed! otherwhise the env wil have a different uid every startup.
The env files dont are moved without move the collection so the problem solved with requestUid dont apply for envs


_.each(_.get(file, 'data.variables', []), (variable) => (variable.uid = uuid()));

Expand Down Expand Up @@ -136,7 +136,7 @@ const changeEnvironmentFile = async (win, pathname, collectionUid, collectionPat
const bruContent = fs.readFileSync(pathname, 'utf8');
file.data = bruToEnvJson(bruContent);
file.data.name = basename.substring(0, basename.length - 4);
file.data.uid = getRequestUid(pathname);
file.data.uid = generateUidBasedOnHash(pathname);
_.each(_.get(file, 'data.variables', []), (variable) => (variable.uid = uuid()));

// hydrate environment variables with secrets
Expand Down Expand Up @@ -168,7 +168,7 @@ const unlinkEnvironmentFile = async (win, pathname, collectionUid) => {
name: path.basename(pathname)
},
data: {
uid: getRequestUid(pathname),
uid: generateUidBasedOnHash(pathname),
name: path.basename(pathname).substring(0, path.basename(pathname).length - 4)
}
};
Expand Down
12 changes: 12 additions & 0 deletions packages/bruno-electron/src/ipc/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { generateUidBasedOnHash, stringifyJson, safeParseJSON, safeStringifyJSON
const { moveRequestUid, deleteRequestUid } = require('../cache/requestUids');
const { deleteCookiesForDomain, getDomainsWithCookies } = require('../utils/cookies');
const EnvironmentSecretsStore = require('../store/env-secrets');
const { saveLastOpenedEnvironment, removeLastOpenedEnvironment } = require('../store/last-opened-environment');

const environmentSecretsStore = new EnvironmentSecretsStore();

Expand Down Expand Up @@ -264,6 +265,15 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
}
});

// select environment
ipcMain.handle('renderer:select-environment', async (event, collectionUid, environmentUid) => {
try {
await saveLastOpenedEnvironment({ collectionUid, environmentUid });
} catch (error) {
return Promise.reject(error);
}
});

// rename environment
ipcMain.handle('renderer:rename-environment', async (event, collectionPathname, environmentName, newName) => {
try {
Expand Down Expand Up @@ -400,6 +410,8 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
console.log(`watcher stopWatching: ${collectionPath}`);
watcher.removeWatcher(collectionPath, mainWindow);
lastOpenedCollections.remove(collectionPath);

removeLastOpenedEnvironment(generateUidBasedOnHash(collectionPath));
}
});

Expand Down
67 changes: 67 additions & 0 deletions packages/bruno-electron/src/store/last-opened-environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const Yup = require('yup');
const Store = require('electron-store');

const lastOpenedEnvironmentSchema = Yup.object().shape({
environmentUid: Yup.string().nullable(),
collectionUid: Yup.string()
});

class LastOpenedEnvironmentsStore {
constructor() {
this.store = new Store({
name: 'preferences',
clearInvalidConfig: true
});
}

get() {
return this.store.get('lastOpenedEnvironments') || {};
}

add({ environmentUid, collectionUid }) {
const lastOpenedEnvironments = this.get();

lastOpenedEnvironments[collectionUid] = environmentUid;

this.store.set('lastOpenedEnvironments', lastOpenedEnvironments);
}

remove(collectionUid) {
const lastOpenedEnvironments = this.get();

delete lastOpenedEnvironments[collectionUid];

this.store.set('lastOpenedEnvironments', lastOpenedEnvironments);
}
}

const lastOpenedEnvironmentsStore = new LastOpenedEnvironmentsStore();

const getLastOpenedEnvironment = (collectionUid) => {
const lastOpenedEnvironments = lastOpenedEnvironmentsStore.get();
return lastOpenedEnvironments[collectionUid];
};

const removeLastOpenedEnvironment = (collectionUid) => {
lastOpenedEnvironmentsStore.remove(collectionUid);
};

const saveLastOpenedEnvironment = async (newLastOpenedEnvironment) => {
return new Promise((resolve, reject) => {
lastOpenedEnvironmentSchema
.validate(newLastOpenedEnvironment, { abortEarly: true })
.then((validatedLastOpenedEnvironment) => {
lastOpenedEnvironmentsStore.add(validatedLastOpenedEnvironment);
resolve();
})
.catch((error) => {
reject(error);
});
});
};

module.exports = {
getLastOpenedEnvironment,
removeLastOpenedEnvironment,
saveLastOpenedEnvironment
};