From 7934fc275ac11e49a8099e9a936017c6b9a59965 Mon Sep 17 00:00:00 2001 From: Lykin <137850705+tiny-craft@users.noreply.github.com> Date: Sat, 2 Dec 2023 02:03:29 +0800 Subject: [PATCH] perf: save last selected database and restore for next connection --- backend/services/browser_service.go | 5 +++-- backend/services/connection_service.go | 17 +++++++++++++++++ backend/types/connection.go | 1 + frontend/src/components/sidebar/BrowserPane.vue | 6 +++++- frontend/src/stores/browser.js | 13 +++++++++++-- frontend/src/stores/connections.js | 15 +++++++++++++++ 6 files changed, 52 insertions(+), 5 deletions(-) diff --git a/backend/services/browser_service.go b/backend/services/browser_service.go index 26be709..088ca2d 100644 --- a/backend/services/browser_service.go +++ b/backend/services/browser_service.go @@ -198,8 +198,9 @@ func (b *browserService) OpenConnection(name string) (resp types.JSResp) { resp.Success = true resp.Data = map[string]any{ - "db": dbs, - "view": selConn.KeyView, + "db": dbs, + "view": selConn.KeyView, + "lastDB": selConn.LastDB, } return } diff --git a/backend/services/connection_service.go b/backend/services/connection_service.go index 7d3f9b2..6deddfe 100644 --- a/backend/services/connection_service.go +++ b/backend/services/connection_service.go @@ -358,3 +358,20 @@ func (c *connectionService) DeleteGroup(name string, includeConn bool) (resp typ resp.Success = true return } + +// SaveLastDB save last selected database index +func (c *connectionService) SaveLastDB(name string, db int) (resp types.JSResp) { + param := c.conns.GetConnection(name) + if param == nil { + resp.Msg = "no connection named \"" + name + "\"" + return + } + + param.LastDB = db + if err := c.conns.UpdateConnection(name, param.ConnectionConfig); err != nil { + resp.Msg = "save connection fail:" + err.Error() + return + } + resp.Success = true + return +} diff --git a/backend/types/connection.go b/backend/types/connection.go index ff5d5c5..88d2fe1 100644 --- a/backend/types/connection.go +++ b/backend/types/connection.go @@ -5,6 +5,7 @@ type ConnectionCategory int type ConnectionConfig struct { Name string `json:"name" yaml:"name"` Group string `json:"group,omitempty" yaml:"-"` + LastDB int `json:"lastDB" yaml:"last_db"` Addr string `json:"addr,omitempty" yaml:"addr,omitempty"` Port int `json:"port,omitempty" yaml:"port,omitempty"` Username string `json:"username,omitempty" yaml:"username,omitempty"` diff --git a/frontend/src/components/sidebar/BrowserPane.vue b/frontend/src/components/sidebar/BrowserPane.vue index 131fe58..f28bb44 100644 --- a/frontend/src/components/sidebar/BrowserPane.vue +++ b/frontend/src/components/sidebar/BrowserPane.vue @@ -3,7 +3,7 @@ import { useThemeVars } from 'naive-ui' import BrowserTree from './BrowserTree.vue' import IconButton from '@/components/common/IconButton.vue' import useTabStore from 'stores/tab.js' -import { computed, onMounted, reactive, ref, unref, watch } from 'vue' +import { computed, nextTick, onMounted, reactive, ref, unref, watch } from 'vue' import { get, map } from 'lodash' import Refresh from '@/components/icons/Refresh.vue' import useDialogStore from 'stores/dialog.js' @@ -19,6 +19,7 @@ import { useRender } from '@/utils/render.js' import RedisTypeSelector from '@/components/common/RedisTypeSelector.vue' import { types } from '@/consts/support_redis_type.js' import Plus from '@/components/icons/Plus.vue' +import useConnectionStore from 'stores/connections.js' const themeVars = useThemeVars() const i18n = useI18n() @@ -26,6 +27,7 @@ const dialogStore = useDialogStore() // const prefStore = usePreferencesStore() const tabStore = useTabStore() const browserStore = useBrowserStore() +const connectionStore = useConnectionStore() const render = useRender() const currentName = computed(() => get(tabStore.currentTab, 'name', '')) const browserTreeRef = ref(null) @@ -159,6 +161,8 @@ watch( browserTreeRef.value?.resetExpandKey(currentName.value, db) fullyLoaded.value = await browserStore.loadMoreKeys(currentName.value, db) browserTreeRef.value?.refreshTree() + + nextTick().then(() => connectionStore.saveLastDB(currentName.value, db)) } catch (e) { $message.error(e.message) } finally { diff --git a/frontend/src/stores/browser.js b/frontend/src/stores/browser.js index f76caec..c4815f9 100644 --- a/frontend/src/stores/browser.js +++ b/frontend/src/stores/browser.js @@ -255,11 +255,12 @@ const useBrowserStore = defineStore('browser', { // if (connNode == null) { // throw new Error('no such connection') // } - const { db, view = KeyViewType.Tree } = data + const { db, view = KeyViewType.Tree, lastDB } = data if (isEmpty(db)) { throw new Error('no db loaded') } const dbs = [] + let containLastDB = false for (let i = 0; i < db.length; i++) { this._getNodeMap(name, i).clear() this._getKeySet(name, i).clear() @@ -274,10 +275,18 @@ const useBrowserStore = defineStore('browser', { isLeaf: false, children: undefined, }) + if (db[i].index === lastDB) { + containLastDB = true + } } this.databases[name] = dbs this.viewType[name] = view - this.openedDB[name] = get(dbs, '0.db', 0) + // get last selected db + if (containLastDB) { + this.openedDB[name] = lastDB + } else { + this.openedDB[name] = get(dbs, '0.db', 0) + } }, /** diff --git a/frontend/src/stores/connections.js b/frontend/src/stores/connections.js index a738bcc..c235cd2 100644 --- a/frontend/src/stores/connections.js +++ b/frontend/src/stores/connections.js @@ -8,6 +8,7 @@ import { ListConnection, RenameGroup, SaveConnection, + SaveLastDB, SaveSortedConnection, } from 'wailsjs/go/services/connectionService.js' import { ConnectionType } from '@/consts/connection_type.js' @@ -333,6 +334,20 @@ const useConnectionStore = defineStore('connections', { return { success: true } }, + /** + * save last selected database + * @param {string} name + * @param {number} db + * @return {Promise<{success: boolean, [msg]: string}>} + */ + async saveLastDB(name, db) { + const { success, msg } = await SaveLastDB(name, db) + if (!success) { + return { success: false, msg } + } + return { success: true } + }, + /** * get default key filter pattern by server name * @param name