forked from usebruno/bruno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: persist last opened env per collection
fixes usebruno#112
- Loading branch information
1 parent
c17e4ef
commit 40c6416
Showing
6 changed files
with
97 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/bruno-electron/src/store/last-opened-environment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |