Add filter commands for finding conversations

This commit is contained in:
Fedor Indutny 2022-04-27 11:52:43 -07:00 committed by GitHub
parent 472df5821a
commit de0450efe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 0 deletions

View File

@ -42,6 +42,29 @@ const cachedIndices = new WeakMap<
Fuse<ConversationType>
>();
type CommandRunnerType = (
conversations: ReadonlyArray<ConversationType>,
query: string
) => Array<ConversationType>;
const COMMANDS = new Map<string, CommandRunnerType>();
COMMANDS.set('uuidEndsWith', (conversations, query) => {
return conversations.filter(convo => convo.uuid?.endsWith(query));
});
COMMANDS.set('idEndsWith', (conversations, query) => {
return conversations.filter(convo => convo.id?.endsWith(query));
});
COMMANDS.set('e164EndsWith', (conversations, query) => {
return conversations.filter(convo => convo.e164?.endsWith(query));
});
COMMANDS.set('groupIdEndsWith', (conversations, query) => {
return conversations.filter(convo => convo.groupId?.endsWith(query));
});
// See https://fusejs.io/examples.html#extended-search for
// extended search documentation.
function searchConversations(
@ -49,6 +72,16 @@ function searchConversations(
searchTerm: string,
regionCode: string | undefined
): Array<ConversationType> {
const maybeCommand = searchTerm.match(/^!([^\s]+):(.*)$/);
if (maybeCommand) {
const [, commandName, query] = maybeCommand;
const command = COMMANDS.get(commandName);
if (command) {
return command(conversations, query);
}
}
const phoneNumber = parseAndFormatPhoneNumber(searchTerm, regionCode);
// Escape the search term