diff --git a/client/src/Components/local/organisms/chat-messages-list/index.tsx b/client/src/Components/local/organisms/chat-messages-list/index.tsx index 42137a81fb..5ca43cbeb3 100644 --- a/client/src/Components/local/organisms/chat-messages-list/index.tsx +++ b/client/src/Components/local/organisms/chat-messages-list/index.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react' +import React, { useMemo, useState } from 'react' import { Box, FormControlLabel, FormGroup, Switch } from '@material-ui/core' /* Import Types */ import PropTypes from './types/props' @@ -15,19 +15,47 @@ export const ChatMessagesList: React.FC = ({ messages, icons, colors, names, onMarkAllAsRead, isUmpire, playerRole, playerForce, chatContainerHeight, turnPresentation, observing, markUnread, hideForcesInChannel, hideAuthor }: PropTypes) => { - const [revMessages, setRevMessages] = useState>([]) const [trimMessages, setTrimMessages] = useState(true) const trimmedLen = 50 - useEffect(() => { + // only update the rendered messages if the messages change + const messageList = useMemo(() => { // cast messages, for type-checking const chatMessages = messages as Array // trim messages, if necessary const rMessages = trimMessages ? chatMessages.slice(-trimmedLen) : chatMessages // note we have to clone it first, since reverse is destructive const cMessages = [...rMessages].reverse() - setRevMessages(cMessages) + return cMessages.map((message, key) => { + if (message.messageType === INFO_MESSAGE_CLIPPED) { + return ( + +

Turn {formatTurn(message.gameTurn, turnPresentation)}

+
+ ) + } else { + const chatMsg = message as ChatMessageType + return ( + + + + + + ) + } + }) }, [messages, trimMessages]) const height = chatContainerHeight || 280 @@ -43,37 +71,7 @@ export const ChatMessagesList: React.FC = ({ setTrimMessages(!trimMessages)} checked={trimMessages} />} label={'Trim to last ' + trimmedLen + ' messaages'} /> - { - revMessages.map((message, key) => { - if (message.messageType === INFO_MESSAGE_CLIPPED) { - return ( - -

Turn {formatTurn(message.gameTurn, turnPresentation)}

-
- ) - } else { - const chatMsg = message as ChatMessageType - return ( - - - - - - ) - } - }) - } + { messageList }
)