-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: ordered mention suggestions #42553
Merged
yuwenmemon
merged 3 commits into
Expensify:main
from
gijoe0295:gijoe/feat-ordered-mention-suggestions
Jun 14, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
import Str from 'expensify-common/lib/str'; | ||
import lodashMapValues from 'lodash/mapValues'; | ||
import lodashSortBy from 'lodash/sortBy'; | ||
import type {ForwardedRef} from 'react'; | ||
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react'; | ||
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react'; | ||
import {useOnyx} from 'react-native-onyx'; | ||
import type {OnyxCollection} from 'react-native-onyx'; | ||
import * as Expensicons from '@components/Icon/Expensicons'; | ||
import type {Mention} from '@components/MentionSuggestions'; | ||
import MentionSuggestions from '@components/MentionSuggestions'; | ||
import {usePersonalDetails} from '@components/OnyxProvider'; | ||
import useArrowKeyFocusManager from '@hooks/useArrowKeyFocusManager'; | ||
import useCurrentReportID from '@hooks/useCurrentReportID'; | ||
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; | ||
import useDebounce from '@hooks/useDebounce'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import * as LoginUtils from '@libs/LoginUtils'; | ||
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; | ||
import getPolicyEmployeeAccountIDs from '@libs/PolicyEmployeeListUtils'; | ||
import * as ReportUtils from '@libs/ReportUtils'; | ||
import * as SuggestionsUtils from '@libs/SuggestionUtils'; | ||
import {isValidRoomName} from '@libs/ValidationUtils'; | ||
import * as ReportUserActions from '@userActions/Report'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type {PersonalDetailsList, Report} from '@src/types/onyx'; | ||
import type {PersonalDetails, PersonalDetailsList, Report} from '@src/types/onyx'; | ||
import type {SuggestionsRef} from './ReportActionCompose'; | ||
import type {SuggestionProps} from './Suggestions'; | ||
|
||
|
@@ -45,6 +48,14 @@ const defaultSuggestionsValues: SuggestionValues = { | |
prefixType: '', | ||
}; | ||
|
||
type SuggestionPersonalDetailsList = Record< | ||
string, | ||
| (PersonalDetails & { | ||
weight: number; | ||
}) | ||
| null | ||
>; | ||
|
||
function SuggestionMention( | ||
{value, selection, setSelection, updateComment, isAutoSuggestionPickerLarge, measureParentContainer, isComposerFocused, isGroupPolicyReport, policyID}: SuggestionProps, | ||
ref: ForwardedRef<SuggestionsRef>, | ||
|
@@ -58,6 +69,36 @@ function SuggestionMention( | |
const currentUserPersonalDetails = useCurrentUserPersonalDetails(); | ||
const isMentionSuggestionsMenuVisible = !!suggestionValues.suggestedMentions.length && suggestionValues.shouldShowSuggestionMenu; | ||
|
||
const currentReportID = useCurrentReportID(); | ||
const currentReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${currentReportID?.currentReportID}`] ?? null; | ||
// Smaller weight means higher order in suggestion list | ||
const getPersonalDetailsWeight = useCallback( | ||
(detail: PersonalDetails, policyEmployeeAccountIDs: number[]): number => { | ||
if (ReportUtils.isReportParticipant(detail.accountID, currentReport)) { | ||
return 0; | ||
} | ||
if (policyEmployeeAccountIDs.includes(detail.accountID)) { | ||
return 1; | ||
} | ||
return 2; | ||
}, | ||
[currentReport], | ||
); | ||
const weightedPersonalDetails: PersonalDetailsList | SuggestionPersonalDetailsList = useMemo(() => { | ||
const policyEmployeeAccountIDs = getPolicyEmployeeAccountIDs(policyID); | ||
if (!ReportUtils.isGroupChat(currentReport) && !ReportUtils.doesReportBelongToWorkspace(currentReport, policyEmployeeAccountIDs, policyID)) { | ||
return personalDetails; | ||
} | ||
return lodashMapValues(personalDetails, (detail) => | ||
detail | ||
? { | ||
...detail, | ||
weight: getPersonalDetailsWeight(detail, policyEmployeeAccountIDs), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB: A more apt name could be maybe something like |
||
} | ||
: null, | ||
); | ||
}, [policyID, currentReport, personalDetails, getPersonalDetailsWeight]); | ||
|
||
const [highlightedMentionIndex, setHighlightedMentionIndex] = useArrowKeyFocusManager({ | ||
isActive: isMentionSuggestionsMenuVisible, | ||
maxIndex: suggestionValues.suggestedMentions.length - 1, | ||
|
@@ -190,7 +231,7 @@ function SuggestionMention( | |
); | ||
|
||
const getUserMentionOptions = useCallback( | ||
(personalDetailsParam: PersonalDetailsList, searchValue = ''): Mention[] => { | ||
(personalDetailsParam: PersonalDetailsList | SuggestionPersonalDetailsList, searchValue = ''): Mention[] => { | ||
const suggestions = []; | ||
|
||
if (CONST.AUTO_COMPLETE_SUGGESTER.HERE_TEXT.includes(searchValue.toLowerCase())) { | ||
|
@@ -231,8 +272,7 @@ function SuggestionMention( | |
return true; | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- nullish coalescing cannot be used if left side can be empty string | ||
const sortedPersonalDetails = lodashSortBy(filteredPersonalDetails, (detail) => detail?.displayName || detail?.login); | ||
const sortedPersonalDetails = lodashSortBy(filteredPersonalDetails, ['weight', 'displayName', 'login']); | ||
sortedPersonalDetails.slice(0, CONST.AUTO_COMPLETE_SUGGESTER.MAX_AMOUNT_OF_SUGGESTIONS - suggestions.length).forEach((detail) => { | ||
suggestions.push({ | ||
text: formatLoginPrivateDomain(PersonalDetailsUtils.getDisplayNameOrDefault(detail), detail?.login), | ||
|
@@ -320,7 +360,7 @@ function SuggestionMention( | |
}; | ||
|
||
if (isMentionCode(suggestionWord) && prefixType === '@') { | ||
const suggestions = getUserMentionOptions(personalDetails, prefix); | ||
const suggestions = getUserMentionOptions(weightedPersonalDetails, prefix); | ||
nextState.suggestedMentions = suggestions; | ||
nextState.shouldShowSuggestionMenu = !!suggestions.length; | ||
} | ||
|
@@ -340,7 +380,7 @@ function SuggestionMention( | |
})); | ||
setHighlightedMentionIndex(0); | ||
}, | ||
[isComposerFocused, value, isGroupPolicyReport, setHighlightedMentionIndex, resetSuggestions, getUserMentionOptions, personalDetails, getRoomMentionOptions, reports], | ||
[isComposerFocused, value, isGroupPolicyReport, setHighlightedMentionIndex, resetSuggestions, getUserMentionOptions, weightedPersonalDetails, getRoomMentionOptions, reports], | ||
); | ||
|
||
useEffect(() => { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this condition is met in my case, that is why weighted ordering didn't work. Removing it helped and showed group members first. I think we don't need this condition. Can we remove it safely?
@gijoe0295
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per OP:
Removing this condition would introduce unnecessary computation of
weightedPersonalDetails
for normal reports.I think there's some incorrect data with your
currentReport
. If it was a group chat, the condition would work properly. Maybe let's log out then log in to see if it got the correct data?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I confused the cases in the condition, that's why my conclusion was wrong
I see that for a report to be a group chat it should have
chatType=group
but it is empty for this case. Maybe it is a thing with older groups. I checked this with multiple old groups in two accounts and the same true for allBelow is
currentReport, policyEmployeeAccountIDs, policyID
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is another bug and not related to this PR. Before group chats, we had group DMs. Maybe the migration from group DMs had some problems.
cc @yuwenmemon We found several group chats with empty
chatType
(should begroup
), do you have any clue on this? Should we ignore it since it is not related to this PR?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should proceed with this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gijoe0295 Yeah maybe let's raise in expensify-bugs: https://expensify.enterprise.slack.com/archives/C049HHMV9SM