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

66 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-08-24 19:26:42 -07:00
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
2021-07-01 16:01:33 -07:00
import ImmutablePropTypes from 'react-immutable-proptypes';
2020-08-25 14:00:27 -07:00
import { injectIntl } from 'react-intl';
2020-08-24 19:26:42 -07:00
import ImmutablePureComponent from 'react-immutable-pure-component';
import Chat from './chat';
2020-08-24 19:26:42 -07:00
2020-08-27 13:07:15 -07:00
const chatDateComparator = (chatA, chatB) => {
// Sort most recently updated chats at the top
const a = new Date(chatA.get('updated_at'));
const b = new Date(chatB.get('updated_at'));
if (a === b) return 0;
if (a > b) return -1;
if (a < b) return 1;
return 0;
};
2020-08-25 10:38:21 -07:00
const mapStateToProps = state => {
2021-07-01 16:01:33 -07:00
const chatIds = state.get('chats')
2020-08-27 13:43:19 -07:00
.toList()
2021-07-01 16:01:33 -07:00
.sort(chatDateComparator)
.map(chat => chat.get('id'));
2020-08-27 13:43:19 -07:00
2020-08-25 10:38:21 -07:00
return {
2021-07-01 16:01:33 -07:00
chatIds,
2020-08-25 10:38:21 -07:00
};
};
2020-08-24 19:26:42 -07:00
export default @connect(mapStateToProps)
@injectIntl
class ChatList extends ImmutablePureComponent {
static propTypes = {
dispatch: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
2021-07-01 16:01:33 -07:00
chatIds: ImmutablePropTypes.list,
2020-08-25 15:24:47 -07:00
onClickChat: PropTypes.func,
2020-08-27 12:02:52 -07:00
emptyMessage: PropTypes.node,
2020-08-24 19:26:42 -07:00
};
render() {
2021-07-01 16:01:33 -07:00
const { chatIds, emptyMessage } = this.props;
2020-08-24 19:26:42 -07:00
return (
<div className='chat-list'>
<div className='chat-list__content'>
2021-07-01 16:01:33 -07:00
{chatIds.count() === 0 &&
2020-08-27 12:02:52 -07:00
<div className='empty-column-indicator'>{emptyMessage}</div>
}
2021-07-01 16:01:33 -07:00
{chatIds.map(chatId => (
<div key={chatId} className='chat-list-item'>
<Chat
2021-07-01 16:01:33 -07:00
chatId={chatId}
2020-08-25 15:24:47 -07:00
onClick={this.props.onClickChat}
2020-08-25 09:33:51 -07:00
/>
2020-08-24 19:26:42 -07:00
</div>
))}
</div>
</div>
);
}
}