Skip to content

Commit

Permalink
people auto complete: Also send user ID for users having same full name.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
agrawal-d committed May 26, 2020
1 parent a1a7912 commit 0873981
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/autocomplete/PeopleAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,6 +33,18 @@ class PeopleAutocomplete extends PureComponent<Props> {
const { users, onAutocomplete } = this.props;
const user = users.find(x => x.email === email);
if (user) {
const idx = binarySearch(users, elem =>
elem.full_name.toLowerCase().localeCompare(user.full_name.toLowerCase()),
);
if (idx !== undefined) {
if (
(idx + 1 < users.length && user[idx + 1].full_name === user.full_name)
|| (idx - 1 >= 0 && user[idx - 1].full_name === user.full_name)
) {
onAutocomplete(`**${user.full_name}|${user.user_id}**`);
return;
}
}
onAutocomplete(`**${user.full_name}**`);
}
};
Expand Down

0 comments on commit 0873981

Please sign in to comment.