feat: auto hydrate last opened collections

This commit is contained in:
Anoop M D 2022-10-16 02:06:58 +05:30
parent 75f6daec06
commit fad953a983
6 changed files with 83 additions and 12 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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;

View File

@ -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

View File

@ -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;