Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions src/libs/LocalePhoneNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import ONYXKEYS from '@src/ONYXKEYS';
import {parsePhoneNumber} from './PhoneNumber';

let countryCodeByIP: number;
let countryCodeByIPOnyx: number;
Onyx.connect({

Check warning on line 8 in src/libs/LocalePhoneNumber.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.COUNTRY_CODE,
callback: (val) => (countryCodeByIP = val ?? 1),
callback: (val) => (countryCodeByIPOnyx = val ?? 1),
});

/**
Expand Down Expand Up @@ -39,6 +39,43 @@

const regionCode = parsedPhoneNumber.countryCode;

if (regionCode === countryCodeByIPOnyx) {
return parsedPhoneNumber.number.national;
}

return parsedPhoneNumber.number.international;
}

/**
* This is a TEMPORARY function to be used until we have migrated away from using Onyx.Connect
* Returns a locally converted phone number for numbers from the same region
* and an internationally converted phone number with the country code for numbers from other regions
*/
function formatPhoneNumberWithCountryCode(number: string, countryCodeByIP: number): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was returning the email address provided in the param, as it's temporary we didn't modify it but rather swiched the ternary in #72291

if (!number) {
return '';
}

// eslint-disable-next-line no-param-reassign
number = number.replace(/ /g, '\u00A0');

// do not parse the string, if it doesn't contain the SMS domain and it's not a phone number
if (number.indexOf(CONST.SMS.DOMAIN) === -1 && !CONST.REGEX.DIGITS_AND_PLUS.test(number)) {
return number;
}
const numberWithoutSMSDomain = Str.removeSMSDomain(number);
const parsedPhoneNumber = parsePhoneNumber(numberWithoutSMSDomain);

// return the string untouched if it's not a phone number
if (!parsedPhoneNumber.valid) {
if (parsedPhoneNumber.number?.international) {
return parsedPhoneNumber.number.international;
}
return numberWithoutSMSDomain;
}

const regionCode = parsedPhoneNumber.countryCode;

if (regionCode === countryCodeByIP) {
return parsedPhoneNumber.number.national;
}
Expand All @@ -49,4 +86,5 @@
export {
// eslint-disable-next-line import/prefer-default-export
formatPhoneNumber,
formatPhoneNumberWithCountryCode,
};
Loading