1- import React , { useState , useRef , useEffect } from 'react' ;
2- import { ChakraProvider , Box , Textarea , Button , VStack , HStack , Avatar , Text } from '@chakra-ui/react' ;
1+ import React , { useState } from 'react' ;
2+ import { ChakraProvider , Box , Textarea , Button , VStack , HStack , Avatar , Text , Link as ChakraLink , Heading } from '@chakra-ui/react' ;
33import ReactMarkdown from 'react-markdown' ;
44
55function ChatApp ( ) {
66 const [ messages , setMessages ] = useState ( [ ] ) ;
77 const [ input , setInput ] = useState ( "" ) ;
8- const textareaRef = useRef ( null ) ;
9-
10- // Maximum height of the textarea should be 30% of the viewport height
11- const maxHeight = window . innerHeight * 0.3 ;
12-
13- // Adjust textarea height based on content
14- useEffect ( ( ) => {
15- if ( textareaRef . current ) {
16- textareaRef . current . style . height = 'auto' ; // Reset the height
17- const newHeight = Math . min ( textareaRef . current . scrollHeight , maxHeight ) ; // Set to scroll height or maxHeight
18- textareaRef . current . style . height = `${ newHeight } px` ;
19- }
20- } , [ input ] ) ; // Recalculate height when input changes
218
229 const sendMessage = ( ) => {
2310 if ( input . trim ( ) !== "" ) {
@@ -32,6 +19,21 @@ function ChatApp() {
3219 }
3320 } ;
3421
22+ // Custom renderers for Markdown components using Chakra UI
23+ const renderers = {
24+ // Render headers using Chakra's Heading component
25+ h1 : ( { children } ) => < Heading as = "h1" size = "xl" mb = { 2 } > { children } </ Heading > ,
26+ h2 : ( { children } ) => < Heading as = "h2" size = "lg" mb = { 2 } > { children } </ Heading > ,
27+ h3 : ( { children } ) => < Heading as = "h3" size = "md" mb = { 2 } > { children } </ Heading > ,
28+
29+ // Render links using Chakra's Link component
30+ a : ( { href, children } ) => (
31+ < ChakraLink href = { href } color = "blue.500" isExternal >
32+ { children }
33+ </ ChakraLink >
34+ ) ,
35+ } ;
36+
3537 return (
3638 < ChakraProvider >
3739 < Box bg = "gray.100" h = "100vh" p = { 4 } display = "flex" flexDirection = "column" >
@@ -54,7 +56,7 @@ function ChatApp() {
5456 { message . sender }
5557 </ Text >
5658 { /* Rendering the message text with markdown support */ }
57- < ReactMarkdown children = { message . text } />
59+ < ReactMarkdown components = { renderers } children = { message . text } />
5860 < Text fontSize = "xs" color = "gray.500" >
5961 { message . timestamp }
6062 </ Text >
@@ -66,13 +68,11 @@ function ChatApp() {
6668 { /* Input Section */ }
6769 < HStack mt = { 4 } spacing = { 2 } align = "end" >
6870 < Textarea
69- ref = { textareaRef }
7071 value = { input }
7172 onChange = { ( e ) => setInput ( e . target . value ) }
7273 placeholder = "Type a message with Markdown..."
7374 bg = "white"
7475 resize = "none" // Disable manual resize
75- maxH = { `${ maxHeight } px` } // Set maximum height
7676 overflow = "hidden" // Hide overflow when max height is reached
7777 flex = "1"
7878 />
0 commit comments