From fad953a983640fe60fd067e935cf635532d11ae2 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sun, 16 Oct 2022 02:06:58 +0530 Subject: [PATCH] feat: auto hydrate last opened collections --- .../App/useLocalCollectionTreeSync.js | 2 + .../ReduxStore/slices/collections/index.js | 7 +++- .../bruno-electron/src/app/collections.js | 4 +- .../src/app/last-opened-collections.js | 42 +++++++++++++++++++ packages/bruno-electron/src/index.js | 7 +++- .../src/ipc/local-collection.js | 33 ++++++++++++--- 6 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 packages/bruno-electron/src/app/last-opened-collections.js diff --git a/packages/bruno-app/src/providers/App/useLocalCollectionTreeSync.js b/packages/bruno-app/src/providers/App/useLocalCollectionTreeSync.js index baeccabe0..ac4f73404 100644 --- a/packages/bruno-app/src/providers/App/useLocalCollectionTreeSync.js +++ b/packages/bruno-app/src/providers/App/useLocalCollectionTreeSync.js @@ -60,6 +60,8 @@ const useLocalCollectionTreeSync = () => { toast.success('Collection is already opened under local collections'); }; + ipcRenderer.invoke('renderer:ready'); + const removeListener1 = ipcRenderer.on('main:collection-opened', _openCollection); const removeListener2 = ipcRenderer.on('main:collection-tree-updated', _collectionTreeUpdated); const removeListener3 = ipcRenderer.on('main:collection-already-opened', _collectionAlreadyOpened); diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js index 7a72f55b3..2b19a56d6 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js @@ -32,7 +32,7 @@ export const collectionsSlice = createSlice({ loadCollections: (state, action) => { each(action.payload.collections, (c) => collapseCollection(c)); each(action.payload.collections, (c) => addDepth(c.items)); - state.collections = action.payload.collections; + each(action.payload.collections, (c) => state.collections.push(c)); }, collectionImported: (state, action) => { const { collection } = action.payload; @@ -41,7 +41,10 @@ export const collectionsSlice = createSlice({ state.collections.push(collection); }, createCollection: (state, action) => { - state.collections.push(action.payload); + const collection = action.payload; + collapseCollection(collection); + addDepth(collection.items); + state.collections.push(collection); }, renameCollection: (state, action) => { const collection = findCollectionByUid(state.collections, action.payload.collectionUid); diff --git a/packages/bruno-electron/src/app/collections.js b/packages/bruno-electron/src/app/collections.js index ee48a47f9..da649eb21 100644 --- a/packages/bruno-electron/src/app/collections.js +++ b/packages/bruno-electron/src/app/collections.js @@ -1,5 +1,5 @@ const { uuid } = require('../utils/common'); -const { dialog } = require('electron'); +const { dialog, ipcMain } = require('electron'); const { isDirectory, normalizeAndResolvePath } = require('../utils/filesystem'); const openCollection = async (win, watcher) => { @@ -13,7 +13,7 @@ const openCollection = async (win, watcher) => { if(!watcher.hasWatcher(resolvedPath)) { const uid = uuid(); win.webContents.send('main:collection-opened', resolvedPath, uid); - watcher.addWatcher(win, resolvedPath, uid); + ipcMain.emit('main:collection-opened', win, resolvedPath, uid); } else { win.webContents.send('main:collection-already-opened', resolvedPath); } diff --git a/packages/bruno-electron/src/app/last-opened-collections.js b/packages/bruno-electron/src/app/last-opened-collections.js new file mode 100644 index 000000000..30ebe2fb2 --- /dev/null +++ b/packages/bruno-electron/src/app/last-opened-collections.js @@ -0,0 +1,42 @@ +const _ = require('lodash'); +const Store = require('electron-store'); +const { isDirectory } = require('../utils/filesystem'); + +class LastOpenedCollections { + constructor() { + this.store = new Store({ + name: 'preferences', + clearInvalidConfig: true + }); + } + + getAll() { + return this.store.get('lastOpenedCollections') || []; + } + + add(collectionPath) { + const collections = this.store.get('lastOpenedCollections') || []; + + if(isDirectory(collectionPath)) { + if(!collections.includes(collectionPath)) { + collections.push(collectionPath); + this.store.set('lastOpenedCollections', collections); + } + } + } + + remove(collectionPath) { + let collections = this.store.get('lastOpenedCollections') || []; + + if(collections.includes(collectionPath)) { + collections = _.filter(collections, c => c !== collectionPath); + this.store.set('lastOpenedCollections', collections); + } + } + + removeAll() { + return this.store.set('lastOpenedCollections', []); + } +}; + +module.exports = LastOpenedCollections; diff --git a/packages/bruno-electron/src/index.js b/packages/bruno-electron/src/index.js index e9bb77b22..1c4fd6489 100644 --- a/packages/bruno-electron/src/index.js +++ b/packages/bruno-electron/src/index.js @@ -5,10 +5,13 @@ const { BrowserWindow, app, Menu } = require('electron'); const { setContentSecurityPolicy } = require('electron-util'); const menuTemplate = require('./app/menu-template'); +const LastOpenedCollections = require('./app/last-opened-collections'); const registerNetworkIpc = require('./ipc/network'); const registerLocalCollectionsIpc = require('./ipc/local-collection'); const Watcher = require('./app/watcher'); +const lastOpenedCollections = new LastOpenedCollections(); + setContentSecurityPolicy(` default-src * 'unsafe-inline' 'unsafe-eval'; script-src * 'unsafe-inline' 'unsafe-eval'; @@ -48,8 +51,8 @@ app.on('ready', async () => { watcher = new Watcher(); // register all ipc handlers - registerNetworkIpc(mainWindow, watcher); - registerLocalCollectionsIpc(mainWindow, watcher); + registerNetworkIpc(mainWindow, watcher, lastOpenedCollections); + registerLocalCollectionsIpc(mainWindow, watcher, lastOpenedCollections); }); // Quit the app once all windows are closed diff --git a/packages/bruno-electron/src/ipc/local-collection.js b/packages/bruno-electron/src/ipc/local-collection.js index 5a5acdda2..c29ef1a00 100644 --- a/packages/bruno-electron/src/ipc/local-collection.js +++ b/packages/bruno-electron/src/ipc/local-collection.js @@ -13,7 +13,7 @@ const { const { uuid, stringifyJson, parseJson } = require('../utils/common'); const { openCollection } = require('../app/collections'); -const registerRendererEventHandlers = (mainWindow, watcher) => { +const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollections) => { // browse directory ipcMain.handle('renderer:browse-directory', async (event, pathname, request) => { try { @@ -47,7 +47,7 @@ const registerRendererEventHandlers = (mainWindow, watcher) => { const uid = uuid(); mainWindow.webContents.send('main:collection-opened', dirPath, uid); - watcher.addWatcher(mainWindow, dirPath, uid); + ipcMain.emit('main:collection-opened', mainWindow, dirPath, uid); return; } catch (error) { @@ -157,19 +157,40 @@ const registerRendererEventHandlers = (mainWindow, watcher) => { watcher.removeWatcher(collectionPath, mainWindow); } }); + + ipcMain.handle('renderer:ready', async (event) => { + // reload last opened collections + const lastOpened = lastOpenedCollections.getAll(); + + if(lastOpened && lastOpened.length) { + for(let collectionPath of lastOpened) { + if(isDirectory(collectionPath)) { + const uid = uuid(); + mainWindow.webContents.send('main:collection-opened', collectionPath, uid); + ipcMain.emit('main:collection-opened', mainWindow, collectionPath, uid); + } + } + } + }); }; -const registerMainEventHandlers = (mainWindow, watcher) => { +const registerMainEventHandlers = (mainWindow, watcher, lastOpenedCollections) => { ipcMain.on('main:open-collection', () => { if(watcher && mainWindow) { openCollection(mainWindow, watcher); } }); + + ipcMain.on('main:collection-opened', (win, pathname, uid) => { + watcher.addWatcher(win, pathname, uid); + lastOpenedCollections.add(pathname); + }); + } -const registerLocalCollectionsIpc = (mainWindow, watcher) => { - registerRendererEventHandlers(mainWindow, watcher); - registerMainEventHandlers(mainWindow, watcher); +const registerLocalCollectionsIpc = (mainWindow, watcher, lastOpenedCollections) => { + registerRendererEventHandlers(mainWindow, watcher, lastOpenedCollections); + registerMainEventHandlers(mainWindow, watcher, lastOpenedCollections); } module.exports = registerLocalCollectionsIpc;