From 9da8b4bd0ad181b63b4c04c8ca34f47b039adfe5 Mon Sep 17 00:00:00 2001 From: Divyanshu Agrawal Date: Thu, 7 May 2020 16:21:47 +0530 Subject: [PATCH] people auto complete: Also send user ID for users having same full name. Previously, our @-mention autocomplete had a bug, wherein it failed to correctly mention the specific user if more than one users had the same full name. Now, if more than one users have the same full name, we send the user ID as well to ensure that the correct user is @-mentioned. The user ID is not sent for every @-mention to ensure the behaviour is consistent with the web app, i.e., we send it only in case of ambiguity. Uses `binarySearch` for better efficiency. --- src/autocomplete/PeopleAutocomplete.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/autocomplete/PeopleAutocomplete.js b/src/autocomplete/PeopleAutocomplete.js index 4e9f5a3d7c1..8069249bef3 100644 --- a/src/autocomplete/PeopleAutocomplete.js +++ b/src/autocomplete/PeopleAutocomplete.js @@ -13,6 +13,7 @@ import { import { Popup } from '../common'; import UserItem from '../users/UserItem'; import UserGroupItem from '../user-groups/UserGroupItem'; +import binarySearch from '../utils/binarySearch'; type Props = $ReadOnly<{| dispatch: Dispatch, @@ -32,6 +33,17 @@ class PeopleAutocomplete extends PureComponent { const { users, onAutocomplete } = this.props; const user = users.find(x => x.email === email); if (user) { + const idx = binarySearch(users, elem => { + if (elem.full_name === user.full_name && elem.user_id === user.user_id) { + return 1; + } + return elem.full_name.toLowerCase().localeCompare(user.full_name.toLowerCase()); + }); + + if (idx !== undefined) { + onAutocomplete(`**${user.full_name}|${user.user_id}**`); + return; + } onAutocomplete(`**${user.full_name}**`); } };