Skip to content

Commit a257bd8

Browse files
Merge pull request #125 from GetStream/vishal/CRNS-58
CRNS-58, 59: Adding support for custom message length in Channel preview
2 parents 18a06ef + c3cfdf6 commit a257bd8

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

src/components/ChannelPreview.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class ChannelPreview extends PureComponent {
6060
const latestMessage = {
6161
text: '',
6262
created_at: '',
63+
messageObject: { ...message },
6364
};
6465

6566
if (!message) {
@@ -72,7 +73,7 @@ export class ChannelPreview extends PureComponent {
7273
}
7374

7475
if (message.text) {
75-
latestMessage.text = message.text.slice(0, 20);
76+
latestMessage.text = message.text;
7677
} else {
7778
if (message.command) {
7879
latestMessage.text = '/' + message.command;

src/components/ChannelPreviewMessenger.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ export const ChannelPreviewMessenger = themed(
7575
latestMessage: PropTypes.object,
7676
/** Number of unread messages on channel */
7777
unread: PropTypes.number,
78+
/** Length at which latest message should be truncated */
79+
latestMessageLength: PropTypes.number,
80+
/**
81+
* Formatter function for date of latest message.
82+
* @param date Message date
83+
* @returns Formatted date string
84+
*
85+
* By default today's date is shown in 'HH:mm A' format and other dates
86+
* are displayed in 'DD/MM/YY' format. props.latestMessage.created_at is the
87+
* default formated date. This default logic is part of ChannelPreview component.
88+
*/
89+
formatLatestMessageDate: PropTypes.func,
90+
};
91+
92+
static defaultProps = {
93+
latestMessageLength: 30,
7894
};
7995

8096
onSelectChannel = () => {
@@ -109,7 +125,7 @@ export const ChannelPreviewMessenger = themed(
109125
.map((member) => member.user.name || member.user.id || 'Unnamed User')
110126
.join(', ');
111127
}
112-
128+
const formatLatestMessageDate = this.props.formatLatestMessageDate;
113129
return (
114130
<Container onPress={this.onSelectChannel}>
115131
{this.renderAvatar(otherMembers)}
@@ -118,17 +134,22 @@ export const ChannelPreviewMessenger = themed(
118134
<Title ellipsizeMode="tail" numberOfLines={1}>
119135
{name}
120136
</Title>
121-
<Date>{this.props.latestMessage.created_at}</Date>
137+
<Date>
138+
{formatLatestMessageDate
139+
? formatLatestMessageDate(
140+
this.props.latestMessage.messageObject.created_at,
141+
)
142+
: this.props.latestMessage.created_at}
143+
</Date>
122144
</DetailsTop>
123145
<Message
124146
unread={this.props.unread > 0 ? this.props.unread : undefined}
125147
>
126148
{!this.props.latestMessage
127149
? 'Nothing yet...'
128-
: truncate(
129-
this.props.latestMessage.text.replace(/\n/g, ' '),
130-
14,
131-
)}
150+
: truncate(this.props.latestMessage.text.replace(/\n/g, ' '), {
151+
length: this.props.latestMessageLength,
152+
})}
132153
</Message>
133154
</Details>
134155
</Container>

src/components/MessageSimple/MessageContent.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ export const MessageContent = themed(
249249
PropTypes.node,
250250
PropTypes.elementType,
251251
]),
252+
formatDate: PropTypes.func,
252253
};
253254

254255
static defaultProps = {
@@ -534,7 +535,9 @@ export const MessageContent = themed(
534535
{!MessageFooter && showTime ? (
535536
<MetaContainer>
536537
<MetaText alignment={pos}>
537-
{moment(message.created_at).format('h:mmA')}
538+
{this.props.formatDate
539+
? this.props.formatDate(message.created_at)
540+
: moment(message.created_at).format('h:mmA')}
538541
</MetaText>
539542
</MetaContainer>
540543
) : null}

src/components/MessageSimple/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export const MessageSimple = themed(
169169
PropTypes.node,
170170
PropTypes.elementType,
171171
]),
172+
formatDate: PropTypes.func,
172173
};
173174

174175
static defaultProps = {

types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,10 @@ export interface ChannelPreviewUIComponentProps
324324
latestMessage: {
325325
text: string;
326326
created_at: string;
327+
messageObject: Client.MessageResponse;
327328
};
329+
/** Length at which latest message should be truncated */
330+
latestMessageLength: number;
328331
}
329332

330333
export interface MessageListProps extends ChannelContextValue {
@@ -458,6 +461,7 @@ export interface MessageUIComponentProps
458461
/** https://github.com/beefe/react-native-actionsheet/blob/master/lib/styles.js */
459462
actionSheetStyles?: object;
460463
AttachmentFileIcon?: React.ElementType<FileIconUIComponentProps>;
464+
formatDate(date: string): string;
461465
}
462466

463467
export interface MessageRepliesUIComponentProps {

0 commit comments

Comments
 (0)