import * as React from 'react'; import Avatar from '../avatar'; import DatalistItem from '../datalist-item'; import { ContactType } from '../../features/unified-share-modal/types'; import { SuggestedPillType } from '../pill-selector-dropdown/types'; import './ContactDatalistItem.scss'; export interface ContactDatalistItemProps { getContactAvatarUrl?: (contact: ContactType) => string | Promise; getPillImageUrl?: (data: SuggestedPillType) => string | Promise; id?: string; isExternal?: boolean; name: string | null | undefined; showAvatar?: boolean; subtitle?: React.ReactNode; type?: string | null | undefined; } interface ContactDatalistItemState { avatarUrl: string | null | undefined; } class ContactDatalistItem extends React.PureComponent { constructor(props: ContactDatalistItemProps) { super(props); this.state = { avatarUrl: undefined }; } isMounted = false; /** * Success handler for getting avatar url * * @param {string} [avatarUrl] the user avatar url */ getAvatarUrlHandler = (avatarUrl: string | null | undefined) => { if (this.isMounted) { this.setState({ avatarUrl, }); } }; /** * Gets the avatar URL for the user from the getContactAvatarUrl prop * * @return {void} */ getAvatarUrl() { const { getContactAvatarUrl, id, type } = this.props; Promise.resolve( getContactAvatarUrl && id ? getContactAvatarUrl({ id, type, }) : undefined, ) .then(this.getAvatarUrlHandler) .catch(() => { // noop }); } componentDidMount() { this.isMounted = true; this.getAvatarUrl(); } componentWillUnmount() { this.isMounted = false; } render() { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { getContactAvatarUrl, id, isExternal, name, showAvatar, subtitle, ...rest } = this.props; const { avatarUrl } = this.state; return ( {showAvatar && ( )}
{name}
{subtitle &&
{subtitle}
}
); } } export default ContactDatalistItem;