import { useEffect, useState, useRef } from 'react'; import { PageHeader } from '@/components/layout/PageHeader'; import * as consultationService from '@/services/consultation.service'; import type { Consultation, ConsultationMessage, Doctor } from '@/types'; import { formatRelative } from '@/utils/format'; import styles from './ChatPage.module.css'; export function ChatPage() { const [doctor, setDoctor] = useState(null); const [consultation, setConsultation] = useState(null); const [messages, setMessages] = useState([]); const [text, setText] = useState(''); const [sending, setSending] = useState(false); const bottomRef = useRef(null); useEffect(() => { // Get the first available doctor consultationService.getDoctors().then((docs) => { if (docs.length > 0) { const doc = docs[0]; setDoctor(doc); // Find or create consultation consultationService.getConsultation(doc.id).then(async (c) => { if (c) { setConsultation(c); loadMessages(c.id); } else { const newC = await consultationService.startConsultation(doc.id, '在线咨询'); setConsultation(newC); } }); } }); }, []); const loadMessages = (cid: string) => { import('@/services/api-client').then(({ api }) => { api.get(`/api/consultations/${cid}/messages`) .then((res) => setMessages(res.data)); }); }; useEffect(() => { bottomRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); useEffect(() => { // Poll for new messages every 3 seconds if (!consultation?.id) return; const timer = setInterval(() => loadMessages(consultation.id), 3000); return () => clearInterval(timer); }, [consultation?.id]); const handleSend = async () => { if (!text.trim() || !consultation || sending) return; setSending(true); const msgText = text; setText(''); const sent = await consultationService.sendMessage(consultation.id, msgText); setMessages((prev) => [...prev, sent]); setSending(false); }; return (
{messages.length === 0 && (
您好,我是{doctor?.name || '医生'},请问有什么可以帮您?
)} {messages.map((msg) => (
{msg.content}
{formatRelative(msg.createdAt)}
))}
setText(e.target.value)} placeholder="输入消息..." onKeyDown={(e) => e.key === 'Enter' && handleSend()} />
); }