Rev 3600 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useEffect, useRef } from 'react';
import { Box } from '@mui/material';
import { Inbox } from '@mui/icons-material';
import { useNearScreen } from '@hooks';
import { Spinner } from '@shared/components';
import EmptySection from '@components/UI/EmptySection';
import MessageItem from './MessageItem';
const MessagesList = ({ messages = [], onLoadMore, loading = false }) => {
const [isIntercepting, ref] = useNearScreen({
once: false,
rootMargin: '100px'
});
const messagesEndRef = useRef(null);
useEffect(() => {
if (isIntercepting && onLoadMore) {
onLoadMore();
}
}, [isIntercepting, onLoadMore]);
useEffect(() => {
// Scroll to bottom when new messages are added
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [messages]);
const handleReport = ({ url, id }) => {
// Aquí puedes implementar la lógica de reporte
console.log('Reportar mensaje:', { url, id });
};
if (!messages.length && !loading) {
return (
<EmptySection
Icon={<Inbox />}
message='No hay mensajes en esta conversación'
align='center'
/>
);
}
return (
<Box
sx={{
display: 'flex',
flexDirection: 'column',
height: '100%',
overflow: 'auto',
bgcolor: 'background.paper'
}}
>
<Box
sx={{
flex: 1,
overflow: 'auto',
display: 'flex',
flexDirection: 'column-reverse',
p: 1
}}
>
{messages.map((message) => (
<MessageItem key={message.uuid} message={message} onReport={handleReport} />
))}
{/* Loading indicator for infinite scroll */}
<Box ref={ref} sx={{ display: 'flex', justifyContent: 'center', py: 2 }}>
{loading && <Spinner size='small' />}
</Box>
{/* Invisible element to scroll to bottom */}
<div ref={messagesEndRef} />
</Box>
</Box>
);
};
export default MessagesList;