ChatMessage.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import React, { useState } from 'react';
  2. import { copyToClipboard } from '../utils/clipboard';
  3. import ReactMarkdown from 'react-markdown';
  4. import remarkGfm from 'remark-gfm';
  5. import { Message, Role } from '../types';
  6. import { Bot, User, AlertCircle, Copy, Check, Search, ChevronDown, ChevronRight, Sparkles } from 'lucide-react';
  7. import { motion, AnimatePresence } from 'framer-motion';
  8. import { useLanguage } from '../contexts/LanguageContext';
  9. import { ChatSource } from '../types';
  10. interface ChatMessageProps {
  11. message: Message;
  12. onPreviewSource?: (source: ChatSource) => void;
  13. onOpenFile?: (source: ChatSource) => void;
  14. }
  15. const ChatMessage: React.FC<ChatMessageProps> = ({ message, onPreviewSource, onOpenFile }) => {
  16. const { t } = useLanguage();
  17. const isUser = message.role === Role.USER;
  18. const [copied, setCopied] = useState(false);
  19. const [sourcesExpanded, setSourcesExpanded] = useState(false);
  20. const handleCopy = async () => {
  21. const success = await copyToClipboard(message.text);
  22. if (success) {
  23. setCopied(true);
  24. setTimeout(() => setCopied(false), 2000);
  25. }
  26. };
  27. const renderContent = (content: string) => {
  28. return (
  29. <div className="markdown-body text-[15px] leading-[1.6] tracking-tight">
  30. <ReactMarkdown
  31. remarkPlugins={[remarkGfm]}
  32. components={{
  33. code({ node, inline, className, children, ...props }: any) {
  34. const match = /language-(\w+)/.exec(className || '');
  35. const language = match ? match[1] : '';
  36. if (language === 'mermaid') {
  37. return (
  38. <div className="my-6 p-5 bg-slate-900/5 border border-slate-200 rounded-2xl overflow-x-auto shadow-inner">
  39. <pre className="text-xs text-slate-600 font-mono whitespace-pre-wrap">
  40. {String(children).replace(/\n$/, '')}
  41. </pre>
  42. <div className="flex items-center gap-2 text-[10px] text-slate-400 mt-4 font-bold uppercase tracking-wider">
  43. <div className="w-4 h-4 bg-slate-200 rounded flex items-center justify-center text-[8px]">M</div>
  44. Mermaid diagram rendering
  45. </div>
  46. </div>
  47. );
  48. }
  49. if (!inline && match) {
  50. return (
  51. <div className="my-5 group/code">
  52. <div className="flex items-center justify-between bg-slate-800 text-slate-300 px-4 py-2 rounded-t-xl text-[10px] font-bold uppercase tracking-widest border-b border-white/5">
  53. <span className="font-mono">{language}</span>
  54. <span className="opacity-0 group-hover/code:opacity-100 transition-opacity text-[10px]">Code Block</span>
  55. </div>
  56. <pre className="bg-[#1E293B] text-slate-100 p-4 rounded-b-xl overflow-x-auto border border-t-0 border-slate-800 shadow-lg">
  57. <code className={className} {...props}>
  58. {children}
  59. </code>
  60. </pre>
  61. </div>
  62. );
  63. }
  64. return (
  65. <code className="bg-blue-50 text-blue-700 rounded-md px-1.5 py-0.5 text-xs font-mono font-bold" {...props}>
  66. {children}
  67. </code>
  68. );
  69. },
  70. h1: ({ children }) => (
  71. <h1 className="text-2xl font-bold mt-8 mb-4 text-slate-900 border-b pb-2 border-slate-100">{children}</h1>
  72. ),
  73. h2: ({ children }) => (
  74. <h2 className="text-xl font-bold mt-6 mb-3 text-slate-800 flex items-start gap-2">
  75. <div className="w-1 h-5 bg-blue-500 rounded-full mt-1 shrink-0" />
  76. <span className="break-words min-w-0">{children}</span>
  77. </h2>
  78. ),
  79. h3: ({ children }) => (
  80. <h3 className="text-lg font-bold mt-5 mb-2 text-slate-800 tracking-tight">{children}</h3>
  81. ),
  82. ul: ({ children }) => (
  83. <ul className="list-disc list-outside ml-5 space-y-2 my-4 text-slate-700">{children}</ul>
  84. ),
  85. ol: ({ children }) => (
  86. <ol className="list-decimal list-outside ml-5 space-y-2 my-4 text-slate-700">{children}</ol>
  87. ),
  88. li: ({ children }) => (
  89. <li className="pl-1">{children}</li>
  90. ),
  91. p: ({ children }) => (
  92. <p className="my-3 leading-[1.7] last:mb-0">{children}</p>
  93. ),
  94. table: ({ children }) => (
  95. <div className="overflow-x-auto my-6 border border-slate-200 rounded-xl shadow-sm">
  96. <table className="min-w-full divide-y divide-slate-200">{children}</table>
  97. </div>
  98. ),
  99. th: ({ children }) => (
  100. <th className="border-b border-slate-200 bg-slate-50/80 px-4 py-3 text-left text-xs font-bold uppercase tracking-wider text-slate-500">{children}</th>
  101. ),
  102. td: ({ children }) => (
  103. <td className="px-4 py-3 text-sm text-slate-600 border-b border-slate-50">{children}</td>
  104. ),
  105. blockquote: ({ children }) => (
  106. <blockquote className="border-l-4 border-blue-200 bg-blue-50/30 px-5 py-3 my-5 rounded-r-xl italic text-slate-600">
  107. {children}
  108. </blockquote>
  109. ),
  110. }}
  111. >
  112. {content}
  113. </ReactMarkdown>
  114. </div>
  115. );
  116. };
  117. return (
  118. <motion.div
  119. initial={{ opacity: 0, y: 10 }}
  120. animate={{ opacity: 1, y: 0 }}
  121. transition={{ duration: 0.4, ease: "easeOut" }}
  122. className={`flex w-full ${isUser ? 'justify-end' : 'justify-start'} mb-2`}
  123. >
  124. <div className={`flex max-w-[90%] md:max-w-[85%] gap-4 ${isUser ? 'flex-row-reverse' : 'flex-row'} items-start`}>
  125. {/* Avatar */}
  126. <div className={`shrink-0 w-9 h-9 rounded-xl flex items-center justify-center transition-all ${isUser ? 'bg-gradient-to-br from-blue-600 to-indigo-600 text-white shadow-blue-200 shadow-md' : 'bg-white border border-slate-200 text-indigo-600 shadow-sm'
  127. }`}>
  128. {isUser ? <User className="w-5 h-5" /> : <Sparkles className="w-5 h-5" />}
  129. </div>
  130. {/* Message Bubble + Sources */}
  131. <div className={`flex flex-col ${isUser ? 'items-end' : 'items-start'} group max-w-full`}>
  132. <div className={`relative px-6 py-4 rounded-[24px] shadow-sm transition-all duration-300 ${isUser
  133. ? 'bg-gradient-to-br from-blue-600 to-indigo-700 text-white rounded-tr-none shadow-blue-200/50 hover:shadow-lg hover:shadow-blue-200/50'
  134. : message.isError
  135. ? 'bg-red-50 border border-red-200 text-red-700 rounded-tl-none'
  136. : 'bg-white/80 backdrop-blur-md border border-slate-200/60 text-slate-800 rounded-tl-none hover:shadow-md hover:border-slate-300/50'
  137. }`}>
  138. {message.isError && (
  139. <div className="flex items-center gap-2 mb-3 text-red-600 font-bold uppercase text-[10px] tracking-widest">
  140. <AlertCircle className="w-4 h-4" />
  141. <span>{t('errorLabel')}</span>
  142. </div>
  143. )}
  144. <div className={`${isUser ? 'text-blue-50' : 'text-slate-800'}`}>
  145. {renderContent(message.text)}
  146. </div>
  147. {/* Copy Button (Always visible, icon only) */}
  148. <div className={`flex justify-end mt-3 pt-3 border-t ${isUser ? 'border-white/10' : 'border-slate-100/50'}`}>
  149. <button
  150. onClick={handleCopy}
  151. className={`flex items-center justify-center p-2 rounded-lg transition-all ${isUser
  152. ? 'text-blue-200 hover:bg-white/10 hover:text-white'
  153. : 'text-slate-400 hover:bg-slate-50 hover:text-indigo-600'
  154. }`}
  155. title={copied ? t('copied') : t('copy')}
  156. >
  157. {copied ? <Check className="w-4 h-4" /> : <Copy className="w-4 h-4" />}
  158. </button>
  159. </div>
  160. </div>
  161. {/* Timestamp */}
  162. <span className="text-[10px] font-bold text-slate-400/80 mt-1.5 px-2 uppercase tracking-tight">
  163. {new Date(message.timestamp).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
  164. </span>
  165. {!isUser && message.sources && message.sources.length > 0 && (
  166. <div className="mt-4 w-full pt-2">
  167. <button
  168. onClick={() => setSourcesExpanded(!sourcesExpanded)}
  169. className="flex items-center gap-2 text-[11px] font-bold uppercase tracking-wider text-slate-500 hover:text-indigo-600 transition-all mb-3 group/btn"
  170. >
  171. <div className={`flex items-center justify-center w-5 h-5 rounded-full border border-slate-200 transition-transform ${sourcesExpanded ? 'rotate-90' : 'rotate-0'}`}>
  172. <ChevronRight className="w-3 h-3" />
  173. </div>
  174. <div className="flex items-center gap-1.5">
  175. <Search className="w-3 h-3" />
  176. <span>{t('citationSources')} ({message.sources.length})</span>
  177. </div>
  178. </button>
  179. <AnimatePresence>
  180. {sourcesExpanded && (
  181. <motion.div
  182. initial={{ opacity: 0, height: 0 }}
  183. animate={{ opacity: 1, height: 'auto' }}
  184. exit={{ opacity: 0, height: 0 }}
  185. transition={{ duration: 0.3, ease: "easeInOut" }}
  186. className="grid gap-3 w-full overflow-hidden"
  187. >
  188. {message.sources.map((source, index) => (
  189. <div
  190. key={`${source.fileName}-${source.chunkIndex}-${index}`}
  191. className="bg-white/60 hover:bg-white border border-slate-200/60 rounded-xl p-4 transition-all cursor-pointer hover:shadow-lg hover:shadow-indigo-500/5 hover:border-indigo-200 group/source relative overflow-hidden"
  192. onClick={() => onPreviewSource?.(source)}
  193. >
  194. <div className="absolute left-0 top-0 bottom-0 w-1 bg-indigo-500 opacity-0 group-hover/source:opacity-100 transition-opacity" />
  195. <div className="flex justify-between items-start mb-2.5">
  196. {source.fileId ? (
  197. <button
  198. onClick={(e) => {
  199. e.stopPropagation();
  200. onOpenFile?.(source);
  201. }}
  202. className="font-bold text-slate-900 text-[13px] truncate pr-3 hover:text-indigo-600 transition-colors text-left"
  203. title={source.fileName}
  204. >
  205. {source.fileName}
  206. </button>
  207. ) : (
  208. <div className="font-bold text-slate-900 text-[13px] truncate pr-3" title={source.fileName}>{source.fileName}</div>
  209. )}
  210. <div className="text-[10px] font-black bg-indigo-50 text-indigo-700 px-2.5 py-1 rounded-full shrink-0 border border-indigo-100 shadow-sm uppercase tracking-tighter">
  211. {(source.score * 100).toFixed(1)}%
  212. </div>
  213. </div>
  214. <div className="text-slate-600 text-sm leading-relaxed line-clamp-2 italic font-medium">
  215. &ldquo;{source.content}&rdquo;
  216. </div>
  217. <div className="text-[10px] font-bold text-slate-400 mt-3 flex justify-between items-center uppercase tracking-widest">
  218. <span>{t('chunkNumber')} #{source.chunkIndex + 1}</span>
  219. <span className="text-indigo-600 opacity-0 group-hover/source:opacity-100 transition-all transform translate-x-2 group-hover/source:translate-x-0 flex items-center gap-1">
  220. {t('sourcePreview')} &rarr;
  221. </span>
  222. </div>
  223. </div>
  224. ))}
  225. </motion.div>
  226. )}
  227. </AnimatePresence>
  228. </div>
  229. )}
  230. </div>
  231. </div>
  232. </motion.div>
  233. );
  234. };
  235. export default ChatMessage;