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

88 lines
2.9 KiB
JavaScript
Raw Normal View History

import PropTypes from 'prop-types';
2020-08-25 09:33:51 -07:00
import React from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';
import { FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';
import Icon from 'soapbox/components/icon';
import emojify from 'soapbox/features/emoji/emoji';
2021-07-01 16:01:33 -07:00
import { makeGetChat } from 'soapbox/selectors';
import { shortNumberFormat } from 'soapbox/utils/numbers';
2022-01-10 14:01:24 -08:00
import Avatar from '../../../components/avatar';
import DisplayName from '../../../components/display_name';
2021-07-01 16:01:33 -07:00
const makeMapStateToProps = () => {
const getChat = makeGetChat();
const mapStateToProps = (state, { chatId }) => {
const chat = state.getIn(['chats', 'items', chatId]);
2021-07-01 16:01:33 -07:00
return {
chat: chat ? getChat(state, chat.toJS()) : undefined,
};
};
return mapStateToProps;
};
export default @connect(makeMapStateToProps)
class Chat extends ImmutablePureComponent {
2020-08-25 09:33:51 -07:00
static propTypes = {
2021-07-01 16:01:33 -07:00
chatId: PropTypes.string.isRequired,
chat: ImmutablePropTypes.map,
2020-08-25 09:33:51 -07:00
onClick: PropTypes.func,
};
handleClick = () => {
2020-08-25 15:24:47 -07:00
this.props.onClick(this.props.chat);
2020-08-25 09:33:51 -07:00
}
render() {
2020-08-25 15:24:47 -07:00
const { chat } = this.props;
if (!chat) return null;
const account = chat.get('account');
2020-08-27 13:43:19 -07:00
const unreadCount = chat.get('unread');
const content = chat.getIn(['last_message', 'content']);
const attachment = chat.getIn(['last_message', 'attachment']);
const image = attachment && attachment.getIn(['pleroma', 'mime_type'], '').startsWith('image/');
const parsedContent = content ? emojify(content) : '';
2020-08-25 09:33:51 -07:00
return (
<div className='account'>
<button className='floating-link' onClick={this.handleClick} />
<div className='account__wrapper'>
<div key={account.get('id')} className='account__display-name'>
<div className='account__avatar-wrapper'>
<Avatar account={account} size={36} />
</div>
<DisplayName account={account} />
{attachment && (
<Icon
className='chat__attachment-icon'
src={image ? require('@tabler/icons/icons/photo.svg') : require('@tabler/icons/icons/paperclip.svg')}
/>
)}
{content ? (
<span
className='chat__last-message'
dangerouslySetInnerHTML={{ __html: parsedContent }}
/>
) : attachment && (
<span
className='chat__last-message attachment'
>
{image ? <FormattedMessage id='chats.attachment_image' defaultMessage='Image' /> : <FormattedMessage id='chats.attachment' defaultMessage='Attachment' />}
</span>
)}
2020-08-27 13:43:19 -07:00
{unreadCount > 0 && <i className='icon-with-badge__badge'>{shortNumberFormat(unreadCount)}</i>}
2020-08-25 09:33:51 -07:00
</div>
</div>
</div>
);
}
}