feat: load workspaces from idb

This commit is contained in:
Anoop M D 2022-10-12 04:41:26 +05:30
parent 1b2097250e
commit 008704c4e1
7 changed files with 3489 additions and 30 deletions

View File

@ -19,7 +19,7 @@ const WorkspaceConfigurer = ({onClose}) => {
> >
<ul className="mb-2" > <ul className="mb-2" >
{workspaces && workspaces.length && workspaces.map((workspace) => ( {workspaces && workspaces.length && workspaces.map((workspace) => (
<WorkspaceItem workspace={workspace} /> <WorkspaceItem workspace={workspace} key={workspace.uid} />
))} ))}
</ul> </ul>
{openAddModal && <AddWorkspace onClose={() => setOpenAddModal(false)}/>} {openAddModal && <AddWorkspace onClose={() => setOpenAddModal(false)}/>}

View File

@ -26,7 +26,7 @@ const WorkspaceSelector = () => {
<IconBox size={18} strokeWidth={1.5}/> <IconBox size={18} strokeWidth={1.5}/>
</span> </span>
<span> <span>
{activeWorkspace.name} {activeWorkspace ? activeWorkspace.name : ''}
</span> </span>
</div> </div>
<IconCaretDown className="caret" size={14} strokeWidth={2}/> <IconCaretDown className="caret" size={14} strokeWidth={2}/>
@ -45,7 +45,7 @@ const WorkspaceSelector = () => {
<div className="items-center cursor-pointer"> <div className="items-center cursor-pointer">
<Dropdown onCreate={onDropdownCreate} icon={<Icon />} placement='bottom-end'> <Dropdown onCreate={onDropdownCreate} icon={<Icon />} placement='bottom-end'>
{workspaces && workspaces.length && workspaces.map((workspace) => ( {workspaces && workspaces.length && workspaces.map((workspace) => (
<div className="dropdown-item" onClick={() => handleSelectWorkspace(workspace)}> <div className="dropdown-item" onClick={() => handleSelectWorkspace(workspace)} key={workspace.uid}>
<span>{workspace.name}</span> <span>{workspace.name}</span>
</div> </div>
))} ))}

View File

@ -2,6 +2,7 @@ import { useEffect } from 'react';
import { openDB } from 'idb'; import { openDB } from 'idb';
import { idbConnectionReady } from 'providers/ReduxStore/slices/app' import { idbConnectionReady } from 'providers/ReduxStore/slices/app'
import { loadCollectionsFromIdb } from 'providers/ReduxStore/slices/collections' import { loadCollectionsFromIdb } from 'providers/ReduxStore/slices/collections'
import { loadWorkspacesFromIdb } from 'providers/ReduxStore/slices/workspaces/actions'
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
const useIdb = () => { const useIdb = () => {
@ -9,12 +10,14 @@ const useIdb = () => {
useEffect(() => { useEffect(() => {
let dbName = `grafnode`; let dbName = `grafnode`;
let connection = openDB(dbName, 1, { let connection = openDB(dbName, 2, {
upgrade(db, oldVersion, newVersion, transaction) { upgrade(db, oldVersion, newVersion, transaction) {
switch(oldVersion) { switch(oldVersion) {
case 0: case 0:
const collectionStore = db.createObjectStore('collection', { keyPath: 'uid' }); const collectionStore = db.createObjectStore('collection', { keyPath: 'uid' });
collectionStore.createIndex('transactionIdIndex', 'transaction_id'); collectionStore.createIndex('transactionIdIndex', 'transaction_id');
case 1:
const workspaceStore = db.createObjectStore('workspace', { keyPath: 'uid' });
} }
} }
}); });
@ -24,6 +27,7 @@ const useIdb = () => {
window.__idb = connection; window.__idb = connection;
dispatch(idbConnectionReady()); dispatch(idbConnectionReady());
dispatch(loadCollectionsFromIdb()); dispatch(loadCollectionsFromIdb());
dispatch(loadWorkspacesFromIdb());
}) })
.catch((err) => console.log(err)); .catch((err) => console.log(err));
}, []); }, []);

View File

@ -0,0 +1,15 @@
import { getWorkspacesFromIdb } from 'utils/idb/workspaces';
import {
loadWorkspaces
} from './index';
export const loadWorkspacesFromIdb = () => (dispatch) => {
return new Promise((resolve, reject) => {
getWorkspacesFromIdb(window.__idb)
.then((workspaces) => dispatch(loadWorkspaces({
workspaces: workspaces
})))
.then(resolve)
.catch(reject);
});
};

View File

@ -1,24 +1,31 @@
import { createSlice } from '@reduxjs/toolkit'; import { createSlice } from '@reduxjs/toolkit';
import { each } from 'lodash';
import { uuid } from 'utils/common'; import { uuid } from 'utils/common';
const initialState = { const initialState = {
workspaces: [{ workspaces: [],
uid: 123, activeWorkspaceUid: null
name: 'My Workspace'
},{
uid: 234,
name: 'workspace B'
},{
uid: 345,
name: 'workspace C'
}],
activeWorkspaceUid: 123
}; };
export const workspacesSlice = createSlice({ export const workspacesSlice = createSlice({
name: 'workspaces', name: 'workspaces',
initialState, initialState,
reducers: { reducers: {
loadWorkspaces: (state, action) => {
const workspaces = action.payload.workspaces;
if(!workspaces || !workspaces.length) {
const uid = uuid();
state.workspaces.push({
uid: uid,
name: 'My workspace'
});
state.activeWorkspaceUid = uid;
return;
}
each(workspaces, w => state.workspaces.push(w));
},
selectWorkspace: (state, action) => { selectWorkspace: (state, action) => {
state.activeWorkspaceUid = action.payload.uid; state.activeWorkspaceUid = action.payload.uid;
}, },
@ -45,6 +52,7 @@ export const workspacesSlice = createSlice({
}); });
export const { export const {
loadWorkspaces,
selectWorkspace, selectWorkspace,
renameWorkspace, renameWorkspace,
deleteWorkspace, deleteWorkspace,

View File

@ -0,0 +1,55 @@
import isArray from 'lodash/isArray';
export const saveWorkspaceToIdb = (connection, workspace) => {
return new Promise((resolve, reject) => {
connection
.then((db) => {
let tx = db.transaction(`workspace`, 'readwrite');
let workspaceStore = tx.objectStore('workspace');
if(isArray(workspace)) {
for(let c of workspace) {
workspaceStore.put(c);
}
} else {
workspaceStore.put(workspace);
}
resolve();
})
.catch((err) => reject(err));
});
};
export const deleteWorkspaceInIdb = (connection, workspaceUid) => {
return new Promise((resolve, reject) => {
connection
.then((db) => {
let tx = db.transaction(`workspace`, 'readwrite');
tx.objectStore('workspace').delete(workspaceUid);
tx.oncomplete = () => resolve();
tx.onerror = () => reject(tx.error);
})
.catch((err) => reject(err));
});
};
export const getWorkspacesFromIdb = (connection) => {
return new Promise((resolve, reject) => {
connection
.then((db) => {
let tx = db.transaction('workspace');
let workspaceStore = tx.objectStore('workspace');
return workspaceStore.getAll();
})
.then((workspaces) => {
if(!Array.isArray(workspaces)) {
return new Error('IDB Corrupted');
}
return resolve(workspaces);
})
.catch((err) => reject(err));
});
};

File diff suppressed because it is too large Load Diff