CustomerGroupChat.tsx
4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { useCallback, useEffect, useRef, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { listGroupMessages, type GroupMessageItem } from '../api/agent';
const PAGE_SIZE = 100;
export function CustomerGroupChat() {
const { groupId } = useParams<{ groupId: string }>();
const navigate = useNavigate();
const scrollRef = useRef<HTMLDivElement>(null);
const [messages, setMessages] = useState<GroupMessageItem[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [page, setPage] = useState(1);
const [hasMore, setHasMore] = useState(true);
const load = useCallback(async (p: number) => {
if (!groupId) return;
setLoading(true);
setError(null);
try {
const data = await listGroupMessages(Number(groupId), { page: p, page_size: PAGE_SIZE });
if (p === 1) {
setMessages(data);
} else {
setMessages((prev) => [...data, ...prev]);
}
setHasMore(data.length >= PAGE_SIZE);
} catch (err: unknown) {
setError(err instanceof Error ? err.message : '加载失败');
} finally {
setLoading(false);
}
}, [groupId]);
useEffect(() => {
load(1);
}, [load]);
// Auto-scroll to bottom when messages first load or new messages appear
useEffect(() => {
if (scrollRef.current && page === 1) {
requestAnimationFrame(() => {
if (scrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
});
}
}, [messages, page]);
function handleLoadMore() {
const next = page + 1;
setPage(next);
load(next);
}
function formatTime(ts: string): string {
if (!ts) return '';
const d = new Date(ts);
const pad = (n: number) => String(n).padStart(2, '0');
return `${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
}
return (
<div className="page chat-page">
{/* Header */}
<div className="chat-header">
<button className="chat-back" onClick={() => navigate('/customer-groups')}>
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
<polyline points="15 18 9 12 15 6" />
</svg>
</button>
<div className="chat-header-info">
<div className="chat-header-title">群聊天记录</div>
<div className="chat-header-sub">群 ID: {groupId}</div>
</div>
</div>
{/* Load More */}
{hasMore && !loading && (
<div className="chat-load-more">
<button className="chat-load-btn" onClick={handleLoadMore}>加载更多</button>
</div>
)}
{loading && <div className="chat-load-more"><span className="chat-loading-text">加载中...</span></div>}
{/* Error */}
{error && (
<div className="alert alert-error" style={{ margin: '8px 16px' }}>
<span className="alert-icon">!</span>
<span>{error}</span>
</div>
)}
{/* Messages */}
<div className="chat-messages" ref={scrollRef}>
{!loading && messages.length === 0 && (
<div className="chat-empty">
<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round">
<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" />
<line x1="9" y1="10" x2="15" y2="10" />
</svg>
<span>暂无聊天记录</span>
</div>
)}
{messages.map((msg) => {
const isBot = msg.bot_send;
return (
<div key={msg.id} className={`chat-msg ${isBot ? 'chat-msg-user' : 'chat-msg-bot'}`}>
<div className="chat-msg-sender">{isBot ? (msg.sender_name || '机器人') : (msg.sender_name || '客户')}</div>
<div className="chat-msg-bubble">
<div className="chat-msg-text">{msg.msg_content || `[${msg.msg_type || '未知类型'}]`}</div>
<div className="chat-msg-time">{formatTime(msg.msg_time)}</div>
</div>
</div>
);
})}
</div>
</div>
);
}