Skip to content

Commit

Permalink
Merge pull request #19221 from allroundexperts/fix-18746
Browse files Browse the repository at this point in the history
Include yourself and @here options in mentions auto-suggestion list
  • Loading branch information
puneetlath committed May 23, 2023
2 parents 312cb9f + 3334ef5 commit 1ac9964
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ const CONST = {
SMALL_CONTAINER_HEIGHT_FACTOR: 2.5,
MIN_AMOUNT_OF_ITEMS: 3,
MAX_AMOUNT_OF_ITEMS: 5,
HERE_TEXT: '@here',
},
COMPOSER_MAX_HEIGHT: 125,
CHAT_FOOTER_MIN_HEIGHT: 65,
Expand Down
3 changes: 3 additions & 0 deletions src/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ export default {
sayHello: 'Say hello!',
usePlusButton: '\n\nYou can also use the + button below to send or request money!',
},
mentionSuggestions: {
hereAlternateText: 'Notify everyone online in this room',
},
newMessages: 'New messages',
reportTypingIndicator: {
isTyping: 'is typing...',
Expand Down
3 changes: 3 additions & 0 deletions src/languages/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ export default {
sayHello: '¡Saluda!',
usePlusButton: '\n\n¡También puedes usar el botón + de abajo para enviar o pedir dinero!',
},
mentionSuggestions: {
hereAlternateText: 'Notificar a todos los que estén en linea de esta sala',
},
newMessages: 'Mensajes nuevos',
reportTypingIndicator: {
isTyping: 'está escribiendo...',
Expand Down
61 changes: 49 additions & 12 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import * as ComposerActions from '../../../libs/actions/Composer';
import * as Welcome from '../../../libs/actions/Welcome';
import Permissions from '../../../libs/Permissions';
import * as TaskUtils from '../../../libs/actions/Task';
import * as OptionsListUtils from '../../../libs/OptionsListUtils';

const propTypes = {
/** Beta features list */
Expand Down Expand Up @@ -116,9 +115,6 @@ const propTypes = {
/** The type of action that's pending */
pendingAction: PropTypes.oneOf(['add', 'update', 'delete']),

/** Collection of recent reports, used to calculate the mention suggestions */
reports: PropTypes.objectOf(reportPropTypes),

...windowDimensionsPropTypes,
...withLocalizePropTypes,
...withCurrentUserPersonalDetailsPropTypes,
Expand All @@ -137,7 +133,6 @@ const defaultProps = {
preferredSkinTone: CONST.EMOJI_DEFAULT_SKIN_TONE,
isComposerFullSize: false,
pendingAction: null,
reports: {},
shouldShowComposeInput: true,
...withCurrentUserPersonalDetailsDefaultProps,
};
Expand Down Expand Up @@ -450,6 +445,53 @@ class ReportActionCompose extends React.Component {
];
}

/**
* Build the suggestions for mentions
* @param {Object} personalDetails
* @param {String} [searchValue]
* @returns {Object}
*/
getMentionOptions(personalDetails, searchValue = '') {
const suggestions = [];

if (CONST.AUTO_COMPLETE_SUGGESTER.HERE_TEXT.includes(searchValue)) {
suggestions.push({
text: CONST.AUTO_COMPLETE_SUGGESTER.HERE_TEXT,
alternateText: this.props.translate('mentionSuggestions.hereAlternateText'),
icons: [
{
source: Expensicons.Megaphone,
type: 'avatar',
},
],
});
}

const filteredPersonalDetails = _.filter(_.values(personalDetails), (detail) => {
if (searchValue && !`${detail.displayName} ${detail.login}`.toLowerCase().includes(searchValue.toLowerCase())) {
return false;
}
return true;
});

const sortedPersonalDetails = _.sortBy(filteredPersonalDetails, (detail) => detail.displayName || detail.login);
_.each(_.first(sortedPersonalDetails, CONST.AUTO_COMPLETE_SUGGESTER.MAX_AMOUNT_OF_ITEMS - suggestions.length), (detail) => {
suggestions.push({
text: detail.displayName,
alternateText: detail.login,
icons: [
{
name: detail.login,
source: detail.avatar,
type: 'avatar',
},
],
});
});

return suggestions;
}

/**
* Clean data related to EmojiSuggestions and MentionSuggestions
*/
Expand Down Expand Up @@ -525,9 +567,7 @@ class ReportActionCompose extends React.Component {
const isCursorBeforeTheMention = valueAfterTheCursor.startsWith(lastWord);

if (!isCursorBeforeTheMention && this.isMentionCode(lastWord)) {
const options = OptionsListUtils.getNewChatOptions(this.props.reports, this.props.personalDetails, this.props.betas, prefix);
const suggestions = _.filter([...options.recentReports, options.userToInvite], (x) => !!x);

const suggestions = this.getMentionOptions(this.props.personalDetails, prefix);
nextState.suggestedMentions = suggestions;
nextState.shouldShowMentionSuggestionMenu = !_.isEmpty(suggestions);
}
Expand Down Expand Up @@ -592,7 +632,7 @@ class ReportActionCompose extends React.Component {
insertSelectedMention(highlightedMentionIndex) {
const commentBeforeAtSign = this.state.value.slice(0, this.state.atSignIndex);
const mentionObject = this.state.suggestedMentions[highlightedMentionIndex];
const mentionCode = `@${mentionObject.alternateText}`;
const mentionCode = mentionObject.text === CONST.AUTO_COMPLETE_SUGGESTER.HERE_TEXT ? CONST.AUTO_COMPLETE_SUGGESTER.HERE_TEXT : `@${mentionObject.alternateText}`;
const commentAfterAtSignWithMentionRemoved = this.state.value.slice(this.state.atSignIndex).replace(CONST.REGEX.MENTION_REPLACER, '');

this.updateComment(`${commentBeforeAtSign}${mentionCode} ${commentAfterAtSignWithMentionRemoved}`, true);
Expand Down Expand Up @@ -1188,9 +1228,6 @@ export default compose(
key: ONYXKEYS.PREFERRED_EMOJI_SKIN_TONE,
selector: EmojiUtils.getPreferredSkinToneIndex,
},
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS,
},
Expand Down

0 comments on commit 1ac9964

Please sign in to comment.