You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			133 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			TypeScript
		
	
		
		
			
		
	
	
			133 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			TypeScript
		
	
| 
								 
											8 years ago
										 
									 | 
							
								// @ts-ignore
							 | 
						||
| 
								 | 
							
								import Attachments from '../../app/attachments';
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								import { format as formatPhoneNumber } from '../types/PhoneNumber';
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								export interface Contact {
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  name?: Name;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  number?: Array<Phone>;
							 | 
						||
| 
								 | 
							
								  email?: Array<Email>;
							 | 
						||
| 
								 | 
							
								  address?: Array<PostalAddress>;
							 | 
						||
| 
								 | 
							
								  avatar?: Avatar;
							 | 
						||
| 
								 | 
							
								  organization?: string;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								interface Name {
							 | 
						||
| 
								 | 
							
								  givenName?: string;
							 | 
						||
| 
								 | 
							
								  familyName?: string;
							 | 
						||
| 
								 | 
							
								  prefix?: string;
							 | 
						||
| 
								 | 
							
								  suffix?: string;
							 | 
						||
| 
								 | 
							
								  middleName?: string;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  displayName?: string;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export enum ContactType {
							 | 
						||
| 
								 | 
							
								  HOME = 1,
							 | 
						||
| 
								 | 
							
								  MOBILE = 2,
							 | 
						||
| 
								 | 
							
								  WORK = 3,
							 | 
						||
| 
								 | 
							
								  CUSTOM = 4,
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export enum AddressType {
							 | 
						||
| 
								 | 
							
								  HOME = 1,
							 | 
						||
| 
								 | 
							
								  WORK = 2,
							 | 
						||
| 
								 | 
							
								  CUSTOM = 3,
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export interface Phone {
							 | 
						||
| 
								 | 
							
								  value: string;
							 | 
						||
| 
								 | 
							
								  type: ContactType;
							 | 
						||
| 
								 | 
							
								  label?: string;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export interface Email {
							 | 
						||
| 
								 | 
							
								  value: string;
							 | 
						||
| 
								 | 
							
								  type: ContactType;
							 | 
						||
| 
								 | 
							
								  label?: string;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export interface PostalAddress {
							 | 
						||
| 
								 | 
							
								  type: AddressType;
							 | 
						||
| 
								 | 
							
								  label?: string;
							 | 
						||
| 
								 | 
							
								  street?: string;
							 | 
						||
| 
								 | 
							
								  pobox?: string;
							 | 
						||
| 
								 | 
							
								  neighborhood?: string;
							 | 
						||
| 
								 | 
							
								  city?: string;
							 | 
						||
| 
								 | 
							
								  region?: string;
							 | 
						||
| 
								 | 
							
								  postcode?: string;
							 | 
						||
| 
								 | 
							
								  country?: string;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								interface Avatar {
							 | 
						||
| 
								 | 
							
								  avatar: Attachment;
							 | 
						||
| 
								 | 
							
								  isProfile: boolean;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								interface Attachment {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  path?: string;
							 | 
						||
| 
								 | 
							
								  error?: boolean;
							 | 
						||
| 
								 | 
							
								  pending?: boolean;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export function contactSelector(
							 | 
						||
| 
								 | 
							
								  contact: Contact,
							 | 
						||
| 
								 | 
							
								  options: {
							 | 
						||
| 
								 | 
							
								    regionCode: string;
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    hasSignalAccount: boolean;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    getAbsoluteAttachmentPath: (path: string) => string;
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    onSendMessage: () => void;
							 | 
						||
| 
								 | 
							
								    onClick: () => void;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								) {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  const {
							 | 
						||
| 
								 | 
							
								    getAbsoluteAttachmentPath,
							 | 
						||
| 
								 | 
							
								    hasSignalAccount,
							 | 
						||
| 
								 | 
							
								    onClick,
							 | 
						||
| 
								 | 
							
								    onSendMessage,
							 | 
						||
| 
								 | 
							
								    regionCode,
							 | 
						||
| 
								 | 
							
								  } = options;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  let { avatar } = contact;
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  if (avatar && avatar.avatar) {
							 | 
						||
| 
								 | 
							
								    if (avatar.avatar.error) {
							 | 
						||
| 
								 | 
							
								      avatar = undefined;
							 | 
						||
| 
								 | 
							
								    } else {
							 | 
						||
| 
								 | 
							
								      avatar = {
							 | 
						||
| 
								 | 
							
								        ...avatar,
							 | 
						||
| 
								 | 
							
								        avatar: {
							 | 
						||
| 
								 | 
							
								          ...avatar.avatar,
							 | 
						||
| 
								 | 
							
								          path: avatar.avatar.path
							 | 
						||
| 
								 | 
							
								            ? getAbsoluteAttachmentPath(avatar.avatar.path)
							 | 
						||
| 
								 | 
							
								            : undefined,
							 | 
						||
| 
								 | 
							
								        },
							 | 
						||
| 
								 | 
							
								      };
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  return {
							 | 
						||
| 
								 | 
							
								    ...contact,
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    hasSignalAccount,
							 | 
						||
| 
								 | 
							
								    onSendMessage,
							 | 
						||
| 
								 | 
							
								    onClick,
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    avatar,
							 | 
						||
| 
								 | 
							
								    number:
							 | 
						||
| 
								 | 
							
								      contact.number &&
							 | 
						||
| 
								 | 
							
								      contact.number.map(item => ({
							 | 
						||
| 
								 | 
							
								        ...item,
							 | 
						||
| 
								 | 
							
								        value: formatPhoneNumber(item.value, {
							 | 
						||
| 
								 | 
							
								          ourRegionCode: regionCode,
							 | 
						||
| 
								 | 
							
								        }),
							 | 
						||
| 
								 | 
							
								      })),
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  };
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								export function getName(contact: Contact): string | undefined {
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  const { name, organization } = contact;
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  const displayName = (name && name.displayName) || undefined;
							 | 
						||
| 
								 | 
							
								  const givenName = (name && name.givenName) || undefined;
							 | 
						||
| 
								 | 
							
								  const familyName = (name && name.familyName) || undefined;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  const backupName =
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    (givenName && familyName && `${givenName} ${familyName}`) || undefined;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  return displayName || organization || backupName || givenName || familyName;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 |