11import React , { useState } from 'react' ;
2- import { ChakraProvider , Box , Textarea , Button , VStack , HStack , Avatar , Text , Link as ChakraLink , Heading } from '@chakra-ui/react' ;
2+ import { ChakraProvider , Box , Textarea , Button , VStack , HStack , Avatar , Text , Link as ChakraLink , Heading , useColorMode , useColorModeValue } from '@chakra-ui/react' ;
33import ReactMarkdown from 'react-markdown' ;
44import MonacoEditor from '@monaco-editor/react' ; // Monaco Editor
55
66function ChatApp ( ) {
77 const [ messages , setMessages ] = useState ( [ ] ) ;
88 const [ input , setInput ] = useState ( "" ) ;
9+ const { colorMode, toggleColorMode } = useColorMode ( ) ; // Chakra color mode
910
1011 const sendMessage = ( ) => {
1112 if ( input . trim ( ) !== "" ) {
@@ -20,32 +21,30 @@ function ChatApp() {
2021 }
2122 } ;
2223
24+ // Dynamic theme for Monaco Editor based on Chakra UI color mode
25+ const monacoTheme = colorMode === 'dark' ? 'vs-dark' : 'vs-light' ;
26+
2327 // Custom renderers for Markdown components using Chakra UI and Monaco Editor for code blocks
2428 const renderers = {
25- // Render headers using Chakra's Heading component
2629 h1 : ( { children } ) => < Heading as = "h1" size = "xl" mb = { 2 } > { children } </ Heading > ,
2730 h2 : ( { children } ) => < Heading as = "h2" size = "lg" mb = { 2 } > { children } </ Heading > ,
2831 h3 : ( { children } ) => < Heading as = "h3" size = "md" mb = { 2 } > { children } </ Heading > ,
29-
30- // Render links using Chakra's Link component
3132 a : ( { href, children } ) => (
3233 < ChakraLink href = { href } color = "blue.500" isExternal >
3334 { children }
3435 </ ChakraLink >
3536 ) ,
36-
37- // Use Monaco Editor for code blocks
3837 code : ( { inline, children, className } ) => {
3938 const language = className ? className . replace ( 'language-' , '' ) : 'plaintext' ;
4039 if ( inline ) {
41- return < Text as = "code" bg = "gray.100" p = "1" borderRadius = "md" > { children } </ Text > ;
40+ return < Text as = "code" bg = { useColorModeValue ( "gray.100" , "gray.700" ) } p = "1" borderRadius = "md" > { children } </ Text > ;
4241 }
4342 return (
44- < Box border = "1px solid" borderColor = "gray.200" borderRadius = "md" overflow = "hidden" my = { 2 } >
43+ < Box border = "1px solid" borderColor = { useColorModeValue ( "gray.200" , "gray.600" ) } borderRadius = "md" overflow = "hidden" my = { 2 } >
4544 < MonacoEditor
4645 height = "200px"
4746 language = { language }
48- theme = "vs-dark"
47+ theme = { monacoTheme }
4948 value = { String ( children ) . trim ( ) }
5049 options = { {
5150 readOnly : true ,
@@ -61,11 +60,11 @@ function ChatApp() {
6160
6261 return (
6362 < ChakraProvider >
64- < Box bg = "gray.100" h = "100vh" p = { 4 } display = "flex" flexDirection = "column" >
63+ < Box bg = { useColorModeValue ( "gray.100" , "gray.900" ) } h = "100vh" p = { 4 } display = "flex" flexDirection = "column" >
6564 { /* Chat Messages Display */ }
6665 < VStack
6766 flex = "1"
68- bg = "white"
67+ bg = { useColorModeValue ( "white" , "gray.800" ) }
6968 w = "100%"
7069 overflowY = "auto"
7170 p = { 4 }
@@ -76,11 +75,10 @@ function ChatApp() {
7675 { messages . map ( ( message ) => (
7776 < HStack key = { message . id } align = "start" w = "100%" >
7877 < Avatar name = { message . sender } size = "sm" />
79- < Box bg = "blue.50" p = { 3 } borderRadius = "lg" width = "80%" >
78+ < Box bg = { useColorModeValue ( "blue.50" , "blue.900" ) } p = { 3 } borderRadius = "lg" width = "80%" >
8079 < Text fontSize = "sm" fontWeight = "bold" >
8180 { message . sender }
8281 </ Text >
83- { /* Rendering the message text with markdown support */ }
8482 < ReactMarkdown components = { renderers } children = { message . text } />
8583 < Text fontSize = "xs" color = "gray.500" >
8684 { message . timestamp }
@@ -96,7 +94,7 @@ function ChatApp() {
9694 value = { input }
9795 onChange = { ( e ) => setInput ( e . target . value ) }
9896 placeholder = "Type a message with Markdown..."
99- bg = "white"
97+ bg = { useColorModeValue ( "white" , "gray.800" ) }
10098 resize = "none"
10199 overflow = "hidden"
102100 flex = "1"
0 commit comments