2023-02-05 19:41:24 -05:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
|
|
|
|
import Message from './Message';
|
2023-02-07 16:22:35 -05:00
|
|
|
import Landing from './Landing';
|
2023-02-05 19:41:24 -05:00
|
|
|
|
|
|
|
|
export default function Messages({ messages }) {
|
2023-02-07 16:22:35 -05:00
|
|
|
if (messages.length === 0) {
|
2023-02-08 09:15:47 -05:00
|
|
|
return <Landing />;
|
|
|
|
|
}
|
2023-02-07 16:22:35 -05:00
|
|
|
|
2023-02-05 19:41:24 -05:00
|
|
|
const messagesEndRef = useRef(null);
|
|
|
|
|
|
|
|
|
|
const scrollToBottom = () => {
|
|
|
|
|
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-07 19:07:12 -05:00
|
|
|
// this useEffect triggers the following warning:
|
|
|
|
|
// Warning: Internal React error: Expected static flag was missing.
|
2023-02-05 19:41:24 -05:00
|
|
|
useEffect(() => {
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
}, [messages]);
|
|
|
|
|
|
2023-02-06 13:27:28 -05:00
|
|
|
// <div className="flex-1 overflow-hidden">
|
2023-02-05 19:41:24 -05:00
|
|
|
// <div className="w-full border-b border-black/10 dark:border-gray-900/50 text-gray-800 dark:text-gray-100 group dark:bg-gray-800">
|
2023-02-06 13:27:28 -05:00
|
|
|
// </div>
|
|
|
|
|
// <div className="flex h-full text-sm dark:bg-gray-800"></div>;
|
2023-02-05 19:41:24 -05:00
|
|
|
return (
|
2023-02-08 09:15:47 -05:00
|
|
|
// <div className="flex-1 overflow-y-auto ">
|
|
|
|
|
<div className="flex-1 overflow-hidden">
|
|
|
|
|
<div className="h-full dark:bg-gray-800">
|
|
|
|
|
<div className="flex h-full flex-col items-center text-sm dark:bg-gray-800">
|
|
|
|
|
{messages.map((message, i) => (
|
|
|
|
|
<Message
|
|
|
|
|
key={i}
|
|
|
|
|
sender={message.sender}
|
|
|
|
|
text={message.text}
|
|
|
|
|
last={i === messages.length - 1}
|
|
|
|
|
error={!!message.error ? true : false}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
<div ref={messagesEndRef} />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2023-02-05 19:41:24 -05:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|