import { useEffect, useState, useRef } from 'react'; import { useParams } from 'react-router-dom'; import { api } from '../../services/api-client'; interface Message { id: string; senderId: string; senderRole: string; content: string; contentType: string; createdAt: string; } export function ChatPage() { const { id } = useParams<{ id: string }>(); const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const bottomRef = useRef(null); useEffect(() => { if (!id) return; api.get(`/api/consultations/${id}/messages`) .then((r) => setMessages(r.data)) .catch(() => {}); }, [id]); useEffect(() => { bottomRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); const handleSend = async () => { if (!input.trim() || !id) return; try { const res = await api.post(`/api/consultations/${id}/messages`, { content: input }); setMessages((prev) => [...prev, res.data]); setInput(''); } catch { /* ignore */ } }; return (
在线问诊
{messages.map((msg) => (
{msg.content}
{msg.createdAt?.split('T')[1]?.slice(0, 5)}
))}
setInput(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleSend()} placeholder="输入回复..." style={{ flex: 1, padding: '10px 14px', border: '1px solid #ddd', borderRadius: 20, fontSize: 14 }} />
); }