File size: 919 Bytes
c3a0082
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from 'react';
import CodeBlock from './CodeBlock';

const formatText = (text) => {
  return text
    .replace(/\n/g, "<br>")
    .replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>")
    .replace(/\*(.*?)\*/g, "<em>$1</em>");
};

export default function MessageBubble({ role, content, time }) {
  return (
    <div className={`message ${role}`}>
      <div className="bubble">
        <FormattedContent content={content} />
        <div className="timestamp">{time}</div>
      </div>
    </div>
  );
}

function FormattedContent({ content }) {
  const blocks = content.split('```');

  return (
    <>
      {blocks.map((block, i) =>
        i % 2 === 1 ? (
          <CodeBlock key={i} content={block} />
        ) : (
          <div
            key={i}
            className="formatted-text"
            dangerouslySetInnerHTML={{ __html: formatText(block) }}
          />
        )
      )}
    </>
  );
}