signal-desktop/ts/components/MessageSearchResult.tsx

169 lines
3.9 KiB
TypeScript
Raw Normal View History

2019-01-14 21:49:58 +00:00
import React from 'react';
import classNames from 'classnames';
import { Avatar } from './Avatar';
import { MessageBodyHighlight } from './MessageBodyHighlight';
import { Timestamp } from './conversation/Timestamp';
import { ContactName } from './conversation/ContactName';
import { LocalizerType } from '../types/Util';
export type PropsDataType = {
isSelected?: boolean;
2019-08-09 23:12:29 +00:00
isSearchingInConversation?: boolean;
2019-01-14 21:49:58 +00:00
id: string;
conversationId: string;
receivedAt: number;
snippet: string;
from: {
phoneNumber: string;
isMe?: boolean;
name?: string;
color?: string;
profileName?: string;
avatarPath?: string;
};
to: {
groupName?: string;
phoneNumber: string;
isMe?: boolean;
name?: string;
profileName?: string;
};
};
type PropsHousekeepingType = {
2019-01-14 21:49:58 +00:00
i18n: LocalizerType;
openConversationInternal: (
conversationId: string,
messageId?: string
) => void;
2019-01-14 21:49:58 +00:00
};
type PropsType = PropsDataType & PropsHousekeepingType;
2019-01-14 21:49:58 +00:00
export class MessageSearchResult extends React.PureComponent<PropsType> {
2019-01-14 21:49:58 +00:00
public renderFromName() {
const { from, i18n, to } = this.props;
if (from.isMe && to.isMe) {
return (
<span className="module-message-search-result__header__name">
{i18n('noteToSelf')}
</span>
);
}
if (from.isMe) {
return (
<span className="module-message-search-result__header__name">
{i18n('you')}
</span>
);
}
return (
<ContactName
phoneNumber={from.phoneNumber}
name={from.name}
profileName={from.profileName}
module="module-message-search-result__header__name"
/>
);
}
public renderFrom() {
2019-08-09 23:12:29 +00:00
const { i18n, to, isSearchingInConversation } = this.props;
2019-01-14 21:49:58 +00:00
const fromName = this.renderFromName();
2019-08-09 23:12:29 +00:00
if (!to.isMe && !isSearchingInConversation) {
2019-01-14 21:49:58 +00:00
return (
<div className="module-message-search-result__header__from">
2019-10-04 18:06:17 +00:00
{fromName} {i18n('toJoiner')}{' '}
2019-01-14 21:49:58 +00:00
<span className="module-mesages-search-result__header__group">
<ContactName
phoneNumber={to.phoneNumber}
name={to.name}
profileName={to.profileName}
/>
</span>
</div>
);
}
return (
<div className="module-message-search-result__header__from">
{fromName}
</div>
);
}
public renderAvatar() {
const { from, i18n, to } = this.props;
const isNoteToSelf = from.isMe && to.isMe;
return (
<Avatar
avatarPath={from.avatarPath}
color={from.color}
conversationType="direct"
i18n={i18n}
name={name}
noteToSelf={isNoteToSelf}
phoneNumber={from.phoneNumber}
profileName={from.profileName}
2019-10-04 18:06:17 +00:00
size={52}
2019-01-14 21:49:58 +00:00
/>
);
}
public render() {
const {
from,
i18n,
id,
isSelected,
conversationId,
openConversationInternal,
2019-01-14 21:49:58 +00:00
receivedAt,
snippet,
to,
} = this.props;
if (!from || !to) {
return null;
}
return (
2019-11-07 21:36:16 +00:00
<button
2019-01-14 21:49:58 +00:00
onClick={() => {
if (openConversationInternal) {
openConversationInternal(conversationId, id);
2019-01-14 21:49:58 +00:00
}
}}
className={classNames(
'module-message-search-result',
isSelected ? 'module-message-search-result--is-selected' : null
)}
2019-11-07 21:36:16 +00:00
data-id={id}
2019-01-14 21:49:58 +00:00
>
{this.renderAvatar()}
<div className="module-message-search-result__text">
<div className="module-message-search-result__header">
{this.renderFrom()}
<div className="module-message-search-result__header__timestamp">
<Timestamp timestamp={receivedAt} i18n={i18n} />
</div>
</div>
<div className="module-message-search-result__body">
<MessageBodyHighlight text={snippet} i18n={i18n} />
</div>
</div>
2019-11-07 21:36:16 +00:00
</button>
2019-01-14 21:49:58 +00:00
);
}
}