Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions resources/js/lib/conversation-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export function conversationsMatching(
const isPersonMatch = ({ handle, name }: User) =>
isMatch(handle) || isMatch(name);

const seen = new Set<string>();

return [
...found,
...onHand.filter(
Expand All @@ -26,8 +28,12 @@ export function conversationsMatching(
isPersonMatch(user),
),
),
].filter(
(conversation, index, all) =>
all.findIndex(({ id }) => id === conversation.id) === index,
);
].filter(({ id }) => {
if (seen.has(id)) {
return false;
}

seen.add(id);
return true;
});
}
11 changes: 11 additions & 0 deletions tests/Unit/ConversationSearch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ test('keeps every conversation the server found for the term, ahead of stale cop

assert.deepEqual(conversationsMatching('review', [org('core', 'Core', [ben])], found), [...found]);
});

test('deduplicates many matching server and local copies without changing their first match', () => {
const first = direct(ana, { ahead: 4 });
const duplicates = Array.from({ length: 100 }, () => direct(ana));
const last = org('team', 'Ana team', [ben]);

assert.deepEqual(
conversationsMatching('ana', [...duplicates, last], [first, first]),
[first, last],
);
});