fix(web): 修复 useChat 无限请求问题
- 用 useRef 存储回调函数,避免依赖变化导致无限循环 - 添加 onSessionNotFound 回调,会话不存在时自动创建新会话 - 限制 WebSocket 重连次数为 5 次 - 添加 web:dev 快捷脚本
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
* 响应式布局:支持桌面端和移动端
|
||||
*/
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { Sidebar } from './components/Sidebar';
|
||||
import { ChatPage } from './pages/Chat';
|
||||
import { FileBrowser } from './components/FileBrowser';
|
||||
@@ -49,6 +49,16 @@ export function App() {
|
||||
setCurrentSessionId(session.id);
|
||||
};
|
||||
|
||||
// 会话不存在时自动创建新会话
|
||||
const handleSessionNotFound = useCallback(async () => {
|
||||
try {
|
||||
const { data: newSession } = await createSession();
|
||||
setCurrentSessionId(newSession.id);
|
||||
} catch (error) {
|
||||
console.error('Failed to create new session:', error);
|
||||
}
|
||||
}, []);
|
||||
|
||||
if (isInitializing) {
|
||||
return (
|
||||
<div className="h-screen flex items-center justify-center bg-gray-900">
|
||||
@@ -116,7 +126,7 @@ export function App() {
|
||||
{/* 聊天区域 */}
|
||||
<div className={`flex-1 min-w-0 ${showFileBrowser ? 'hidden md:block md:w-1/2' : 'w-full'}`}>
|
||||
{currentSessionId ? (
|
||||
<ChatPage key={currentSessionId} sessionId={currentSessionId} />
|
||||
<ChatPage key={currentSessionId} sessionId={currentSessionId} onSessionNotFound={handleSessionNotFound} />
|
||||
) : (
|
||||
<div className="flex-1 flex items-center justify-center h-full">
|
||||
<p className="text-gray-400">Select or create a session</p>
|
||||
|
||||
Reference in New Issue
Block a user