diff --git a/src/pages/TaskAssistant.tsx b/src/pages/TaskAssistant.tsx index 8d8034a..76544ac 100644 --- a/src/pages/TaskAssistant.tsx +++ b/src/pages/TaskAssistant.tsx @@ -1,5 +1,5 @@ import { useState, useRef, useEffect, useCallback } from 'react' -import { Input, Button, Avatar, Spin, theme, Modal, Tooltip, Typography } from 'antd' +import { Input, Button, Avatar, Spin, theme, Typography, App } from 'antd' import { SendOutlined, RobotOutlined, UserOutlined, PlusOutlined, DeleteOutlined, StopOutlined, MessageOutlined, @@ -20,7 +20,8 @@ interface Message { timestamp: number } -export default function TaskAssistant() { +function ChatPage() { + const { modal } = App.useApp() const [conversations, setConversations] = useState([]) const [activeId, setActiveId] = useState(null) const [messages, setMessages] = useState([]) @@ -28,23 +29,21 @@ export default function TaskAssistant() { const [loading, setLoading] = useState(false) const [editingId, setEditingId] = useState(null) const [editTitle, setEditTitle] = useState('') + const [deleting, setDeleting] = useState(false) const abortRef = useRef(null) const messagesEndRef = useRef(null) const editInputRef = useRef(null) const { token } = theme.useToken() - // Load conversations const loadConversations = useCallback(async () => { try { setConversations(await listConversations()) } catch { /* */ } }, []) useEffect(() => { loadConversations() }, [loadConversations]) - // Scroll to bottom useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }) }, [messages]) - // Load messages when switching conversation const switchConversation = useCallback(async (id: string) => { if (loading) { abortRef.current?.abort(); setLoading(false) } setActiveId(id) @@ -58,7 +57,6 @@ export default function TaskAssistant() { } catch { /* */ } }, [loading]) - // New conversation const handleNew = async () => { if (loading) { abortRef.current?.abort(); setLoading(false) } try { @@ -70,27 +68,31 @@ export default function TaskAssistant() { } catch { /* */ } } - // Delete conversation - const handleDelete = async (id: string) => { - Modal.confirm({ - title: '删除对话', content: '确定要删除这个对话吗?', - okText: '删除', okType: 'danger', cancelText: '取消', + const handleDelete = (id: string) => { + modal.confirm({ + title: '删除对话', + content: '确定要删除这个对话吗?', + okText: '删除', + okType: 'danger', + cancelText: '取消', onOk: async () => { - await deleteConversation(id).catch(() => {}) - setConversations(prev => prev.filter(c => c.id !== id)) - if (activeId === id) { setActiveId(null); setMessages([]) } + setDeleting(true) + try { + await deleteConversation(id) + setConversations(prev => prev.filter(c => c.id !== id)) + if (activeId === id) { setActiveId(null); setMessages([]) } + } catch { /* */ } + setDeleting(false) }, }) } - // Start editing title const startEdit = (conv: Conversation) => { setEditingId(conv.id) setEditTitle(conv.title) setTimeout(() => editInputRef.current?.focus(), 50) } - // Save title const saveTitle = async (id: string) => { const title = editTitle.trim() if (title && title !== conversations.find(c => c.id === id)?.title) { @@ -100,7 +102,6 @@ export default function TaskAssistant() { setEditingId(null) } - // Send message const handleSend = async () => { const text = input.trim() if (!text || loading) return @@ -128,11 +129,10 @@ export default function TaskAssistant() { loadConversations() } - const assistantMsg: Message = { + setMessages(prev => [...prev, { id: (Date.now() + 1).toString(), role: 'assistant', content: result.content, timestamp: Date.now(), - } - setMessages(prev => [...prev, assistantMsg]) + }]) loadConversations() } catch (err: any) { if (err.name === 'AbortError') return @@ -155,7 +155,6 @@ export default function TaskAssistant() { return (
- {/* Sidebar */}
) : ( - { e.stopPropagation(); startEdit(conv) }} - >{conv.title} + { e.stopPropagation(); startEdit(conv) }}> + {conv.title} + )} - - { e.stopPropagation(); handleDelete(conv.id) }} - style={{ fontSize: 12, color: token.colorTextQuaternary, marginLeft: 4, cursor: 'pointer' }} - /> - +
))} {conversations.length === 0 && ( @@ -208,9 +207,7 @@ export default function TaskAssistant() {
- {/* Chat area */}
- {/* Messages */}
- {/* Input area — redesigned */}
{loading ? ( - + ) : ( - + style={{ borderRadius: 8, height: 34, minWidth: 72 }}>发送 )}
@@ -312,3 +302,11 @@ export default function TaskAssistant() { ) } + +export default function TaskAssistant() { + return ( + + + + ) +}