From ecc2252e8430c63965a0ee419d19cbf353854b1f Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sun, 16 Oct 2022 17:24:30 +0530 Subject: [PATCH] feat: persist active workspace in local storage --- .../ReduxStore/slices/workspaces/index.js | 12 +++++++++++- packages/bruno-app/src/utils/common/cache.js | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 packages/bruno-app/src/utils/common/cache.js diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/workspaces/index.js b/packages/bruno-app/src/providers/ReduxStore/slices/workspaces/index.js index 6a478c3a..2d2623ea 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/workspaces/index.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/workspaces/index.js @@ -1,7 +1,9 @@ import { createSlice } from '@reduxjs/toolkit'; import find from 'lodash/find'; +import map from 'lodash/map'; import filter from 'lodash/filter'; import { findCollectionInWorkspace } from 'utils/workspaces'; +import cache from 'utils/common/cache'; const initialState = { workspaces: [], @@ -16,11 +18,19 @@ export const workspacesSlice = createSlice({ state.workspaces = action.payload.workspaces; if(state.workspaces && state.workspaces.length) { - state.activeWorkspaceUid = state.workspaces[0].uid; + const workspaceUids = map(state.workspaces, (w) => w.uid); + const activeWorkspaceUid = cache.getActiveWorkspaceUid(); + if(activeWorkspaceUid && workspaceUids.includes(activeWorkspaceUid)) { + state.activeWorkspaceUid = activeWorkspaceUid; + } else { + state.activeWorkspaceUid = state.workspaces[0].uid; + cache.setActiveWorkspaceUid(state.activeWorkspaceUid); + } } }, selectWorkspace: (state, action) => { state.activeWorkspaceUid = action.payload.workspaceUid; + cache.setActiveWorkspaceUid(state.activeWorkspaceUid); }, renameWorkspace: (state, action) => { const { name, uid } = action.payload; diff --git a/packages/bruno-app/src/utils/common/cache.js b/packages/bruno-app/src/utils/common/cache.js new file mode 100644 index 00000000..30bab61f --- /dev/null +++ b/packages/bruno-app/src/utils/common/cache.js @@ -0,0 +1,18 @@ +class Cache { + get(key) { + return window.localStorage.getItem(key); + } + set(key, val) { + window.localStorage.setItem(key, val); + } + + getActiveWorkspaceUid() { + return this.get('bruno.activeWorkspaceUid'); + } + + setActiveWorkspaceUid(workspaceUid) { + this.set('bruno.activeWorkspaceUid', workspaceUid); + } +}; + +module.exports = new Cache();;