Files
twenty/packages/twenty-front/src/modules/ui/field/display/components/PhoneDisplay.tsx
T
Guillim 0527bc296e Default address country 🗺️ & Phone prefix ☎️ (#8614)
# Default address 🗺️ country & Phone ☎️ country

We add the ability to add a Default address country and a default Phone
country for fields in the Data model.

fix #8081

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
2024-12-02 12:34:05 +00:00

49 lines
1.4 KiB
TypeScript

import { parsePhoneNumber, PhoneNumber } from 'libphonenumber-js';
import { MouseEvent } from 'react';
import { ContactLink } from 'twenty-ui';
import { isDefined } from '~/utils/isDefined';
interface PhoneDisplayProps {
value: PhoneDisplayValueProps;
}
type PhoneDisplayValueProps = {
number: string | null | undefined;
callingCode: string | null | undefined;
};
export const PhoneDisplay = ({
value: { number, callingCode },
}: PhoneDisplayProps) => {
if (!isDefined(number)) return <ContactLink href="#">{number}</ContactLink>;
const callingCodeSanitized = callingCode?.replace('+', '');
let parsedPhoneNumber: PhoneNumber | null = null;
try {
parsedPhoneNumber = parsePhoneNumber(number, {
defaultCallingCode: callingCodeSanitized || '1',
});
} catch (error) {
if (!(error instanceof Error))
return <ContactLink href="#">{number}</ContactLink>;
if (error.message === 'NOT_A_NUMBER')
return <ContactLink href="#">{`+${callingCodeSanitized}`}</ContactLink>;
return <ContactLink href="#">{number}</ContactLink>;
}
const URI = parsedPhoneNumber.getURI();
const formatedPhoneNumber = parsedPhoneNumber.formatInternational();
return (
<ContactLink
href={URI}
onClick={(event: MouseEvent<HTMLElement>) => {
event.stopPropagation();
}}
>
{formatedPhoneNumber || number}
</ContactLink>
);
};