修复Dashboard数据加载问题:添加错误日志、修复strict mode重复渲染导致的selectedId闭包问题

This commit is contained in:
yml2213
2026-07-14 11:40:58 +08:00
parent 280dd48f3f
commit caf14e8e52
+9 -5
View File
@@ -1,4 +1,4 @@
import { useState, useEffect } from 'react' import { useState, useEffect, useRef } from 'react'
import { Input, Spin, message } from 'antd' import { Input, Spin, message } from 'antd'
import { SearchOutlined, UserOutlined, StarFilled } from '@ant-design/icons' import { SearchOutlined, UserOutlined, StarFilled } from '@ant-design/icons'
import { useAuth } from '@/stores/auth' import { useAuth } from '@/stores/auth'
@@ -29,21 +29,25 @@ const Dashboard = () => {
const [detail, setDetail] = useState<SessionDetail | null>(null) const [detail, setDetail] = useState<SessionDetail | null>(null)
const [detailLoading, setDetailLoading] = useState(false) const [detailLoading, setDetailLoading] = useState(false)
const [messageInput, setMessageInput] = useState('') const [messageInput, setMessageInput] = useState('')
const initialLoad = useRef(true)
useEffect(() => { useEffect(() => {
loadSessions() loadSessions()
}, []) }, [])
const loadSessions = async () => { const loadSessions = async () => {
setLoading(true)
try { try {
setLoading(true)
const res = await getSessions() const res = await getSessions()
console.log('Sessions loaded:', res)
setSessions(res.list) setSessions(res.list)
if (res.list.length > 0 && !selectedId) { if (res.list.length > 0 && initialLoad.current) {
setSelectedId(res.list[0].id) setSelectedId(res.list[0].id)
initialLoad.current = false
} }
} catch { } catch (err) {
// fallback to empty console.error('Failed to load sessions:', err)
message.error('加载会话失败,请重新登录')
} finally { } finally {
setLoading(false) setLoading(false)
} }