Skip to content
34 changes: 23 additions & 11 deletions src/libs/ReportNameUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import {Str} from 'expensify-common';
import Onyx from 'react-native-onyx';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import type {LocaleContextProps} from '@components/LocaleContextProvider';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {
Expand All @@ -21,7 +22,7 @@
import type {SelectedParticipant} from '@src/types/onyx/NewGroupChatDraft';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import {convertToDisplayString} from './CurrencyUtils';
import {formatPhoneNumber} from './LocalePhoneNumber';
import {formatPhoneNumber as formatPhoneNumberPhoneUtils} from './LocalePhoneNumber';
// eslint-disable-next-line @typescript-eslint/no-deprecated
import {translateLocal} from './Localize';
import {getForReportAction, getMovedReportID} from './ModifiedExpenseMessage';
Expand Down Expand Up @@ -113,14 +114,14 @@
let currentUserAccountID: number | undefined;
let allPersonalDetails: OnyxEntry<PersonalDetailsList>;

Onyx.connect({

Check warning on line 117 in src/libs/ReportNameUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
currentUserAccountID = value?.accountID;
},
});

Onyx.connect({

Check warning on line 124 in src/libs/ReportNameUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
allPersonalDetails = value;
Expand Down Expand Up @@ -148,7 +149,7 @@
accountID,
shouldUseShortForm: true,
personalDetailsData,
formatPhoneNumber,
formatPhoneNumber: formatPhoneNumberPhoneUtils,
}),
}))
.filter((participant) => participant.name)
Expand All @@ -158,7 +159,7 @@
return getDisplayNameForParticipant({
accountID,
personalDetailsData,
formatPhoneNumber,
formatPhoneNumber: formatPhoneNumberPhoneUtils,
});
}
return formattedNames ? `${formattedNames}, ${name}` : name;
Expand All @@ -175,7 +176,13 @@
/**
* Returns the report name if the report is a group chat
*/
function getGroupChatName(participants?: SelectedParticipant[], shouldApplyLimit = false, report?: OnyxEntry<Report>, reportMetadataParam?: OnyxEntry<ReportMetadata>): string | undefined {
function getGroupChatName(
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
participants?: SelectedParticipant[],
shouldApplyLimit = false,
report?: OnyxEntry<Report>,
reportMetadataParam?: OnyxEntry<ReportMetadata>,
): string | undefined {
// If we have a report always try to get the name from the report.
if (report?.reportName) {
return report.reportName;
Expand Down Expand Up @@ -222,7 +229,7 @@
const personalDetails = ownerAccountID ? personalDetailsList?.[ownerAccountID] : undefined;
const login = personalDetails ? personalDetails.login : null;
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const reportOwnerDisplayName = getDisplayNameForParticipant({accountID: ownerAccountID, shouldRemoveDomain: true, formatPhoneNumber}) || login;
const reportOwnerDisplayName = getDisplayNameForParticipant({accountID: ownerAccountID, shouldRemoveDomain: true, formatPhoneNumber: formatPhoneNumberPhoneUtils}) || login;

if (reportOwnerDisplayName) {
// eslint-disable-next-line @typescript-eslint/no-deprecated
Expand Down Expand Up @@ -260,7 +267,7 @@
}

if (isIndividual) {
return formatPhoneNumber(getDisplayNameOrDefault((personalDetails ?? allPersonalDetails)?.[invoiceReceiverAccountID]));
return formatPhoneNumberPhoneUtils(getDisplayNameOrDefault((personalDetails ?? allPersonalDetails)?.[invoiceReceiverAccountID]));
}

return getPolicyName({report, policy: receiverPolicyResolved, policies});
Expand All @@ -283,7 +290,7 @@

if (isIndividual) {
const personalDetail = invoiceReceiverPersonalDetail ?? allPersonalDetails?.[invoiceReceiver.accountID];
return formatPhoneNumber(getDisplayNameOrDefault(personalDetail ?? undefined));
return formatPhoneNumberPhoneUtils(getDisplayNameOrDefault(personalDetail ?? undefined));
}

return getPolicyName({report, policy: invoiceReceiverPolicy});
Expand All @@ -308,7 +315,7 @@
const chatReport = getReportOrDraftReport(report?.chatReportID);
payerOrApproverName = getInvoicePayerName(chatReport, invoiceReceiverPolicy);
} else {
payerOrApproverName = getDisplayNameForParticipant({accountID: report?.managerID, formatPhoneNumber}) ?? '';
payerOrApproverName = getDisplayNameForParticipant({accountID: report?.managerID, formatPhoneNumber: formatPhoneNumberPhoneUtils}) ?? '';
}
// eslint-disable-next-line @typescript-eslint/no-deprecated
const payerPaidAmountMessage = translateLocal('iou.payerPaidAmount', {
Expand All @@ -330,7 +337,7 @@
}

if (!isSettled(report?.reportID) && hasNonReimbursableTransactions(report?.reportID)) {
payerOrApproverName = getDisplayNameForParticipant({accountID: report?.ownerAccountID, formatPhoneNumber}) ?? '';
payerOrApproverName = getDisplayNameForParticipant({accountID: report?.ownerAccountID, formatPhoneNumber: formatPhoneNumberPhoneUtils}) ?? '';
// eslint-disable-next-line @typescript-eslint/no-deprecated
return translateLocal('iou.payerSpentAmount', {payer: payerOrApproverName, amount: formattedAmount});
}
Expand Down Expand Up @@ -643,7 +650,7 @@
}

if (isGroupChat(report)) {
return getGroupChatName(undefined, true, report) ?? '';
return getGroupChatName(formatPhoneNumberPhoneUtils, undefined, true, report) ?? '';
}

let formattedName: string | undefined;
Expand Down Expand Up @@ -683,7 +690,12 @@
}

if (isSelfDM(report)) {
formattedName = getDisplayNameForParticipant({accountID: report?.ownerAccountID, shouldAddCurrentUserPostfix: true, personalDetailsData: personalDetailsList, formatPhoneNumber});
formattedName = getDisplayNameForParticipant({
accountID: report?.ownerAccountID,
shouldAddCurrentUserPostfix: true,
personalDetailsData: personalDetailsList,
formatPhoneNumber: formatPhoneNumberPhoneUtils,
});
}

if (isConciergeChatReport(report)) {
Expand Down
18 changes: 12 additions & 6 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3391,7 +3391,13 @@ const customCollator = new Intl.Collator('en', {usage: 'sort', sensitivity: 'var
* @deprecated Moved to src/libs/ReportNameUtils.ts.
* Use ReportNameUtils.getGroupChatName instead.
*/
function getGroupChatName(participants?: SelectedParticipant[], shouldApplyLimit = false, report?: OnyxEntry<Report>, reportMetadataParam?: OnyxEntry<ReportMetadata>): string | undefined {
function getGroupChatName(
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
participants?: SelectedParticipant[],
shouldApplyLimit = false,
report?: OnyxEntry<Report>,
reportMetadataParam?: OnyxEntry<ReportMetadata>,
): string | undefined {
// If we have a report always try to get the name from the report.
if (report?.reportName) {
return report.reportName;
Expand All @@ -3417,8 +3423,8 @@ function getGroupChatName(participants?: SelectedParticipant[], shouldApplyLimit
return participantAccountIDs
.map(
(participantAccountID, index) =>
getDisplayNameForParticipant({accountID: participantAccountID, shouldUseShortForm: isMultipleParticipantReport, formatPhoneNumber: formatPhoneNumberPhoneUtils}) ||
formatPhoneNumberPhoneUtils(participants?.[index]?.login ?? ''),
getDisplayNameForParticipant({accountID: participantAccountID, shouldUseShortForm: isMultipleParticipantReport, formatPhoneNumber}) ||
formatPhoneNumber(participants?.[index]?.login ?? ''),
)
.sort((first, second) => customCollator.compare(first ?? '', second ?? ''))
.filter(Boolean)
Expand All @@ -3428,7 +3434,7 @@ function getGroupChatName(participants?: SelectedParticipant[], shouldApplyLimit
}
// eslint-disable-next-line @typescript-eslint/no-deprecated
return translateLocal('groupChat.defaultReportName', {
displayName: getDisplayNameForParticipant({accountID: participantAccountIDs.at(0), formatPhoneNumber: formatPhoneNumberPhoneUtils}),
displayName: getDisplayNameForParticipant({accountID: participantAccountIDs.at(0), formatPhoneNumber}),
});
}

Expand Down Expand Up @@ -3653,7 +3659,7 @@ function getIconsForGroupChat(report: OnyxInputOrEntry<Report>): Icon[] {
type: CONST.ICON_TYPE_AVATAR,
// This will be fixed as follow up https://github.com/Expensify/App/pull/75357
// eslint-disable-next-line @typescript-eslint/no-deprecated
name: getGroupChatName(undefined, true, report),
name: getGroupChatName(formatPhoneNumberPhoneUtils, undefined, true, report),
};
return [groupChatIcon];
}
Expand Down Expand Up @@ -5995,7 +6001,7 @@ function getReportName(
if (isGroupChat(report)) {
// This will be fixed as follow up https://github.com/Expensify/App/pull/75357
// eslint-disable-next-line @typescript-eslint/no-deprecated
return getGroupChatName(undefined, true, report) ?? '';
return getGroupChatName(formatPhoneNumberPhoneUtils, undefined, true, report) ?? '';
}

if (isChatRoom(report)) {
Expand Down
10 changes: 7 additions & 3 deletions src/pages/GroupChatNameEditPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {NewChatNavigatorParamList} from '@libs/Navigation/types';
import {getGroupChatDraft, getGroupChatName} from '@libs/ReportUtils';
import {getGroupChatName} from '@libs/ReportNameUtils';
import {getGroupChatDraft} from '@libs/ReportUtils';
import StringUtils from '@libs/StringUtils';
import {setGroupDraft, updateChatName} from '@userActions/Report';
import CONST from '@src/CONST';
Expand All @@ -35,10 +36,13 @@ function GroupChatNameEditPage({report}: GroupChatNameEditPageProps) {
const [groupChatDraft = getGroupChatDraft()] = useOnyx(ONYXKEYS.NEW_GROUP_CHAT_DRAFT, {canBeMissing: true});

const styles = useThemeStyles();
const {translate} = useLocalize();
const {translate, formatPhoneNumber} = useLocalize();
const {inputCallbackRef} = useAutoFocusInput();

const existingReportName = useMemo(() => (report ? getGroupChatName(undefined, false, report) : getGroupChatName(groupChatDraft?.participants)), [groupChatDraft?.participants, report]);
const existingReportName = useMemo(
() => (report ? getGroupChatName(formatPhoneNumber, undefined, false, report) : getGroupChatName(formatPhoneNumber, groupChatDraft?.participants)),
[formatPhoneNumber, groupChatDraft?.participants, report],
);
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const currentChatName = reportID ? existingReportName : groupChatDraft?.reportName || existingReportName;

Expand Down
5 changes: 3 additions & 2 deletions src/pages/InviteReportParticipantsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import type {ParticipantsNavigatorParamList} from '@libs/Navigation/types';
import {getHeaderMessage} from '@libs/OptionsListUtils';
import {getLoginsByAccountIDs} from '@libs/PersonalDetailsUtils';
import {addSMSDomainIfPhoneNumber, parsePhoneNumber} from '@libs/PhoneNumber';
import {getGroupChatName} from '@libs/ReportNameUtils';
import type {OptionData} from '@libs/ReportUtils';
import {getGroupChatName, getParticipantsAccountIDsForDisplay} from '@libs/ReportUtils';
import {getParticipantsAccountIDsForDisplay} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand Down Expand Up @@ -123,7 +124,7 @@ function InviteReportParticipantsPage({report}: InviteReportParticipantsPageProp
const validate = useCallback(() => selectedOptions.length > 0, [selectedOptions.length]);

const reportID = report.reportID;
const reportName = useMemo(() => getGroupChatName(undefined, true, report), [report]);
const reportName = useMemo(() => getGroupChatName(formatPhoneNumber, undefined, true, report), [formatPhoneNumber, report]);

const goBack = useCallback(() => {
Navigation.goBack(ROUTES.REPORT_PARTICIPANTS.getRoute(reportID, route.params.backTo));
Expand Down
7 changes: 4 additions & 3 deletions src/pages/NewChatConfirmPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import type {CustomRNImageManipulatorResult} from '@libs/cropOrRotateImage/types
import {readFileAsync} from '@libs/fileDownload/FileUtils';
import Navigation from '@libs/Navigation/Navigation';
import {getParticipantsOption} from '@libs/OptionsListUtils';
import {generateReportID, getDefaultGroupAvatar, getGroupChatName} from '@libs/ReportUtils';
import {getGroupChatName} from '@libs/ReportNameUtils';
import {generateReportID, getDefaultGroupAvatar} from '@libs/ReportUtils';
import {navigateToAndOpenReport, setGroupDraft} from '@userActions/Report';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand All @@ -37,7 +38,7 @@ function navigateToEditChatName() {
function NewChatConfirmPage() {
const optimisticReportID = useRef<string>(generateReportID());
const [avatarFile, setAvatarFile] = useState<File | CustomRNImageManipulatorResult | undefined>();
const {translate, localeCompare} = useLocalize();
const {translate, localeCompare, formatPhoneNumber} = useLocalize();
const styles = useThemeStyles();
const personalData = useCurrentUserPersonalDetails();
const [newGroupDraft, newGroupDraftMetaData] = useOnyx(ONYXKEYS.NEW_GROUP_CHAT_DRAFT, {canBeMissing: true});
Expand All @@ -54,7 +55,7 @@ function NewChatConfirmPage() {
return options;
}, [allPersonalDetails, newGroupDraft?.participants]);

const groupName = newGroupDraft?.reportName ? newGroupDraft?.reportName : getGroupChatName(newGroupDraft?.participants);
const groupName = newGroupDraft?.reportName ? newGroupDraft?.reportName : getGroupChatName(formatPhoneNumber, newGroupDraft?.participants);
const selectedParticipants: ListItem[] = useMemo(
() =>
selectedOptions
Expand Down
21 changes: 11 additions & 10 deletions tests/unit/ReportNameUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetailsList, Policy, Report, ReportAction, ReportActions, ReportAttributesDerivedValue, ReportNameValuePairs} from '@src/types/onyx';
import {createAdminRoom, createPolicyExpenseChat, createRegularChat, createRegularTaskReport, createSelfDM, createWorkspaceThread} from '../utils/collections/reports';
import {fakePersonalDetails} from '../utils/LHNTestUtils';
import {formatPhoneNumber} from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

describe('ReportNameUtils', () => {
Expand Down Expand Up @@ -375,27 +376,27 @@ describe('ReportNameUtils', () => {
describe('When participantAccountIDs is passed to getGroupChatName', () => {
it('shows all participants when count <= 5 and shouldApplyLimit is false', async () => {
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, fakePersonalDetails);
expect(getGroupChatName(fourParticipants)).toEqual('Four, One, Three, Two');
expect(getGroupChatName(formatPhoneNumber, fourParticipants)).toEqual('Four, One, Three, Two');
});

it('shows all participants when count <= 5 and shouldApplyLimit is true', async () => {
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, fakePersonalDetails);
expect(getGroupChatName(fourParticipants, true)).toEqual('Four, One, Three, Two');
expect(getGroupChatName(formatPhoneNumber, fourParticipants, true)).toEqual('Four, One, Three, Two');
});

it('shows 5 participants with ellipsis when count > 5 and shouldApplyLimit is true', async () => {
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, fakePersonalDetails);
expect(getGroupChatName(eightParticipants, true)).toEqual('Five, Four, One, Three, Two...');
expect(getGroupChatName(formatPhoneNumber, eightParticipants, true)).toEqual('Five, Four, One, Three, Two...');
});

it('shows all participants when count > 5 and shouldApplyLimit is false', async () => {
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, fakePersonalDetails);
expect(getGroupChatName(eightParticipants, false)).toEqual('Eight, Five, Four, One, Seven, Six, Three, Two');
expect(getGroupChatName(formatPhoneNumber, eightParticipants, false)).toEqual('Eight, Five, Four, One, Seven, Six, Three, Two');
});

it('uses correct display names for participants', async () => {
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, participantsPersonalDetails);
expect(getGroupChatName(fourParticipants, true)).toEqual('(833) 240-3627, floki@vikings.net, Lagertha, Ragnar');
expect(getGroupChatName(formatPhoneNumber, fourParticipants, true)).toEqual('(833) 240-3627, floki@vikings.net, Lagertha, Ragnar');
});
});

Expand All @@ -408,7 +409,7 @@ describe('ReportNameUtils', () => {
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report);
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, fakePersonalDetails);
expect(getGroupChatName(undefined, false, report)).toEqual("Let's talk");
expect(getGroupChatName(formatPhoneNumber, undefined, false, report)).toEqual("Let's talk");
});

it('uses report name when available (limit true)', async () => {
Expand All @@ -419,7 +420,7 @@ describe('ReportNameUtils', () => {
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report);
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, fakePersonalDetails);
expect(getGroupChatName(undefined, true, report)).toEqual("Let's talk");
expect(getGroupChatName(formatPhoneNumber, undefined, true, report)).toEqual("Let's talk");
});

it('uses report name when >5 participants and limit true', async () => {
Expand All @@ -430,7 +431,7 @@ describe('ReportNameUtils', () => {
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report);
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, fakePersonalDetails);
expect(getGroupChatName(undefined, true, report)).toEqual("Let's talk");
expect(getGroupChatName(formatPhoneNumber, undefined, true, report)).toEqual("Let's talk");
});

it('uses report name when >5 participants and limit false', async () => {
Expand All @@ -441,7 +442,7 @@ describe('ReportNameUtils', () => {
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report);
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, fakePersonalDetails);
expect(getGroupChatName(undefined, false, report)).toEqual("Let's talk");
expect(getGroupChatName(formatPhoneNumber, undefined, false, report)).toEqual("Let's talk");
});

it('falls back to participant names when report name is empty', async () => {
Expand All @@ -452,7 +453,7 @@ describe('ReportNameUtils', () => {
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report);
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, fakePersonalDetails);
expect(getGroupChatName(undefined, false, report)).toEqual('Eight, Five, Four, One, Seven, Six, Three, Two');
expect(getGroupChatName(formatPhoneNumber, undefined, false, report)).toEqual('Eight, Five, Four, One, Seven, Six, Three, Two');
});
});
});
Expand Down
Loading
Loading