@@ -24,6 +24,7 @@ import { toast } from "sonner";
2424import Logo from "@/shared/components/Logo" ;
2525import { Separator } from "@/shared/components/ui/separator" ;
2626import { notFound } from "next/navigation" ;
27+ import { MathJax , MathJaxContext } from "better-react-mathjax" ;
2728
2829interface Message extends Tables < "Messages" > {
2930 user_id : string ;
@@ -234,95 +235,102 @@ const MessageArea = ({
234235 }
235236 let previousMessageTime : Date | undefined = undefined ;
236237 return (
237- < div className = "mt-5 flex min-h-[400px] w-11/12 flex-1 flex-col place-self-center rounded border p-4 text-gray-800 shadow dark:text-white max-[500px]:w-full" >
238+ < div className = "mt-5 flex min-h-[400px] w-11/12 flex-1 flex-col place-self-center rounded border bg-background/95 p-4 text-gray-800 shadow dark:text-white max-[500px]:w-full" >
238239 < Logo
239240 className = {
240241 "size-[6vmin] h-fit min-w-10 place-self-center fill-foreground stroke-foreground stroke-[10px]"
241242 }
242243 />
243244 { /* <div className="mt-10 flex w-11/12 flex-col place-self-center rounded border p-4 text-gray-800 shadow dark:text-white"> */ }
244- < div className = "flex-1 overflow-auto" >
245- < ChatMessageList smooth className = "max-[500px]:px-0" >
246- { messages . flatMap ( ( message ) => {
247- const variant =
248- message . user_id === userAndClassData . userData . id
249- ? "sent"
250- : "received" ;
245+ < MathJaxContext >
246+ < div className = "flex-1 overflow-auto" >
247+ < ChatMessageList smooth className = "max-[500px]:px-0" >
248+ { messages . flatMap ( ( message ) => {
249+ const variant =
250+ message . user_id === userAndClassData . userData . id
251+ ? "sent"
252+ : "received" ;
251253
252- // Format the timestamp
253- const messageTime = new Date ( message . created_at ) ;
254- const formattedTime = messageTime . toLocaleTimeString ( "en-US" , {
255- hour : "numeric" ,
256- minute : "numeric" ,
257- } ) ;
258- const formattedDay = new Date (
259- message . created_at
260- ) . toLocaleDateString ( "en-US" , {
261- month : "short" ,
262- day : "numeric" ,
263- year : "numeric" ,
264- } ) ;
265- const elements = [ ] ;
266- if (
267- ! previousMessageTime ||
268- isDifferentDay ( messageTime , previousMessageTime )
269- ) {
254+ // Format the timestamp
255+ const messageTime = new Date ( message . created_at ) ;
256+ const formattedTime = messageTime . toLocaleTimeString ( "en-US" , {
257+ hour : "numeric" ,
258+ minute : "numeric" ,
259+ } ) ;
260+ const formattedDay = new Date (
261+ message . created_at
262+ ) . toLocaleDateString ( "en-US" , {
263+ month : "short" ,
264+ day : "numeric" ,
265+ year : "numeric" ,
266+ } ) ;
267+ const elements = [ ] ;
268+ if (
269+ ! previousMessageTime ||
270+ isDifferentDay ( messageTime , previousMessageTime )
271+ ) {
272+ elements . push (
273+ < div
274+ key = { messageTime . getMilliseconds ( ) }
275+ className = "flex items-center justify-center gap-4"
276+ >
277+ < Separator className = "w-[20%]" />
278+ < span className = "text-muted-foreground" >
279+ { formattedDay }
280+ </ span >
281+ < Separator className = "w-[20%]" />
282+ </ div >
283+ ) ;
284+ }
285+ previousMessageTime = messageTime ;
270286 elements . push (
271- < div
272- key = { messageTime . getMilliseconds ( ) }
273- className = "flex items-center justify-center gap-4"
287+ < ChatBubble
288+ key = { message . id }
289+ variant = { variant }
290+ className = "max-w-[80%]"
274291 >
275- < Separator className = "w-[20%]" />
276- < span className = "text-muted-foreground" > { formattedDay } </ span >
277- < Separator className = "w-[20%]" />
278- </ div >
279- ) ;
280- }
281- previousMessageTime = messageTime ;
282- elements . push (
283- < ChatBubble
284- key = { message . id }
285- variant = { variant }
286- className = "max-w-[80%]"
287- >
288- { ! message ?. member_id ? (
289- < AIAvatar />
290- ) : (
291- < ChatBubbleAvatar src = { message . avatar_url ! } fallback = "Me" />
292- ) }
293- < div className = "flex flex-col" >
294- < div className = "mb-1 flex items-center gap-2 text-xs font-medium text-muted-foreground" >
295- { message ?. member_id
296- ? ( message . full_name ?? "Unknown" )
297- : AIFullNameFormatted ( "AI Assistant" ) } { " " }
298- • { formattedTime }
299- { message . is_ask && (
300- < span className = "inline-flex items-center rounded-full bg-purple-100 px-2 py-0.5 text-xs font-medium text-purple-800 dark:bg-purple-900 dark:text-purple-200" >
301- Ask LLM
302- </ span >
303- ) }
292+ { ! message ?. member_id ? (
293+ < AIAvatar />
294+ ) : (
295+ < ChatBubbleAvatar src = { message . avatar_url ! } fallback = "Me" />
296+ ) }
297+ < div className = "flex flex-col" >
298+ < div className = "mb-1 flex items-center gap-2 text-xs font-medium text-muted-foreground" >
299+ { message ?. member_id
300+ ? ( message . full_name ?? "Unknown" )
301+ : AIFullNameFormatted ( "AI Assistant" ) } { " " }
302+ • { formattedTime }
303+ { message . is_ask && (
304+ < span className = "inline-flex items-center rounded-full bg-purple-100 px-2 py-0.5 text-xs font-medium text-purple-800 dark:bg-purple-900 dark:text-purple-200" >
305+ Ask LLM
306+ </ span >
307+ ) }
308+ </ div >
309+ < ChatBubbleMessage className = "p-2" variant = { variant } >
310+ < MathJax
311+ dynamic
312+ hideUntilTypeset = "every"
313+ className = "prose w-fit !whitespace-normal font-medium marker:text-inherit"
314+ >
315+ < ReactMarkdown >
316+ { cleanMessage ( message . content ) }
317+ </ ReactMarkdown >
318+ </ MathJax >
319+ </ ChatBubbleMessage >
304320 </ div >
305- < ChatBubbleMessage
306- className = "prose w-fit !whitespace-normal p-2 font-medium marker:text-inherit"
307- variant = { variant }
308- >
309- < ReactMarkdown >
310- { cleanMessage ( message . content ) }
311- </ ReactMarkdown >
312- </ ChatBubbleMessage >
313- </ div >
321+ </ ChatBubble >
322+ ) ;
323+ return elements ;
324+ } ) }
325+ { isLoading && (
326+ < ChatBubble variant = "received" >
327+ < AIAvatar />
328+ < ChatBubbleMessage isLoading variant = "received" />
314329 </ ChatBubble >
315- ) ;
316- return elements ;
317- } ) }
318- { isLoading && (
319- < ChatBubble variant = "received" >
320- < AIAvatar />
321- < ChatBubbleMessage isLoading variant = "received" />
322- </ ChatBubble >
323- ) }
324- </ ChatMessageList >
325- </ div >
330+ ) }
331+ </ ChatMessageList >
332+ </ div >
333+ </ MathJaxContext >
326334 < div className = "relative mt-4 flex items-center justify-between gap-2 rounded-lg border bg-background p-1" >
327335 < ChatInput
328336 value = { messageBoxValue }
0 commit comments