jiti-meet/react/features/chat/components/web/Chat.js

205 lines
5.6 KiB
JavaScript
Raw Normal View History

// @flow
import React from 'react';
import Transition from 'react-transition-group/Transition';
import { translate } from '../../../base/i18n';
2019-10-22 07:56:42 +00:00
import { Icon, IconClose } from '../../../base/icons';
2019-03-21 16:38:29 +00:00
import { connect } from '../../../base/redux';
import AbstractChat, {
_mapDispatchToProps,
_mapStateToProps,
type Props
} from '../AbstractChat';
2020-05-20 10:57:03 +00:00
import ChatInput from './ChatInput';
import DisplayNameForm from './DisplayNameForm';
import MessageContainer from './MessageContainer';
2019-10-07 12:35:04 +00:00
import MessageRecipient from './MessageRecipient';
/**
* React Component for holding the chat feature in a side panel that slides in
* and out of view.
*/
class Chat extends AbstractChat<Props> {
/**
* Whether or not the {@code Chat} component is off-screen, having finished
* its hiding animation.
*/
_isExited: boolean;
/**
* Reference to the React Component for displaying chat messages. Used for
* scrolling to the end of the chat messages.
*/
_messageContainerRef: Object;
/**
* Initializes a new {@code Chat} instance.
*
* @param {Object} props - The read-only properties with which the new
* instance is to be initialized.
*/
constructor(props: Props) {
super(props);
this._isExited = true;
this._messageContainerRef = React.createRef();
// Bind event handlers so they are only bound once for every instance.
this._renderPanelContent = this._renderPanelContent.bind(this);
// Bind event handlers so they are only bound once for every instance.
this._onChatInputResize = this._onChatInputResize.bind(this);
}
/**
* Implements {@code Component#componentDidMount}.
*
* @inheritdoc
*/
componentDidMount() {
this._scrollMessageContainerToBottom(true);
}
/**
* Implements {@code Component#componentDidUpdate}.
*
* @inheritdoc
*/
componentDidUpdate(prevProps) {
if (this.props._messages !== prevProps._messages) {
this._scrollMessageContainerToBottom(true);
} else if (this.props._isOpen && !prevProps._isOpen) {
this._scrollMessageContainerToBottom(false);
}
}
/**
* Implements React's {@link Component#render()}.
*
* @inheritdoc
* @returns {ReactElement}
*/
render() {
return (
<Transition
in = { this.props._isOpen }
timeout = { 500 }>
{ this._renderPanelContent }
</Transition>
);
}
_onChatInputResize: () => void;
/**
* Callback invoked when {@code ChatInput} changes height. Preserves
* displaying the latest message if it is scrolled to.
*
* @private
* @returns {void}
*/
_onChatInputResize() {
this._messageContainerRef.current.maybeUpdateBottomScroll();
}
/**
* Returns a React Element for showing chat messages and a form to send new
* chat messages.
*
* @private
* @returns {ReactElement}
*/
_renderChat() {
return (
2019-05-03 21:26:28 +00:00
<>
<MessageContainer
messages = { this.props._messages }
ref = { this._messageContainerRef } />
2019-10-07 12:35:04 +00:00
<MessageRecipient />
<ChatInput
onResize = { this._onChatInputResize }
onSend = { this.props._onSendMessage } />
2019-05-03 21:26:28 +00:00
</>
);
}
/**
* Instantiates a React Element to display at the top of {@code Chat} to
* close {@code Chat}.
*
* @private
* @returns {ReactElement}
*/
_renderChatHeader() {
return (
<div className = 'chat-header'>
<div
className = 'chat-close'
2019-10-22 07:56:42 +00:00
onClick = { this.props._onToggleChat }>
<Icon src = { IconClose } />
</div>
</div>
);
}
_renderPanelContent: (string) => React$Node | null;
/**
* Renders the contents of the chat panel, depending on the current
* animation state provided by {@code Transition}.
*
* @param {string} state - The current display transition state of the
* {@code Chat} component, as provided by {@code Transition}.
* @private
* @returns {ReactElement | null}
*/
_renderPanelContent(state) {
this._isExited = state === 'exited';
const { _isOpen, _showNamePrompt } = this.props;
const ComponentToRender = !_isOpen && state === 'exited'
? null
: (
2019-05-03 21:26:28 +00:00
<>
{ this._renderChatHeader() }
{ _showNamePrompt
? <DisplayNameForm /> : this._renderChat() }
2019-05-03 21:26:28 +00:00
</>
);
let className = '';
if (_isOpen) {
className = 'slideInExt';
} else if (this._isExited) {
className = 'invisible';
}
return (
<div
2019-05-03 21:26:28 +00:00
className = { `sideToolbarContainer ${className}` }
id = 'sideToolbarContainer'>
{ ComponentToRender }
</div>
);
}
/**
* Scrolls the chat messages so the latest message is visible.
*
* @param {boolean} withAnimation - Whether or not to show a scrolling
* animation.
* @private
* @returns {void}
*/
_scrollMessageContainerToBottom(withAnimation) {
if (this._messageContainerRef.current) {
this._messageContainerRef.current.scrollToBottom(withAnimation);
}
}
}
export default translate(connect(_mapStateToProps, _mapDispatchToProps)(Chat));