diff --git a/lib/shared/message-utils.js b/lib/shared/message-utils.js --- a/lib/shared/message-utils.js +++ b/lib/shared/message-utils.js @@ -31,6 +31,7 @@ import type { RelativeUserInfo, UserInfos } from '../types/user-types'; import { codeBlockRegex, type ParserRules } from './markdown'; import { messageSpecs } from './messages/message-specs'; +import { threadIsGroupChat } from './thread-utils'; import { stringForUser } from './user-utils'; const localIDPrefix = 'local'; @@ -507,6 +508,35 @@ }; } +type MessagePreviewResult = { + +message: string, + +username: ?string, +}; +function getMessagePreview( + originalMessageInfo: MessageInfo, + threadInfo: ThreadInfo, + markdownRules: ParserRules, +): MessagePreviewResult { + const messageInfo: ComposableMessageInfo | RobotextMessageInfo = + originalMessageInfo.type === messageTypes.SIDEBAR_SOURCE + ? originalMessageInfo.sourceMessage + : originalMessageInfo; + + const messageTitle = getMessageTitle(messageInfo, threadInfo, markdownRules); + + const hasUsername = + threadIsGroupChat(threadInfo) || + threadInfo.name !== '' || + messageInfo.creator.isViewer; + + let userString = null; + if (messageInfo.type === messageTypes.TEXT && hasUsername) { + userString = stringForUser(messageInfo.creator); + } + + return { message: messageTitle, username: userString }; +} + export { localIDPrefix, messageKey, @@ -534,4 +564,5 @@ getMessageTitle, removeCreatorAsViewer, mergeThreadMessageInfos, + getMessagePreview, }; diff --git a/web/chat/message-preview.react.js b/web/chat/message-preview.react.js --- a/web/chat/message-preview.react.js +++ b/web/chat/message-preview.react.js @@ -3,15 +3,8 @@ import classNames from 'classnames'; import * as React from 'react'; -import { getMessageTitle } from 'lib/shared/message-utils'; -import { threadIsGroupChat } from 'lib/shared/thread-utils'; -import { stringForUser } from 'lib/shared/user-utils'; -import { - type MessageInfo, - messageTypes, - type ComposableMessageInfo, - type RobotextMessageInfo, -} from 'lib/types/message-types'; +import { getMessagePreview } from 'lib/shared/message-utils'; +import { type MessageInfo } from 'lib/types/message-types'; import { type ThreadInfo } from 'lib/types/thread-types'; import { getDefaultTextMessageRules } from '../markdown/rules.react'; @@ -38,26 +31,16 @@ ); } - const messageInfo: ComposableMessageInfo | RobotextMessageInfo = - originalMessageInfo.type === messageTypes.SIDEBAR_SOURCE - ? originalMessageInfo.sourceMessage - : originalMessageInfo; - const messageTitle = getMessageTitle( - messageInfo, + const { message: messageTitle, username } = getMessagePreview( + originalMessageInfo, threadInfo, getDefaultTextMessageRules().simpleMarkdownRules, ); - const hasUsername = - threadIsGroupChat(threadInfo) || - threadInfo.name !== '' || - messageInfo.creator.isViewer; - let usernameText = null; - if (messageInfo.type === messageTypes.TEXT && hasUsername) { - const userString = stringForUser(messageInfo.creator); - usernameText = {`${userString}: `}; + if (username) { + usernameText = {`${username}: `}; } return (