From 2a73e488f9e0cc172edf5011bf1641430863968c Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Thu, 4 Jan 2024 10:17:59 +0100 Subject: [PATCH 1/3] refactor: remove getDisplayNamesStringFromTooltips --- src/libs/GroupChatUtils.ts | 22 +++++++--------------- src/libs/ReportUtils.ts | 12 ------------ 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/src/libs/GroupChatUtils.ts b/src/libs/GroupChatUtils.ts index ba14bc9c9c3d..26b3665ca4ce 100644 --- a/src/libs/GroupChatUtils.ts +++ b/src/libs/GroupChatUtils.ts @@ -1,26 +1,18 @@ -import type {OnyxEntry} from 'react-native-onyx'; -import Onyx from 'react-native-onyx'; -import ONYXKEYS from '@src/ONYXKEYS'; -import type {PersonalDetailsList, Report} from '@src/types/onyx'; -import * as OptionsListUtils from './OptionsListUtils'; +import type {Report} from '@src/types/onyx'; import * as ReportUtils from './ReportUtils'; -let allPersonalDetails: OnyxEntry = {}; -Onyx.connect({ - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - callback: (val) => (allPersonalDetails = val), -}); - /** * Returns the report name if the report is a group chat */ function getGroupChatName(report: Report): string | undefined { const participants = report.participantAccountIDs ?? []; const isMultipleParticipantReport = participants.length > 1; - const participantPersonalDetails = OptionsListUtils.getPersonalDetailsForAccountIDs(participants, allPersonalDetails ?? {}); - // @ts-expect-error Error will gone when OptionsListUtils will be migrated to Typescript - const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips(participantPersonalDetails, isMultipleParticipantReport); - return ReportUtils.getDisplayNamesStringFromTooltips(displayNamesWithTooltips); + + return participants + .map((participant) => ReportUtils.getDisplayNameForParticipant(participant, isMultipleParticipantReport)) + .sort((first, second) => first?.localeCompare(second ?? '') ?? 0) + .filter(Boolean) + .join(', '); } export { diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 33a18b8534df..4a8883ddf60c 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1537,17 +1537,6 @@ function getDisplayNamesWithTooltips( }); } -/** - * Gets a joined string of display names from the list of display name with tooltip objects. - * - */ -function getDisplayNamesStringFromTooltips(displayNamesWithTooltips: DisplayNameWithTooltips | undefined) { - return displayNamesWithTooltips - ?.map(({displayName}) => displayName) - .filter(Boolean) - .join(', '); -} - /** * For a deleted parent report action within a chat report, * let us return the appropriate display message @@ -4317,7 +4306,6 @@ export { getIcons, getRoomWelcomeMessage, getDisplayNamesWithTooltips, - getDisplayNamesStringFromTooltips, getReportName, getReport, getReportNotificationPreference, From 1c53bfaa739ca31cd4e6ab3261378ffb6312b30b Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Thu, 4 Jan 2024 10:27:33 +0100 Subject: [PATCH 2/3] refactor: rename getDisplayName to createDisplayName --- src/libs/ReportUtils.ts | 1 - src/libs/actions/PersonalDetails.ts | 15 +++++++-------- src/libs/actions/User.js | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 4a8883ddf60c..bfa384da7af6 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1475,7 +1475,6 @@ function getDisplayNameForParticipant(accountID?: number, shouldUseShortForm = f } const personalDetails = getPersonalDetailsForAccountID(accountID); - // console.log(personalDetails); // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing const formattedLogin = LocalePhoneNumber.formatPhoneNumber(personalDetails.login || ''); // This is to check if account is an invite/optimistically created one diff --git a/src/libs/actions/PersonalDetails.ts b/src/libs/actions/PersonalDetails.ts index 20c6d8fef247..6bcbb57695c9 100644 --- a/src/libs/actions/PersonalDetails.ts +++ b/src/libs/actions/PersonalDetails.ts @@ -41,20 +41,19 @@ Onyx.connect({ }); /** - * Returns the displayName for a user + * Creates a new displayName for a user based on passed personal details or login. */ -function getDisplayName(login: string, personalDetail: Pick | null): string { +function createDisplayName(login: string, personalDetails: Pick): string { // If we have a number like +15857527441@expensify.sms then let's remove @expensify.sms and format it // so that the option looks cleaner in our UI. const userLogin = LocalePhoneNumber.formatPhoneNumber(login); - const userDetails = personalDetail ?? allPersonalDetails?.[login]; - if (!userDetails) { + if (!personalDetails) { return userLogin; } - const firstName = userDetails.firstName ?? ''; - const lastName = userDetails.lastName ?? ''; + const firstName = personalDetails.firstName ?? ''; + const lastName = personalDetails.lastName ?? ''; const fullName = `${firstName} ${lastName}`.trim(); // It's possible for fullName to be empty string, so we must use "||" to fallback to userLogin. @@ -148,7 +147,7 @@ function updateDisplayName(firstName: string, lastName: string) { [currentUserAccountID]: { firstName, lastName, - displayName: getDisplayName(currentUserEmail ?? '', { + displayName: createDisplayName(currentUserEmail ?? '', { firstName, lastName, }), @@ -560,7 +559,7 @@ export { deleteAvatar, extractFirstAndLastNameFromAvailableDetails, getCountryISO, - getDisplayName, + createDisplayName, getPrivatePersonalDetails, openPersonalDetailsPage, openPublicProfilePage, diff --git a/src/libs/actions/User.js b/src/libs/actions/User.js index 67828c766147..4984d32bafb5 100644 --- a/src/libs/actions/User.js +++ b/src/libs/actions/User.js @@ -724,7 +724,7 @@ function setContactMethodAsDefault(newDefaultContactMethod) { value: { [currentUserAccountID]: { login: newDefaultContactMethod, - displayName: PersonalDetails.getDisplayName(newDefaultContactMethod, myPersonalDetails), + displayName: PersonalDetails.createDisplayName(newDefaultContactMethod, myPersonalDetails), }, }, }, From d8b9af7b44d618088de2e72a895e8bfe0cd8539c Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Tue, 9 Jan 2024 17:03:40 +0100 Subject: [PATCH 3/3] fix: fix typing --- src/libs/actions/PersonalDetails.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/PersonalDetails.ts b/src/libs/actions/PersonalDetails.ts index 18320f3f2406..508cca34fb88 100644 --- a/src/libs/actions/PersonalDetails.ts +++ b/src/libs/actions/PersonalDetails.ts @@ -45,7 +45,7 @@ Onyx.connect({ /** * Creates a new displayName for a user based on passed personal details or login. */ -function createDisplayName(login: string, personalDetails: Pick): string { +function createDisplayName(login: string, personalDetails: Pick | OnyxEntry): string { // If we have a number like +15857527441@expensify.sms then let's remove @expensify.sms and format it // so that the option looks cleaner in our UI. const userLogin = LocalePhoneNumber.formatPhoneNumber(login);