1
+ import React , { useState } from 'react' ;
2
+ import { ChakraProvider , Box , Input , Button , VStack , HStack , Avatar , Text } from '@chakra-ui/react' ;
3
+ import ReactMarkdown from 'react-markdown' ;
4
+
5
+ function ChatAppWithMarkdown ( ) {
6
+ const [ messages , setMessages ] = useState ( [ ] ) ;
7
+ const [ input , setInput ] = useState ( "" ) ;
8
+
9
+ const sendMessage = ( ) => {
10
+ if ( input . trim ( ) !== "" ) {
11
+ const newMessage = {
12
+ id : messages . length + 1 ,
13
+ text : input ,
14
+ sender : 'You' ,
15
+ timestamp : new Date ( ) . toLocaleTimeString ( ) ,
16
+ } ;
17
+ setMessages ( [ ...messages , newMessage ] ) ;
18
+ setInput ( "" ) ;
19
+ }
20
+ } ;
21
+
22
+ return (
23
+ < ChakraProvider >
24
+ < Box bg = "gray.100" h = "100vh" p = { 4 } display = "flex" flexDirection = "column" >
25
+ { /* Chat Messages Display */ }
26
+ < VStack
27
+ flex = "1"
28
+ bg = "white"
29
+ w = "100%"
30
+ overflowY = "auto"
31
+ p = { 4 }
32
+ spacing = { 4 }
33
+ borderRadius = "md"
34
+ boxShadow = "md"
35
+ >
36
+ { messages . map ( ( message ) => (
37
+ < HStack key = { message . id } align = "start" w = "100%" >
38
+ < Avatar name = { message . sender } size = "sm" />
39
+ < Box bg = "blue.50" p = { 3 } borderRadius = "lg" maxW = "80%" >
40
+ < Text fontSize = "sm" fontWeight = "bold" >
41
+ { message . sender }
42
+ </ Text >
43
+ { /* Rendering the message text with markdown support */ }
44
+ < ReactMarkdown children = { message . text } />
45
+ < Text fontSize = "xs" color = "gray.500" >
46
+ { message . timestamp }
47
+ </ Text >
48
+ </ Box >
49
+ </ HStack >
50
+ ) ) }
51
+ </ VStack >
52
+
53
+ { /* Input Section */ }
54
+ < HStack mt = { 4 } spacing = { 2 } >
55
+ < Input
56
+ value = { input }
57
+ onChange = { ( e ) => setInput ( e . target . value ) }
58
+ placeholder = "Type a message with Markdown..."
59
+ bg = "white"
60
+ flex = "1"
61
+ />
62
+ < Button colorScheme = "blue" onClick = { sendMessage } >
63
+ Send
64
+ </ Button >
65
+ </ HStack >
66
+ </ Box >
67
+ </ ChakraProvider >
68
+ ) ;
69
+ }
70
+
71
+ export default ChatAppWithMarkdown ;
0 commit comments