import React, { useEffect, useState } from 'react'; import { useSelector } from 'react-redux'; import { List, ListItem, ListItemAvatar, Avatar, ListItemText, Fade, Popover, CircularProgress } from '@material-ui/core'; import moment from 'moment'; import DomPurify from 'dompurify'; //import Code from 'react-code-prettify'; import UserInfo from '../UserInfo/UserInfo'; import { StoreState } from '../../reducers'; interface MessageList { from: string; to?: string; msg: string; date: Date; } declare var PR: any; export default function Messages() { // Get States from Redux Store const chatStore = useSelector((state: StoreState) => state.chat); const { activeServer, activeChannel, activeView, activePMUser } = chatStore; // Local states const [userInfoVisible, setUserInfoVisible] = useState(false); const [messageIndex, setMessageIndex] = useState(12); const [loadMessages, setLoadMessages] = useState(false); const [userName, setUserName] = useState(''); const [anchorEl, setAnchorEl] = useState(null); // ref to message container (for keeping scroll to bottom of chat) let messageContainerBottomRef = document.getElementById('messagesContainerBottom'); let messageContainerRef = document.getElementById('messagesContainer'); // Get message list from channel or from specific user let messages: MessageList[] = []; let messagesLength = 0; if (activeView === 'servers') { messages = chatStore.servers[activeServer]['channels'][activeChannel]; messagesLength = messages.length; } else { messages = chatStore.privateMessages[activePMUser]; // If no messages need to make empty array if (messages === undefined) { messages = []; } messagesLength = messages.length; } // Scroll to bottom of container if were not loading new messages useEffect(() => { if (messageContainerBottomRef && messageContainerRef) { if (loadMessages) { messageContainerRef.scroll(0, 60); } else { messageContainerBottomRef.scrollIntoView({ block: 'end', behavior: 'smooth' }); } } }, [loadMessages, messages, messageContainerRef, messageContainerBottomRef]); // Checks is message is a code block const isTextCodeBlock = (message: string) => { if (message.startsWith('```') && message.endsWith('```')) return true; else return false; }; // Handles to load more messages when scroll at top const handleScrollTop = (e: any) => { const element = e.target; if (element.scrollTop > 60) { setLoadMessages(false); } if (element.scrollTop === 0) { if (messagesLength > messageIndex) { setTimeout(() => { setLoadMessages(true); if (messageIndex + 12 > messagesLength) { setMessageIndex(messagesLength); } else { setMessageIndex(messageIndex + 12); } }, 400); } } }; // Formats the code block const formatCode = (message: string) => { return message.split('```')[1]; }; // Handles clicks for setting anchor to User Info (To private message) const handleUserClick = (e: any, userName: string) => { setUserName(userName); setUserInfoVisible(true); setAnchorEl(e.currentTarget); }; // Closes popup of User Info const handlePopoverClose = () => { setUserInfoVisible(false); setAnchorEl(null); }; // Load pretty print on every render change useEffect(() => { PR.prettyPrint(); }); return ( {messages.length === 0 && (
{' '}
{' '} The server just woke up! Hold on for 5 seconds, while he munches a quick byte!{' '}
)}
handleScrollTop(e)} ref={element => (messageContainerRef = element)} > {messagesLength >= messageIndex ? (
) : null} {messages !== null ? messages.slice(messagesLength - messageIndex, messagesLength).map((message, i) => { // Filter for null messages (dummy message on backend should fix...) return ( handleUserClick(e, message.from)} src={process.env.PUBLIC_URL + '/user.png'} alt="user icon" height="48" /> {isTextCodeBlock(message.msg) ? ( handleUserClick(e, message.from)}> {message.from.toLowerCase()}
{` - ${moment(message.date).format('LLL')}`}
} secondary={
                              
} className="message-text" /> ) : ( handleUserClick(e, message.from)}> {message.from.toLowerCase()}
{` - ${moment(message.date).format('LLL')}`}
} secondary={message.msg} className="message-text" /> )} ); }) : null}
(messageContainerBottomRef = element)} id="messagesContainerBottom">
); }