In left pane and "add group member", sort E164-only contacts to the bottom

This commit is contained in:
Evan Hahn 2021-03-11 17:33:12 -06:00 committed by Josh Perez
parent 053c18f295
commit 45dbe4d145
2 changed files with 59 additions and 11 deletions

View file

@ -10,32 +10,53 @@ describe('filterAndSortContacts', () => {
const conversations = [ const conversations = [
getDefaultConversation({ getDefaultConversation({
title: '+16505551234', title: '+16505551234',
firstName: undefined, e164: '+16505551234',
name: undefined,
profileName: undefined, profileName: undefined,
}), }),
getDefaultConversation({ title: 'Carlos Santana' }), getDefaultConversation({
getDefaultConversation({ title: 'Aaron Aardvark' }), name: 'Carlos Santana',
getDefaultConversation({ title: 'Belinda Beetle' }), title: 'Carlos Santana',
getDefaultConversation({ title: 'Belinda Zephyr' }), e164: '+16505559876',
}),
getDefaultConversation({
name: 'Aaron Aardvark',
title: 'Aaron Aardvark',
}),
getDefaultConversation({
name: 'Belinda Beetle',
title: 'Belinda Beetle',
}),
getDefaultConversation({
name: 'Belinda Zephyr',
title: 'Belinda Zephyr',
}),
]; ];
it('without a search term, sorts conversations by title', () => { it('without a search term, sorts conversations by title (but puts no-name contacts at the bottom)', () => {
const titles = filterAndSortContacts(conversations, '').map( const titles = filterAndSortContacts(conversations, '').map(
contact => contact.title contact => contact.title
); );
assert.deepEqual(titles, [ assert.deepEqual(titles, [
'+16505551234',
'Aaron Aardvark', 'Aaron Aardvark',
'Belinda Beetle', 'Belinda Beetle',
'Belinda Zephyr', 'Belinda Zephyr',
'Carlos Santana', 'Carlos Santana',
'+16505551234',
]); ]);
}); });
it('filters conversations a search terms', () => { it('can search for contacts by title', () => {
const titles = filterAndSortContacts(conversations, 'belind').map( const titles = filterAndSortContacts(conversations, 'belind').map(
contact => contact.title contact => contact.title
); );
assert.deepEqual(titles, ['Belinda Beetle', 'Belinda Zephyr']); assert.sameMembers(titles, ['Belinda Beetle', 'Belinda Zephyr']);
});
it('can search for contacts by phone number (and puts no-name contacts at the bottom)', () => {
const titles = filterAndSortContacts(conversations, '650555').map(
contact => contact.title
);
assert.sameMembers(titles, ['Carlos Santana', '+16505551234']);
}); });
}); });

View file

@ -9,7 +9,20 @@ const FUSE_OPTIONS: FuseOptions<ConversationType> = {
// A small-but-nonzero threshold lets us match parts of E164s better, and makes the // A small-but-nonzero threshold lets us match parts of E164s better, and makes the
// search a little more forgiving. // search a little more forgiving.
threshold: 0.05, threshold: 0.05,
keys: ['title', 'name', 'e164'], keys: [
{
name: 'title',
weight: 1,
},
{
name: 'name',
weight: 1,
},
{
name: 'e164',
weight: 0.5,
},
],
}; };
const collator = new Intl.Collator(); const collator = new Intl.Collator();
@ -23,5 +36,19 @@ export function filterAndSortContacts(
searchTerm searchTerm
); );
} }
return contacts.concat().sort((a, b) => collator.compare(a.title, b.title));
return contacts.concat().sort((a, b) => {
const aHasName = hasName(a);
const bHasName = hasName(b);
if (aHasName === bHasName) {
return collator.compare(a.title, b.title);
}
return aHasName && !bHasName ? -1 : 1;
});
}
function hasName(contact: Readonly<ConversationType>): boolean {
return Boolean(contact.name || contact.profileName);
} }