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

86 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-08-25 12:58:35 -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';
import ImmutablePureComponent from 'react-immutable-pure-component';
2020-08-25 15:54:10 -07:00
import { getSettings } from 'soapbox/actions/settings';
2020-08-25 12:58:35 -07:00
import ChatList from './chat_list';
import { FormattedMessage } from 'react-intl';
2020-08-25 14:00:27 -07:00
import { makeGetChat } from 'soapbox/selectors';
2020-08-25 18:33:49 -07:00
import { openChat, toggleMainWindow } from 'soapbox/actions/chats';
import ChatWindow from './chat_window';
2020-08-27 13:43:19 -07:00
import { shortNumberFormat } from 'soapbox/utils/numbers';
2020-08-25 12:58:35 -07:00
2020-08-25 14:00:27 -07:00
const addChatsToPanes = (state, panesData) => {
const getChat = makeGetChat();
const newPanes = panesData.get('panes').map(pane => {
const chat = getChat(state, { id: pane.get('chat_id') });
return pane.set('chat', chat);
});
return panesData.set('panes', newPanes);
};
const mapStateToProps = state => {
2020-08-25 15:24:47 -07:00
const panesData = getSettings(state).get('chats');
2020-08-25 14:00:27 -07:00
return {
panesData: addChatsToPanes(state, panesData),
2020-08-27 13:43:19 -07:00
unreadCount: state.get('chats').reduce((acc, curr) => acc + curr.get('unread'), 0),
2020-08-25 14:00:27 -07:00
};
};
2020-08-25 12:58:35 -07:00
export default @connect(mapStateToProps)
@injectIntl
class ChatPanes extends ImmutablePureComponent {
static propTypes = {
dispatch: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
panesData: ImmutablePropTypes.map,
}
2020-08-25 15:24:47 -07:00
handleClickChat = (chat) => {
2020-08-25 15:54:10 -07:00
this.props.dispatch(openChat(chat.get('id')));
2020-08-25 15:24:47 -07:00
}
2020-08-25 16:53:36 -07:00
handleMainWindowToggle = () => {
this.props.dispatch(toggleMainWindow());
}
2020-08-25 12:58:35 -07:00
render() {
2020-08-27 13:43:19 -07:00
const { panesData, unreadCount } = this.props;
2020-08-25 16:53:36 -07:00
const panes = panesData.get('panes');
const mainWindow = panesData.get('mainWindow');
2020-08-25 12:58:35 -07:00
2020-08-27 12:02:52 -07:00
const mainWindowPane = (
<div className={`pane pane--main pane--${mainWindow}`}>
<div className='pane__header'>
{unreadCount > 0 && <i className='icon-with-badge__badge'>{shortNumberFormat(unreadCount)}</i>}
2020-08-27 12:02:52 -07:00
<button className='pane__title' onClick={this.handleMainWindowToggle}>
<FormattedMessage id='chat_panels.main_window.title' defaultMessage='Chats' />
</button>
</div>
<div className='pane__content'>
<ChatList
onClickChat={this.handleClickChat}
emptyMessage={<FormattedMessage id='chat_panels.main_window.empty' defaultMessage="No chats found. To start a chat, visit a user's profile." />}
/>
</div>
</div>
);
2020-08-25 12:58:35 -07:00
return (
<div className='chat-panes'>
2020-08-27 12:02:52 -07:00
{mainWindowPane}
2020-08-25 18:33:49 -07:00
{panes.map((pane, i) =>
<ChatWindow idx={i} pane={pane} key={pane.get('chat_id')} />
)}
2020-08-25 12:58:35 -07:00
</div>
);
}
}