summaryrefslogtreecommitdiff
path: root/extension/react-app/src/tabs/chat/MessageDiv.tsx
blob: 3543dd937b0292d94e3ed8a7796a03431661e418 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React, { useEffect } from "react";
import { ChatMessage } from "../../redux/store";
import styled from "styled-components";
import {
  buttonColor,
  defaultBorderRadius,
  secondaryDark,
} from "../../components";
import VSCodeFileLink from "../../components/VSCodeFileLink";
import ReactMarkdown from "react-markdown";
import "../../highlight/dark.min.css";
import hljs from "highlight.js";
import { useSelector } from "react-redux";
import { selectIsStreaming } from "../../redux/selectors/chatSelectors";

const Container = styled.div`
  padding-left: 8px;
  padding-right: 8px;
  border-radius: 8px;
  margin: 3px;
  width: fit-content;
  max-width: 75%;
  overflow-y: scroll;
  word-wrap: break-word;
  -ms-word-wrap: break-word;
  height: fit-content;
  overflow: hidden;
  background-color: ${(props) => {
    if (props.role === "user") {
      return buttonColor;
    } else {
      return secondaryDark;
    }
  }};
  float: ${(props) => {
    if (props.role === "user") {
      return "right";
    } else {
      return "left";
    }
  }};
  display: block;

  & pre {
    border: 1px solid gray;
    border-radius: ${defaultBorderRadius};
  }
`;

function MessageDiv(props: ChatMessage) {
  const [richContent, setRichContent] = React.useState<JSX.Element[]>([]);
  const isStreaming = useSelector(selectIsStreaming);

  useEffect(() => {
    if (!isStreaming) {
      hljs.highlightAll();
    }
  }, [richContent, isStreaming]);

  useEffect(() => {
    setRichContent([
      <ReactMarkdown key={1} children={props.content}></ReactMarkdown>,
    ]);
  }, [props.content]);

  return (
    <>
      <div className="overflow-auto">
        <Container role={props.role}>{richContent}</Container>
      </div>
    </>
  );
}

export default MessageDiv;