ChatMessage.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import React, { useState } from 'react';
  2. import ReactMarkdown from 'react-markdown';
  3. import remarkGfm from 'remark-gfm';
  4. import { Message, Role } from '../types';
  5. import { Bot, User, AlertCircle, Copy, Check, Search, ChevronDown, ChevronRight } from 'lucide-react';
  6. import { useLanguage } from '../contexts/LanguageContext';
  7. import { ChatSource } from '../types';
  8. interface ChatMessageProps {
  9. message: Message;
  10. onPreviewSource?: (source: ChatSource) => void;
  11. onOpenFile?: (source: ChatSource) => void;
  12. }
  13. const ChatMessage: React.FC<ChatMessageProps> = ({ message, onPreviewSource, onOpenFile }) => {
  14. const { t } = useLanguage();
  15. const isUser = message.role === Role.USER;
  16. const [copied, setCopied] = useState(false);
  17. const [sourcesExpanded, setSourcesExpanded] = useState(false);
  18. const handleCopy = async () => {
  19. try {
  20. await navigator.clipboard.writeText(message.text);
  21. setCopied(true);
  22. setTimeout(() => setCopied(false), 2000);
  23. } catch (err) {
  24. console.error('Failed to copy text: ', err);
  25. }
  26. };
  27. const renderContent = (content: string) => {
  28. return (
  29. <div className="markdown-body text-sm leading-relaxed">
  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. // Check if it's a Mermaid diagram
  37. if (language === 'mermaid') {
  38. return (
  39. <div className="my-4 p-4 bg-slate-50 border border-slate-200 rounded-lg overflow-x-auto">
  40. <pre className="text-xs text-slate-600 font-mono whitespace-pre-wrap">
  41. {String(children).replace(/\n$/, '')}
  42. </pre>
  43. <div className="text-xs text-slate-400 mt-2">
  44. 💡 Mermaid diagram (rendering requires mermaid.js)
  45. </div>
  46. </div>
  47. );
  48. }
  49. // Code block with language
  50. if (!inline && match) {
  51. return (
  52. <div className="my-3">
  53. <div className="flex items-center justify-between bg-slate-700 text-slate-200 px-3 py-1 rounded-t text-xs">
  54. <span className="font-mono">{language}</span>
  55. </div>
  56. <pre className="bg-slate-800 text-slate-100 p-3 rounded-b overflow-x-auto">
  57. <code className={className} {...props}>
  58. {children}
  59. </code>
  60. </pre>
  61. </div>
  62. );
  63. }
  64. // Inline code
  65. return (
  66. <code className="bg-slate-100 text-slate-800 rounded px-1.5 py-0.5 text-xs font-mono" {...props}>
  67. {children}
  68. </code>
  69. );
  70. },
  71. // Style headings
  72. h2: ({ children }) => (
  73. <h2 className="text-lg font-semibold mt-4 mb-2 text-slate-800">{children}</h2>
  74. ),
  75. h3: ({ children }) => (
  76. <h3 className="text-base font-semibold mt-3 mb-2 text-slate-700">{children}</h3>
  77. ),
  78. // Style lists
  79. ul: ({ children }) => (
  80. <ul className="list-disc list-inside space-y-1 my-2">{children}</ul>
  81. ),
  82. ol: ({ children }) => (
  83. <ol className="list-decimal list-inside space-y-1 my-2">{children}</ol>
  84. ),
  85. // Style paragraphs
  86. p: ({ children }) => (
  87. <p className="my-2 leading-relaxed">{children}</p>
  88. ),
  89. // Style tables
  90. table: ({ children }) => (
  91. <div className="overflow-x-auto my-3">
  92. <table className="min-w-full border border-slate-200 rounded">{children}</table>
  93. </div>
  94. ),
  95. th: ({ children }) => (
  96. <th className="border border-slate-200 bg-slate-50 px-3 py-2 text-left text-sm font-semibold">{children}</th>
  97. ),
  98. td: ({ children }) => (
  99. <td className="border border-slate-200 px-3 py-2 text-sm">{children}</td>
  100. ),
  101. }}
  102. >
  103. {content}
  104. </ReactMarkdown>
  105. </div>
  106. );
  107. };
  108. return (
  109. <div className={`flex w-full ${isUser ? 'justify-end' : 'justify-start'} animate-in fade-in slide-in-from-bottom-2 duration-300`}>
  110. <div className={`flex max-w-[85%] md:max-w-[75%] gap-3 ${isUser ? 'flex-row-reverse' : 'flex-row'}`}>
  111. {/* Avatar */}
  112. <div className={`shrink-0 w-8 h-8 rounded-full flex items-center justify-center shadow-sm ${isUser ? 'bg-blue-600 text-white' : 'bg-white border border-slate-200 text-purple-600'
  113. }`}>
  114. {isUser ? <User className="w-5 h-5" /> : <Bot className="w-5 h-5" />}
  115. </div>
  116. {/* Message Bubble + Sources */}
  117. <div className={`flex flex-col ${isUser ? 'items-end' : 'items-start'} group max-w-full`}>
  118. <div className={`relative px-5 py-3.5 rounded-2xl shadow-sm ${isUser
  119. ? 'bg-blue-600 text-white rounded-tr-none'
  120. : message.isError
  121. ? 'bg-red-50 border border-red-200 text-red-700 rounded-tl-none'
  122. : 'bg-white border border-slate-200 text-slate-800 rounded-tl-none'
  123. }`}>
  124. {message.isError && (
  125. <div className="flex items-center gap-2 mb-2 text-red-600 font-semibold">
  126. <AlertCircle className="w-4 h-4" />
  127. <span>{t('errorLabel')}</span>
  128. </div>
  129. )}
  130. <div className={`${isUser ? 'text-white' : 'text-slate-800'}`}>
  131. {renderContent(message.text)}
  132. </div>
  133. {/* Copy Button (Always visible, icon only) */}
  134. <div className={`flex justify-end mt-2 pt-2 border-t ${isUser ? 'border-white/20' : 'border-slate-100/50'}`}>
  135. <button
  136. onClick={handleCopy}
  137. className={`flex items-center justify-center p-1.5 rounded transition-colors ${isUser
  138. ? 'text-blue-100 hover:bg-blue-700'
  139. : 'text-slate-400 hover:bg-slate-100 text-slate-500'
  140. }`}
  141. title={copied ? t('copied') : t('copy')}
  142. >
  143. {copied ? <Check className="w-3.5 h-3.5" /> : <Copy className="w-3.5 h-3.5" />}
  144. </button>
  145. </div>
  146. </div>
  147. {/* Timestamp */}
  148. <span className="text-[10px] text-slate-400 mt-1 px-1">
  149. {new Date(message.timestamp).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
  150. </span>
  151. {/* Sources (Collapsible) */}
  152. {!isUser && message.sources && message.sources.length > 0 && (
  153. <div className="mt-3 w-full border-t border-slate-200 pt-3">
  154. <button
  155. onClick={() => setSourcesExpanded(!sourcesExpanded)}
  156. className="flex items-center gap-2 text-sm text-slate-600 hover:text-slate-800 transition-colors mb-2"
  157. >
  158. {sourcesExpanded ? (
  159. <ChevronDown className="w-4 h-4" />
  160. ) : (
  161. <ChevronRight className="w-4 h-4" />
  162. )}
  163. <Search className="w-4 h-4" />
  164. <span className="font-medium">{t('citationSources')} ({message.sources.length})</span>
  165. </button>
  166. {sourcesExpanded && (
  167. <div className="grid gap-3 pl-6 animate-in slide-in-from-top-2 duration-200">
  168. {message.sources.map((source, index) => (
  169. <div
  170. key={`${source.fileName}-${source.chunkIndex}-${index}`}
  171. className="bg-slate-50 border border-slate-200 rounded-lg p-3 hover:shadow-sm transition-all cursor-pointer hover:border-blue-300 group/source"
  172. onClick={() => onPreviewSource?.(source)}
  173. >
  174. <div className="flex justify-between items-start mb-2">
  175. {source.fileId ? (
  176. <button
  177. onClick={(e) => {
  178. e.stopPropagation();
  179. onOpenFile?.(source);
  180. }}
  181. className="font-medium text-slate-800 text-sm truncate pr-2 hover:text-blue-600 hover:underline text-left pointer-events-auto relative z-10"
  182. title={source.fileName}
  183. >
  184. {source.fileName}
  185. </button>
  186. ) : (
  187. <div className="font-medium text-slate-800 text-sm truncate pr-2" title={source.fileName}>{source.fileName}</div>
  188. )}
  189. <div className="text-xs bg-blue-100 text-blue-700 px-2 py-1 rounded-full shrink-0">
  190. {(source.score * 100).toFixed(1)}%
  191. </div>
  192. </div>
  193. <div className="text-slate-600 text-sm leading-relaxed line-clamp-2">
  194. {source.content}
  195. </div>
  196. <div className="text-xs text-slate-400 mt-2 flex justify-between items-center gap-4">
  197. <div className="flex gap-4">
  198. <span>{t('chunkNumber')} #{source.chunkIndex + 1}</span>
  199. {source.pageNumber !== undefined && (
  200. <span>{t('pageNumber')} {source.pageNumber}</span>
  201. )}
  202. </div>
  203. <span className="text-blue-500 opacity-0 group-hover/source:opacity-100 transition-opacity flex items-center gap-1">
  204. {t('sourcePreview')} &rarr;
  205. </span>
  206. </div>
  207. </div>
  208. ))}
  209. </div>
  210. )}
  211. </div>
  212. )}
  213. </div>
  214. </div>
  215. </div>
  216. );
  217. };
  218. export default ChatMessage;