fix: DbSelector 连接后自动选择第一个有数据的 DB,切换 DB 时重新加载键树

- 新增 getDefaultDb(): 优先恢复 lastDbMap > 第一个有数据的 DB > DB0
- onMounted 执行 DB 选择(覆盖首次连接和右键重连场景)
- activeId watcher 改为完整 DB 选择流程(不再硬编码 DB0)
- isConnected watcher 精简为仅加载自定义名称(避免竞态重复调用)
- switchDb() 新增 scanKeys 重新加载键树
This commit is contained in:
2026-07-12 00:28:14 +08:00
parent 76c9758f0a
commit 83159151b9
+36 -10
View File
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { ref, watch, computed } from 'vue'
import { ref, watch, computed, onMounted } from 'vue'
import { useConnectionStore } from '@renderer/stores/connection'
import { useKeyStore } from '@renderer/stores/key'
import { useI18n } from '@renderer/i18n'
@@ -113,14 +113,36 @@ async function fetchDbInfo() {
}
}
// Determine the default DB: saved last DB > first non-empty > 0
function getDefaultDb(): number {
if (!connStore.activeId) return 0
// Restore saved DB if exists
const saved = connStore.lastDbMap[connStore.activeId]
if (saved !== undefined && saved !== null) return saved
// Auto-detect: first non-empty DB
for (let i = 0; i < dbCount.value; i++) {
if ((dbKeyCounts.value[i] ?? 0) > 0) return i
}
return 0
}
// Initialize DB selection on mount (covers initial connect and right-click reconnect)
onMounted(async () => {
if (!connStore.isConnected || !connStore.activeId) return
loadCustomNames()
await fetchDbInfo()
const defaultDb = getDefaultDb()
selectedDb.value = defaultDb
keyStore.currentDb = defaultDb
await window.electronAPI.redis.selectDb(connStore.activeId, defaultDb)
await keyStore.scanKeys(connStore.activeId, '0', true)
})
watch(
() => connStore.isConnected,
async (connected) => {
(connected) => {
if (connected) {
selectedDb.value = 0
keyStore.currentDb = 0
loadCustomNames()
await fetchDbInfo()
}
}
)
@@ -128,12 +150,14 @@ watch(
watch(
() => connStore.activeId,
async () => {
if (!connStore.isConnected || !connStore.activeId) return
loadCustomNames()
if (connStore.isConnected) {
selectedDb.value = 0
keyStore.currentDb = 0
await fetchDbInfo()
}
await fetchDbInfo()
const defaultDb = getDefaultDb()
selectedDb.value = defaultDb
keyStore.currentDb = defaultDb
await window.electronAPI.redis.selectDb(connStore.activeId, defaultDb)
await keyStore.scanKeys(connStore.activeId, '0', true)
}
)
@@ -142,6 +166,8 @@ async function switchDb(db: number) {
selectedDb.value = db
connStore.saveLastDb(connStore.activeId, db)
await keyStore.selectDb(connStore.activeId, db)
// Reload key tree for the new DB
await keyStore.scanKeys(connStore.activeId, '0', true)
}
function formatCount(n: number): string {