1
1
import React , { useState } from 'react' ;
2
2
import { ChakraProvider , Box , Textarea , Button , VStack , HStack , Avatar , Text , Link as ChakraLink , Heading } from '@chakra-ui/react' ;
3
3
import ReactMarkdown from 'react-markdown' ;
4
+ import MonacoEditor from '@monaco-editor/react' ; // Monaco Editor
4
5
5
6
function ChatApp ( ) {
6
7
const [ messages , setMessages ] = useState ( [ ] ) ;
@@ -19,7 +20,7 @@ function ChatApp() {
19
20
}
20
21
} ;
21
22
22
- // Custom renderers for Markdown components using Chakra UI
23
+ // Custom renderers for Markdown components using Chakra UI and Monaco Editor for code blocks
23
24
const renderers = {
24
25
// Render headers using Chakra's Heading component
25
26
h1 : ( { children } ) => < Heading as = "h1" size = "xl" mb = { 2 } > { children } </ Heading > ,
@@ -32,6 +33,30 @@ function ChatApp() {
32
33
{ children }
33
34
</ ChakraLink >
34
35
) ,
36
+
37
+ // Use Monaco Editor for code blocks
38
+ code : ( { inline, children, className } ) => {
39
+ const language = className ? className . replace ( 'language-' , '' ) : 'plaintext' ;
40
+ if ( inline ) {
41
+ return < Text as = "code" bg = "gray.100" p = "1" borderRadius = "md" > { children } </ Text > ;
42
+ }
43
+ return (
44
+ < Box border = "1px solid" borderColor = "gray.200" borderRadius = "md" overflow = "hidden" my = { 2 } >
45
+ < MonacoEditor
46
+ height = "200px"
47
+ language = { language }
48
+ theme = "vs-dark"
49
+ value = { String ( children ) . trim ( ) }
50
+ options = { {
51
+ readOnly : true ,
52
+ minimap : { enabled : false } ,
53
+ scrollBeyondLastLine : false ,
54
+ lineNumbers : "off" ,
55
+ } }
56
+ />
57
+ </ Box >
58
+ ) ;
59
+ } ,
35
60
} ;
36
61
37
62
return (
@@ -51,7 +76,7 @@ function ChatApp() {
51
76
{ messages . map ( ( message ) => (
52
77
< HStack key = { message . id } align = "start" w = "100%" >
53
78
< Avatar name = { message . sender } size = "sm" />
54
- < Box bg = "blue.50" p = { 3 } borderRadius = "lg" maxW = "80%" >
79
+ < Box bg = "blue.50" p = { 3 } borderRadius = "lg" width = "80%" >
55
80
< Text fontSize = "sm" fontWeight = "bold" >
56
81
{ message . sender }
57
82
</ Text >
@@ -72,8 +97,8 @@ function ChatApp() {
72
97
onChange = { ( e ) => setInput ( e . target . value ) }
73
98
placeholder = "Type a message with Markdown..."
74
99
bg = "white"
75
- resize = "none" // Disable manual resize
76
- overflow = "hidden" // Hide overflow when max height is reached
100
+ resize = "none"
101
+ overflow = "hidden"
77
102
flex = "1"
78
103
/>
79
104
< Button colorScheme = "blue" onClick = { sendMessage } >
0 commit comments