signal-desktop/ts/components/MessageSearchResult.tsx

178 lines
4.2 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';
import { ColorType } from '../types/Colors';
2019-01-14 21:49:58 +00:00
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;
sentAt: number;
2019-01-14 21:49:58 +00:00
snippet: string;
from: {
2020-07-24 01:35:32 +00:00
phoneNumber?: string;
title: string;
2019-01-14 21:49:58 +00:00
isMe?: boolean;
name?: string;
color?: ColorType;
2019-01-14 21:49:58 +00:00
profileName?: string;
avatarPath?: string;
};
to: {
groupName?: string;
2020-07-24 01:35:32 +00:00
phoneNumber?: string;
title: string;
2019-01-14 21:49:58 +00:00
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
};
export type PropsType = PropsDataType & PropsHousekeepingType;
2019-01-14 21:49:58 +00:00
export class MessageSearchResult extends React.PureComponent<PropsType> {
2020-09-12 00:46:52 +00:00
public renderFromName(): JSX.Element {
2019-01-14 21:49:58 +00:00
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}
2020-07-24 01:35:32 +00:00
title={from.title}
2019-01-14 21:49:58 +00:00
module="module-message-search-result__header__name"
2020-07-24 01:35:32 +00:00
i18n={i18n}
2019-01-14 21:49:58 +00:00
/>
);
}
2020-09-12 00:46:52 +00:00
public renderFrom(): JSX.Element {
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}
2020-07-24 01:35:32 +00:00
title={to.title}
i18n={i18n}
2019-01-14 21:49:58 +00:00
/>
</span>
</div>
);
}
return (
<div className="module-message-search-result__header__from">
{fromName}
</div>
);
}
2020-09-12 00:46:52 +00:00
public renderAvatar(): JSX.Element {
2019-01-14 21:49:58 +00:00
const { from, i18n, to } = this.props;
const isNoteToSelf = from.isMe && to.isMe;
return (
<Avatar
avatarPath={from.avatarPath}
color={from.color}
conversationType="direct"
i18n={i18n}
2020-09-12 00:46:52 +00:00
name={from.name}
2019-01-14 21:49:58 +00:00
noteToSelf={isNoteToSelf}
phoneNumber={from.phoneNumber}
profileName={from.profileName}
2020-07-24 01:35:32 +00:00
title={from.title}
2019-10-04 18:06:17 +00:00
size={52}
2019-01-14 21:49:58 +00:00
/>
);
}
2020-09-12 00:46:52 +00:00
public render(): JSX.Element | null {
2019-01-14 21:49:58 +00:00
const {
from,
i18n,
id,
isSelected,
conversationId,
openConversationInternal,
sentAt,
2019-01-14 21:49:58 +00:00
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}
2020-09-12 00:46:52 +00:00
type="button"
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={sentAt} i18n={i18n} />
2019-01-14 21:49:58 +00:00
</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
);
}
}