import React, { useState, useEffect } from 'react'; import { SearchHistoryItem, KnowledgeGroup } from '../types'; import { searchHistoryService } from '../services/searchHistoryService'; import { useToast } from '../contexts/ToastContext'; import { MessageCircle, Trash2, Clock, Users } from 'lucide-react'; interface SearchHistoryListProps { groups: KnowledgeGroup[]; onSelectHistory: (historyId: string) => void; onDeleteHistory?: (historyId: string) => void; } export const SearchHistoryList: React.FC = ({ groups, onSelectHistory, onDeleteHistory }) => { const [histories, setHistories] = useState([]); const [loading, setLoading] = useState(true); const [page, setPage] = useState(1); const [hasMore, setHasMore] = useState(true); const { showToast } = useToast(); const loadHistories = async (pageNum: number = 1, append: boolean = false) => { try { setLoading(true); const response = await searchHistoryService.getHistories(pageNum, 20); if (append) { setHistories(prev => [...prev, ...response.histories]); } else { setHistories(response.histories); } setHasMore(response.histories.length === 20); setPage(pageNum); } catch (error) { showToast('加载搜索历史失败', 'error'); } finally { setLoading(false); } }; useEffect(() => { loadHistories(); }, []); const handleDelete = async (historyId: string, e: React.MouseEvent) => { e.stopPropagation(); if (!confirm('确定要删除这条对话历史吗?')) return; try { await searchHistoryService.deleteHistory(historyId); setHistories(prev => prev.filter(h => h.id !== historyId)); onDeleteHistory?.(historyId); showToast('对话历史删除成功', 'success'); } catch (error) { showToast('删除对话历史失败', 'error'); } }; const loadMore = () => { if (!loading && hasMore) { loadHistories(page + 1, true); } }; const formatDate = (dateString: string) => { const date = new Date(dateString); const now = new Date(); const diffMs = now.getTime() - date.getTime(); const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)); if (diffDays === 0) { return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' }); } else if (diffDays === 1) { return '昨天'; } else if (diffDays < 7) { return `${diffDays}天前`; } else { return date.toLocaleDateString('zh-CN'); } }; const getGroupNames = (selectedGroups: string[] | null) => { if (!selectedGroups || selectedGroups.length === 0) { return '全部分组'; } return selectedGroups .map(id => groups.find(g => g.id === id)?.name) .filter(Boolean) .join(', '); }; if (loading && histories.length === 0) { return (
加载中...
); } if (histories.length === 0) { return (
暂无对话历史
开始一次对话来创建历史记录
); } return (
{histories.map((history) => (
onSelectHistory(history.id)} className="p-4 bg-white rounded-lg border hover:shadow-sm cursor-pointer transition-all group" >
{history.title}
{history.messageCount} 条消息
{formatDate(history.lastMessageAt)}
{getGroupNames(history.selectedGroups)}
))} {hasMore && ( )}
); };