fix: stabilize admin dashboard navigation

This commit is contained in:
linkong
2026-03-31 18:09:52 +08:00
parent e903723877
commit 6f01dfb590
8 changed files with 51 additions and 18 deletions

View File

@@ -90,6 +90,8 @@ const RESTART_GUIDE_LINES: Record<RestartAction, string[]> = {
],
}
let cachedDashboardStats: Stats | null = null
function getRestartConfirmMessage(action: RestartAction): string {
if (action === 'restart-database') {
return '将重启 PostgreSQL 和 Redis页面通常保持在线但相关请求可能短暂波动。'
@@ -102,8 +104,8 @@ function getRestartConfirmMessage(action: RestartAction): string {
function Dashboard() {
const { token, clearAuth, user } = useAuthStore()
const [stats, setStats] = useState<Stats | null>(null)
const [loading, setLoading] = useState(true)
const [stats, setStats] = useState<Stats | null>(cachedDashboardStats)
const [loading, setLoading] = useState(cachedDashboardStats === null)
const [wsConnected, setWsConnected] = useState(false)
const [error, setError] = useState<string | null>(null)
const [restartModalOpen, setRestartModalOpen] = useState(false)
@@ -122,7 +124,9 @@ function Dashboard() {
const fetchStats = async () => {
try {
setLoading(true)
if (!cachedDashboardStats) {
setLoading(true)
}
const res = await fetch('/api/v1/dashboard/stats', {
headers: { Authorization: `Bearer ${token}` },
})
@@ -132,6 +136,7 @@ function Dashboard() {
return
}
const data = await res.json()
cachedDashboardStats = data
setStats(data)
setError(null)
} catch (err) {
@@ -164,7 +169,9 @@ function Dashboard() {
try {
const msg = JSON.parse(event.data)
if (msg.type === 'data_frame' && msg.channel === 'dashboard') {
setStats(msg.payload?.stats as Stats)
const nextStats = msg.payload?.stats as Stats
cachedDashboardStats = nextStats
setStats(nextStats)
}
} catch (e) {
console.error('Parse WS message error:', e)