2021-04-30 19:40:25 +00:00
|
|
|
// Copyright 2019-2021 Signal Messenger, LLC
|
2021-02-23 20:34:28 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { FunctionComponent, ReactNode } from 'react';
|
|
|
|
import React, { useCallback } from 'react';
|
2021-03-04 19:34:04 +00:00
|
|
|
import { escapeRegExp } from 'lodash';
|
2021-02-23 20:34:28 +00:00
|
|
|
|
|
|
|
import { MessageBodyHighlight } from './MessageBodyHighlight';
|
|
|
|
import { ContactName } from '../conversation/ContactName';
|
|
|
|
|
2022-09-15 19:17:15 +00:00
|
|
|
import { assertDev } from '../../util/assert';
|
2021-11-17 21:11:21 +00:00
|
|
|
import type {
|
|
|
|
BodyRangesType,
|
|
|
|
LocalizerType,
|
|
|
|
ThemeType,
|
|
|
|
} from '../../types/Util';
|
2021-02-23 20:34:28 +00:00
|
|
|
import { BaseConversationListItem } from './BaseConversationListItem';
|
2022-06-16 19:12:50 +00:00
|
|
|
import type {
|
|
|
|
ConversationType,
|
|
|
|
ShowConversationType,
|
|
|
|
} from '../../state/ducks/conversations';
|
2021-11-17 21:11:21 +00:00
|
|
|
import type { PreferredBadgeSelectorType } from '../../state/selectors/badges';
|
2021-02-23 20:34:28 +00:00
|
|
|
|
|
|
|
export type PropsDataType = {
|
|
|
|
isSelected?: boolean;
|
|
|
|
isSearchingInConversation?: boolean;
|
|
|
|
|
|
|
|
id: string;
|
|
|
|
conversationId: string;
|
|
|
|
sentAt?: number;
|
|
|
|
|
|
|
|
snippet: string;
|
2021-03-04 19:34:04 +00:00
|
|
|
body: string;
|
|
|
|
bodyRanges: BodyRangesType;
|
2021-02-23 20:34:28 +00:00
|
|
|
|
2021-04-30 19:40:25 +00:00
|
|
|
from: Pick<
|
|
|
|
ConversationType,
|
|
|
|
| 'acceptedMessageRequest'
|
|
|
|
| 'avatarPath'
|
2021-11-17 21:11:21 +00:00
|
|
|
| 'badges'
|
2021-04-30 19:40:25 +00:00
|
|
|
| 'color'
|
|
|
|
| 'isMe'
|
|
|
|
| 'name'
|
|
|
|
| 'phoneNumber'
|
|
|
|
| 'profileName'
|
|
|
|
| 'sharedGroupNames'
|
|
|
|
| 'title'
|
|
|
|
| 'unblurredAvatarPath'
|
|
|
|
>;
|
2021-02-23 20:34:28 +00:00
|
|
|
|
|
|
|
to: {
|
|
|
|
groupName?: string;
|
|
|
|
phoneNumber?: string;
|
|
|
|
title: string;
|
|
|
|
isMe?: boolean;
|
|
|
|
name?: string;
|
|
|
|
profileName?: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
type PropsHousekeepingType = {
|
2021-11-17 21:11:21 +00:00
|
|
|
getPreferredBadge: PreferredBadgeSelectorType;
|
2021-02-23 20:34:28 +00:00
|
|
|
i18n: LocalizerType;
|
2022-06-16 19:12:50 +00:00
|
|
|
showConversation: ShowConversationType;
|
2021-11-17 21:11:21 +00:00
|
|
|
theme: ThemeType;
|
2021-02-23 20:34:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type PropsType = PropsDataType & PropsHousekeepingType;
|
|
|
|
|
|
|
|
const renderPerson = (
|
|
|
|
i18n: LocalizerType,
|
|
|
|
person: Readonly<{
|
|
|
|
isMe?: boolean;
|
|
|
|
title: string;
|
|
|
|
}>
|
|
|
|
): ReactNode =>
|
2021-09-16 16:15:43 +00:00
|
|
|
person.isMe ? i18n('you') : <ContactName title={person.title} />;
|
2021-02-23 20:34:28 +00:00
|
|
|
|
2021-03-04 19:34:04 +00:00
|
|
|
// This function exists because bodyRanges tells us the character position
|
|
|
|
// where the at-mention starts at according to the full body text. The snippet
|
|
|
|
// we get back is a portion of the text and we don't know where it starts. This
|
|
|
|
// function will find the relevant bodyRanges that apply to the snippet and
|
|
|
|
// then update the proper start position of each body range.
|
|
|
|
function getFilteredBodyRanges(
|
|
|
|
snippet: string,
|
|
|
|
body: string,
|
|
|
|
bodyRanges: BodyRangesType
|
|
|
|
): BodyRangesType {
|
2021-04-28 21:48:52 +00:00
|
|
|
if (!bodyRanges.length) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-03-04 19:34:04 +00:00
|
|
|
// Find where the snippet starts in the full text
|
|
|
|
const stripped = snippet
|
|
|
|
.replace(/<<left>>/g, '')
|
|
|
|
.replace(/<<right>>/g, '')
|
|
|
|
.replace(/^.../, '')
|
|
|
|
.replace(/...$/, '');
|
|
|
|
const rx = new RegExp(escapeRegExp(stripped));
|
|
|
|
const match = rx.exec(body);
|
|
|
|
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(Boolean(match), `No match found for "${snippet}" inside "${body}"`);
|
2021-03-04 19:34:04 +00:00
|
|
|
|
|
|
|
const delta = match ? match.index + snippet.length : 0;
|
|
|
|
|
|
|
|
// Filters out the @mentions that are present inside the snippet
|
|
|
|
const filteredBodyRanges = bodyRanges.filter(bodyRange => {
|
|
|
|
return bodyRange.start < delta;
|
|
|
|
});
|
|
|
|
|
|
|
|
const snippetBodyRanges = [];
|
|
|
|
const MENTIONS_REGEX = /\uFFFC/g;
|
|
|
|
|
|
|
|
let bodyRangeMatch = MENTIONS_REGEX.exec(snippet);
|
|
|
|
let i = 0;
|
|
|
|
|
|
|
|
// Find the start position within the snippet so these can later be
|
|
|
|
// encoded and rendered correctly.
|
|
|
|
while (bodyRangeMatch) {
|
|
|
|
const bodyRange = filteredBodyRanges[i];
|
|
|
|
|
|
|
|
if (bodyRange) {
|
|
|
|
snippetBodyRanges.push({
|
|
|
|
...bodyRange,
|
|
|
|
start: bodyRangeMatch.index,
|
|
|
|
});
|
|
|
|
} else {
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(
|
2021-03-04 19:34:04 +00:00
|
|
|
false,
|
|
|
|
`Body range does not exist? Count: ${i}, Length: ${filteredBodyRanges.length}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyRangeMatch = MENTIONS_REGEX.exec(snippet);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return snippetBodyRanges;
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:34:28 +00:00
|
|
|
export const MessageSearchResult: FunctionComponent<PropsType> = React.memo(
|
2021-08-11 19:29:07 +00:00
|
|
|
function MessageSearchResult({
|
2021-03-04 19:34:04 +00:00
|
|
|
body,
|
|
|
|
bodyRanges,
|
2021-02-23 20:34:28 +00:00
|
|
|
conversationId,
|
|
|
|
from,
|
2021-11-17 21:11:21 +00:00
|
|
|
getPreferredBadge,
|
2021-02-23 20:34:28 +00:00
|
|
|
i18n,
|
2021-03-04 19:34:04 +00:00
|
|
|
id,
|
|
|
|
sentAt,
|
2022-06-16 19:12:50 +00:00
|
|
|
showConversation,
|
2021-02-23 20:34:28 +00:00
|
|
|
snippet,
|
2021-11-17 21:11:21 +00:00
|
|
|
theme,
|
2021-03-04 19:34:04 +00:00
|
|
|
to,
|
2021-08-11 19:29:07 +00:00
|
|
|
}) {
|
2021-02-23 20:34:28 +00:00
|
|
|
const onClickItem = useCallback(() => {
|
2022-06-16 19:12:50 +00:00
|
|
|
showConversation({ conversationId, messageId: id });
|
|
|
|
}, [showConversation, conversationId, id]);
|
2021-02-23 20:34:28 +00:00
|
|
|
|
|
|
|
if (!from || !to) {
|
2021-08-11 16:23:21 +00:00
|
|
|
return <div />;
|
2021-02-23 20:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const isNoteToSelf = from.isMe && to.isMe;
|
|
|
|
|
|
|
|
let headerName: ReactNode;
|
|
|
|
if (isNoteToSelf) {
|
|
|
|
headerName = i18n('noteToSelf');
|
|
|
|
} else {
|
|
|
|
// This isn't perfect because (1) it doesn't work with RTL languages (2)
|
|
|
|
// capitalization may be incorrect for some languages, like English.
|
|
|
|
headerName = (
|
2021-10-18 22:43:03 +00:00
|
|
|
<span>
|
2021-02-23 20:34:28 +00:00
|
|
|
{renderPerson(i18n, from)} {i18n('toJoiner')} {renderPerson(i18n, to)}
|
2021-10-18 22:43:03 +00:00
|
|
|
</span>
|
2021-02-23 20:34:28 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-04 19:34:04 +00:00
|
|
|
const snippetBodyRanges = getFilteredBodyRanges(snippet, body, bodyRanges);
|
|
|
|
const messageText = (
|
|
|
|
<MessageBodyHighlight
|
|
|
|
text={snippet}
|
|
|
|
bodyRanges={snippetBodyRanges}
|
|
|
|
i18n={i18n}
|
|
|
|
/>
|
|
|
|
);
|
2021-02-23 20:34:28 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<BaseConversationListItem
|
2021-04-30 19:40:25 +00:00
|
|
|
acceptedMessageRequest={from.acceptedMessageRequest}
|
2021-02-23 20:34:28 +00:00
|
|
|
avatarPath={from.avatarPath}
|
2021-11-17 21:11:21 +00:00
|
|
|
badge={getPreferredBadge(from.badges)}
|
2021-02-23 20:34:28 +00:00
|
|
|
color={from.color}
|
|
|
|
conversationType="direct"
|
|
|
|
headerDate={sentAt}
|
|
|
|
headerName={headerName}
|
|
|
|
i18n={i18n}
|
|
|
|
id={id}
|
|
|
|
isNoteToSelf={isNoteToSelf}
|
|
|
|
isMe={from.isMe}
|
|
|
|
isSelected={false}
|
|
|
|
messageText={messageText}
|
|
|
|
name={from.name}
|
|
|
|
onClick={onClickItem}
|
|
|
|
phoneNumber={from.phoneNumber}
|
|
|
|
profileName={from.profileName}
|
2021-04-30 19:40:25 +00:00
|
|
|
sharedGroupNames={from.sharedGroupNames}
|
2021-11-17 21:11:21 +00:00
|
|
|
theme={theme}
|
2021-02-23 20:34:28 +00:00
|
|
|
title={from.title}
|
2021-04-30 19:40:25 +00:00
|
|
|
unblurredAvatarPath={from.unblurredAvatarPath}
|
2021-02-23 20:34:28 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|