修复Dashboard数据加载问题:添加错误日志、修复strict mode重复渲染导致的selectedId闭包问题
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useState, useEffect, useRef } from 'react'
|
||||
import { Input, Spin, message } from 'antd'
|
||||
import { SearchOutlined, UserOutlined, StarFilled } from '@ant-design/icons'
|
||||
import { useAuth } from '@/stores/auth'
|
||||
@@ -29,21 +29,25 @@ const Dashboard = () => {
|
||||
const [detail, setDetail] = useState<SessionDetail | null>(null)
|
||||
const [detailLoading, setDetailLoading] = useState(false)
|
||||
const [messageInput, setMessageInput] = useState('')
|
||||
const initialLoad = useRef(true)
|
||||
|
||||
useEffect(() => {
|
||||
loadSessions()
|
||||
}, [])
|
||||
|
||||
const loadSessions = async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
setLoading(true)
|
||||
const res = await getSessions()
|
||||
console.log('Sessions loaded:', res)
|
||||
setSessions(res.list)
|
||||
if (res.list.length > 0 && !selectedId) {
|
||||
if (res.list.length > 0 && initialLoad.current) {
|
||||
setSelectedId(res.list[0].id)
|
||||
initialLoad.current = false
|
||||
}
|
||||
} catch {
|
||||
// fallback to empty
|
||||
} catch (err) {
|
||||
console.error('Failed to load sessions:', err)
|
||||
message.error('加载会话失败,请重新登录')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user