diff --git a/src/components/chat/index.js b/src/components/chat/index.js index a524738..0a2ac4d 100644 --- a/src/components/chat/index.js +++ b/src/components/chat/index.js @@ -38,6 +38,9 @@ const Chat = () => { } }; + const scrollTo = (element) => { + handleAutoScroll(firstNode.current, element, POSITIONS.TOP, config.align); + } useEffect(() => { if (!interaction.current) { if (config.scroll) { @@ -58,6 +61,7 @@ const Chat = () => { > setRef(node, index)} /> diff --git a/src/components/chat/messages/index.js b/src/components/chat/messages/index.js index 9e4a373..d2cc1a5 100644 --- a/src/components/chat/messages/index.js +++ b/src/components/chat/messages/index.js @@ -20,6 +20,7 @@ const defaultProps = { const Messages = ({ currentIndex, + scrollTo, setRef, }) => { @@ -33,8 +34,17 @@ const Messages = ({ switch (type) { case ID.USERS: + const indexOfMessageToBeReplied = (item.replyToMessageId) + ? storage.messages.findIndex((message) => message.id === item.replyToMessageId) : -1; + const messageToBeReplied = (indexOfMessageToBeReplied !== -1) + ? storage.messages[indexOfMessageToBeReplied] + : null; return ( - setRef(node, index)}> + setRef(node, index)}> diff --git a/src/components/chat/messages/index.scss b/src/components/chat/messages/index.scss index 7c43c23..6d7ede6 100644 --- a/src/components/chat/messages/index.scss +++ b/src/components/chat/messages/index.scss @@ -2,6 +2,27 @@ :root { --gray-light-color: var(--gray-light); + --gray-lightest-color: var(--gray-lightest); + --highlight-color: var(--blue); +} + +.reply-tag { + background-color: var(--gray-lightest-color); + display: flex; + width: 90%; + padding: $padding-small; + margin-bottom: $margin-extra-small; + border-radius: .35rem; + align-items: center; + line-height: 1; +} + +.user-message-wrapper { + transition: background-color .5s ease; +} + +.highlight { + background-color: var(--highlight-color); } .message { diff --git a/src/components/chat/messages/message.js b/src/components/chat/messages/message.js index 35195bf..6649bed 100644 --- a/src/components/chat/messages/message.js +++ b/src/components/chat/messages/message.js @@ -5,6 +5,7 @@ import Info from './info'; import Margin from './margin'; import player from 'utils/player'; import './index.scss'; +import Reply from './reply'; import ChatMessageReactions from './reactions'; const propTypes = { @@ -39,6 +40,8 @@ const Message = ({ emphasized, icon, initials, + messageToBeReplied, + scrollTo, edited, name, reactions, @@ -68,6 +71,14 @@ const Message = ({ name={name} timestamp={timestamp} /> + {messageToBeReplied && + + }
{children}
diff --git a/src/components/chat/messages/reply/index.js b/src/components/chat/messages/reply/index.js new file mode 100644 index 0000000..e181787 --- /dev/null +++ b/src/components/chat/messages/reply/index.js @@ -0,0 +1,54 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import cx from 'classnames'; +import '../index.scss'; + +const propTypes = { + active: PropTypes.bool, + idToReference: PropTypes.string, + text: PropTypes.string, +}; + +const defaultProps = { + active: false, + idToReference: '', + text: '', +}; + +const Reply = ({ + active, + idToReference, + scrollTo, + text, +}) => { + + const handleClickReply = () => { + const messageReplied = document.getElementById(idToReference); + scrollTo(messageReplied); + messageReplied.classList.add('highlight') + setTimeout(() => + messageReplied.classList.remove('highlight'), 800 + ); + } + + return ( + + {text} + + ); +}; + +Reply.propTypes = propTypes; +Reply.defaultProps = defaultProps; + +// Checks the message active state +const areEqual = (prevProps, nextProps) => { + if (prevProps.active !== nextProps.active) return false; + + return true; +}; + +export default React.memo(Reply, areEqual); diff --git a/src/components/chat/messages/user/index.js b/src/components/chat/messages/user/index.js index cc98693..2a5f2ee 100644 --- a/src/components/chat/messages/user/index.js +++ b/src/components/chat/messages/user/index.js @@ -35,6 +35,8 @@ const User = ({ text, edited, timestamp, + messageToBeReplied, + scrollTo, reactions, }) => { @@ -48,6 +50,8 @@ const User = ({ name={name} reactions={reactions} timestamp={timestamp} + messageToBeReplied={messageToBeReplied} + scrollTo={scrollTo} > { ); return { clear, + id: chat._id, emphasized, hyperlink: message !== chat._message, initials, @@ -470,6 +471,7 @@ const buildChat = result => { message, moderator, reactions, + replyToMessageId: chat._replyToMessageId, lastEditedTimestamp: chat._lastEditedTimestamp, timestamp: parseFloat(chat._in), };