bigbuffet-rw/app/soapbox/features/chats/components/chat_window.js

134 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-08-25 18:33:49 -07:00
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { injectIntl } from 'react-intl';
2020-08-30 17:50:39 -07:00
import { Link } from 'react-router-dom';
2020-08-25 18:33:49 -07:00
import ImmutablePureComponent from 'react-immutable-pure-component';
import Avatar from 'soapbox/components/avatar';
import { getAcct } from 'soapbox/utils/accounts';
2020-08-25 18:33:49 -07:00
import IconButton from 'soapbox/components/icon_button';
2020-08-27 14:09:03 -07:00
import {
closeChat,
toggleChat,
} from 'soapbox/actions/chats';
import ChatBox from './chat_box';
2020-08-27 13:43:19 -07:00
import { shortNumberFormat } from 'soapbox/utils/numbers';
import { displayFqn } from 'soapbox/utils/state';
import HoverRefWrapper from 'soapbox/components/hover_ref_wrapper';
2021-07-01 16:01:33 -07:00
import { makeGetChat } from 'soapbox/selectors';
2020-08-25 18:33:49 -07:00
2021-07-01 16:01:33 -07:00
const makeMapStateToProps = () => {
const getChat = makeGetChat();
2020-08-25 18:33:49 -07:00
2021-07-01 16:01:33 -07:00
const mapStateToProps = (state, { chatId }) => {
const chat = state.getIn(['chats', chatId]);
return {
me: state.get('me'),
chat: chat ? getChat(state, chat.toJS()) : undefined,
displayFqn: displayFqn(state),
};
};
return mapStateToProps;
};
export default @connect(makeMapStateToProps)
2020-08-25 18:33:49 -07:00
@injectIntl
class ChatWindow extends ImmutablePureComponent {
static propTypes = {
dispatch: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
2021-07-01 16:01:33 -07:00
chatId: PropTypes.string.isRequired,
windowState: PropTypes.string.isRequired,
2020-08-25 18:33:49 -07:00
idx: PropTypes.number,
2020-08-27 13:43:19 -07:00
chat: ImmutablePropTypes.map,
2020-08-25 22:21:54 -07:00
me: PropTypes.node,
displayFqn: PropTypes.bool,
2020-08-25 18:33:49 -07:00
}
2020-08-25 19:31:34 -07:00
state = {
content: '',
}
2020-08-25 18:33:49 -07:00
handleChatClose = (chatId) => {
return (e) => {
this.props.dispatch(closeChat(chatId));
};
}
handleChatToggle = (chatId) => {
return (e) => {
this.props.dispatch(toggleChat(chatId));
};
}
2020-08-25 19:31:34 -07:00
handleContentChange = (e) => {
this.setState({ content: e.target.value });
}
handleInputRef = (el) => {
this.inputElem = el;
this.focusInput();
};
2020-08-27 14:09:03 -07:00
focusInput = () => {
if (!this.inputElem) return;
this.inputElem.focus();
}
componentDidUpdate(prevProps) {
2021-07-01 16:01:33 -07:00
const oldState = prevProps.windowState;
const newState = this.props.windowState;
if (oldState !== newState && newState === 'open')
this.focusInput();
}
2020-08-25 18:33:49 -07:00
render() {
2021-07-01 16:01:33 -07:00
const { windowState, idx, chat, displayFqn } = this.props;
if (!chat) return null;
const account = chat.get('account');
2020-08-25 18:33:49 -07:00
const right = (285 * (idx + 1)) + 20;
2020-08-27 13:43:19 -07:00
const unreadCount = chat.get('unread');
2020-08-25 18:33:49 -07:00
const unreadIcon = (
<i className='icon-with-badge__badge'>
{shortNumberFormat(unreadCount)}
</i>
);
const avatar = (
<HoverRefWrapper accountId={account.get('id')}>
<Link to={`/@${account.get('acct')}`}>
<Avatar account={account} size={18} />
</Link>
</HoverRefWrapper>
);
2020-08-25 18:33:49 -07:00
return (
2021-07-01 16:01:33 -07:00
<div className={`pane pane--${windowState}`} style={{ right: `${right}px` }}>
2020-08-25 18:33:49 -07:00
<div className='pane__header'>
{unreadCount > 0 ? unreadIcon : avatar }
2020-08-25 18:33:49 -07:00
<button className='pane__title' onClick={this.handleChatToggle(chat.get('id'))}>
@{getAcct(account, displayFqn)}
2020-08-25 18:33:49 -07:00
</button>
<div className='pane__close'>
<IconButton icon='close' title='Close chat' onClick={this.handleChatClose(chat.get('id'))} />
</div>
</div>
<div className='pane__content'>
<ChatBox
chatId={chat.get('id')}
onSetInputRef={this.handleInputRef}
/>
2020-08-25 18:33:49 -07:00
</div>
</div>
);
}
}