From b39407960fa908dc55b8ea48625f3c7095c37b56 Mon Sep 17 00:00:00 2001 From: vishtree Date: Fri, 24 Apr 2020 11:34:42 +0200 Subject: [PATCH] Adding direct messaging support for channel preview --- src/components/ChannelPreview.js | 32 +++++++++++++++++++-- src/components/ChannelPreviewCompact.js | 13 +++++++-- src/components/ChannelPreviewCountOnly.js | 8 ++++-- src/components/ChannelPreviewLastMessage.js | 14 ++++++--- src/components/ChannelPreviewMessenger.js | 10 +++++-- types/index.d.ts | 8 +++++- 6 files changed, 70 insertions(+), 15 deletions(-) diff --git a/src/components/ChannelPreview.js b/src/components/ChannelPreview.js index 5d8d0ea1a..a76e8acce 100644 --- a/src/components/ChannelPreview.js +++ b/src/components/ChannelPreview.js @@ -2,7 +2,7 @@ import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import { ChannelPreviewCountOnly } from './ChannelPreviewCountOnly'; -import { withTranslationContext } from '../context'; +import { withTranslationContext, withChatContext } from '../context'; class ChannelPreview extends PureComponent { constructor(props) { @@ -111,6 +111,32 @@ class ChannelPreview extends PureComponent { } }; + getDisplayTitle = () => { + const { channel, client } = this.props; + let title = channel.data.name; + const members = Object.values(channel.state.members); + + if (!title && members.length === 2) { + const otherMember = members.find((m) => m.user.id !== client.user.id); + title = otherMember.user.name; + } + + return title; + }; + + getDisplayImage = () => { + const { channel, client } = this.props; + let image = channel.data.image; + const members = Object.values(channel.state.members); + + if (!image && members.length === 2) { + const otherMember = members.find((m) => m.user.id !== client.user.id); + image = otherMember.user.image; + } + + return image; + }; + render() { const props = { ...this.state, ...this.props }; @@ -119,6 +145,8 @@ class ChannelPreview extends PureComponent { = 1 ? 'str-chat__channel-preview-compact--unread' @@ -43,7 +48,7 @@ export class ChannelPreviewCompact extends React.PureComponent { const activeClass = this.props.active ? 'str-chat__channel-preview-compact--active' : ''; - const name = this.props.channel.data.name || this.props.channel.cid; + return ( ); } diff --git a/src/components/ChannelPreviewCountOnly.js b/src/components/ChannelPreviewCountOnly.js index e6dfeb0ec..70ecd5dbe 100644 --- a/src/components/ChannelPreviewCountOnly.js +++ b/src/components/ChannelPreviewCountOnly.js @@ -16,11 +16,15 @@ export class ChannelPreviewCountOnly extends PureComponent { watchers: PropTypes.object, /** Number of unread messages */ unread: PropTypes.number, + /** Title of channel to display */ + displayTitle: PropTypes.string, + /** Image of channel to display */ + displayImage: PropTypes.string, }; render() { + const { displayTitle } = this.props; const unreadClass = this.props.unread >= 1 ? 'unread' : ''; - const name = this.props.channel.data.name || this.props.channel.cid; return (
@@ -28,7 +32,7 @@ export class ChannelPreviewCountOnly extends PureComponent { onClick={this.props.setActiveChannel.bind(this, this.props.channel)} > {' '} - {name} {this.props.unread} + {displayTitle} {this.props.unread}
); diff --git a/src/components/ChannelPreviewLastMessage.js b/src/components/ChannelPreviewLastMessage.js index bf9ea1e53..601d9515b 100644 --- a/src/components/ChannelPreviewLastMessage.js +++ b/src/components/ChannelPreviewLastMessage.js @@ -32,6 +32,10 @@ class ChannelPreviewLastMessage extends PureComponent { latestMessage: PropTypes.string, /** Length of latest message to truncate at */ latestMessageLength: PropTypes.number, + /** Title of channel to display */ + displayTitle: PropTypes.string, + /** Image of channel to display */ + displayImage: PropTypes.string, }; static defaultProps = { @@ -46,14 +50,14 @@ class ChannelPreviewLastMessage extends PureComponent { }; render() { - const { t } = this.props; + const { t, displayTitle, displayImage } = this.props; const unreadClass = this.props.unread >= 1 ? 'str-chat__channel-preview--unread' : ''; const activeClass = this.props.active ? 'str-chat__channel-preview--active' : ''; - const name = this.props.channel.data.name || this.props.channel.cid; + return (
= 1 && (
)} - +
- {name} + + {displayTitle} + {!this.props.channel.state.messages[0] ? t('Nothing yet...') diff --git a/src/components/ChannelPreviewMessenger.js b/src/components/ChannelPreviewMessenger.js index fb2816411..a3b600aeb 100644 --- a/src/components/ChannelPreviewMessenger.js +++ b/src/components/ChannelPreviewMessenger.js @@ -34,6 +34,10 @@ class ChannelPreviewMessenger extends PureComponent { latestMessage: PropTypes.string, /** Length of latest message to truncate at */ latestMessageLength: PropTypes.number, + /** Title of channel to display */ + displayTitle: PropTypes.string, + /** Image of channel to display */ + displayImage: PropTypes.string, }; static defaultProps = { @@ -56,7 +60,7 @@ class ChannelPreviewMessenger extends PureComponent { ? 'str-chat__channel-preview-messenger--active' : ''; - const { channel, t } = this.props; + const { channel, displayTitle, displayImage, t } = this.props; return (