ChatHistoryList.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* eslint-disable react/prop-types */
  2. import { FaTrash } from 'react-icons/fa';
  3. interface ChatSession {
  4. id: string;
  5. title: string;
  6. createdAt: number;
  7. }
  8. interface ChatHistoryListProps {
  9. sessions: ChatSession[];
  10. onSessionSelect: (sessionId: string) => void;
  11. onSessionDelete: (sessionId: string) => void;
  12. visible: boolean;
  13. isDarkMode?: boolean;
  14. }
  15. const ChatHistoryList: React.FC<ChatHistoryListProps> = ({
  16. sessions,
  17. onSessionSelect,
  18. onSessionDelete,
  19. visible,
  20. isDarkMode = false,
  21. }) => {
  22. if (!visible) return null;
  23. const formatDate = (timestamp: number) => {
  24. const date = new Date(timestamp);
  25. return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' });
  26. };
  27. return (
  28. <div className="h-full overflow-y-auto p-4">
  29. <h2 className={`mb-4 text-lg font-semibold ${isDarkMode ? 'text-gray-200' : 'text-gray-800'}`}>Chat History</h2>
  30. {sessions.length === 0 ? (
  31. <div
  32. className={`rounded-lg ${isDarkMode ? 'bg-slate-800 text-gray-400' : 'bg-white/30 text-gray-500'} p-4 text-center backdrop-blur-sm`}>
  33. No chat history available
  34. </div>
  35. ) : (
  36. <div className="space-y-2">
  37. {sessions.map(session => (
  38. <div
  39. key={session.id}
  40. className={`group relative rounded-lg ${
  41. isDarkMode ? 'bg-slate-800 hover:bg-slate-700' : 'bg-white/50 hover:bg-white/70'
  42. } p-3 transition-all backdrop-blur-sm`}>
  43. <button onClick={() => onSessionSelect(session.id)} className="w-full text-left" type="button">
  44. <h3 className={`text-sm font-medium ${isDarkMode ? 'text-gray-200' : 'text-gray-900'}`}>
  45. {session.title}
  46. </h3>
  47. <p className={`mt-1 text-xs ${isDarkMode ? 'text-gray-400' : 'text-gray-500'}`}>
  48. {formatDate(session.createdAt)}
  49. </p>
  50. </button>
  51. <button
  52. onClick={e => {
  53. e.stopPropagation();
  54. onSessionDelete(session.id);
  55. }}
  56. className={`absolute right-2 top-2 rounded p-1 opacity-0 transition-opacity group-hover:opacity-100 ${
  57. isDarkMode
  58. ? 'bg-slate-700 text-red-400 hover:bg-slate-600'
  59. : 'bg-white text-red-500 hover:bg-gray-100'
  60. }`}
  61. aria-label="Delete session"
  62. type="button">
  63. <FaTrash size={14} />
  64. </button>
  65. </div>
  66. ))}
  67. </div>
  68. )}
  69. </div>
  70. );
  71. };
  72. export default ChatHistoryList;