File size: 2,720 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {
	getUrlParts,
	getUrlFromParts,
	determineUrlType,
	URL_TYPE,
	format as formatUrl,
	safeImageUrl,
} from '@automattic/calypso-url';
import clsx from 'clsx';
import { Component } from 'react';

import './style.scss';

type Props = {
	className?: string;
	user?: {
		display_name?: string;
		name?: string;
		avatar_URL?: string;
	} | null;
	size?: number;
	imgSize?: number;
	// connected props:
	tempImage?:
		| string // the temp image base64 string if it exists
		| false; // or false if the temp image does not exist
	alt?: string;
	title?: string;
	role?: 'presentation' | 'img' | 'button' | 'link';
};

export class Gravatar extends Component< Props > {
	static defaultProps = {
		// The REST-API returns s=96 by default, so that is most likely to be cached
		imgSize: 96,
		size: 32,
	};

	state = { failedToLoad: false };

	getResizedImageURL( imageURL: string | null ) {
		const { imgSize } = this.props;
		const defaultUrl = 'https://www.gravatar.com/avatar/0';
		imageURL = imageURL || defaultUrl;
		const urlType = determineUrlType( imageURL );

		if ( urlType === URL_TYPE.INVALID || urlType === URL_TYPE.PATH_RELATIVE ) {
			return defaultUrl;
		}

		const { search, origin, host, ...parsedURL } = getUrlParts( imageURL );

		if ( /^([-a-zA-Z0-9_]+\.)*(gravatar.com)$/.test( parsedURL.hostname ) ) {
			parsedURL.searchParams.set( 's', imgSize?.toString() ?? '32' );
			parsedURL.searchParams.set( 'd', 'mm' );
		} else {
			// assume photon
			parsedURL.searchParams.set( 'resize', `${ imgSize },${ imgSize }` );
		}

		// getUrlFromParts can only handle absolute URLs, so add dummy data if needed.
		// formatUrl will remove it away, to match the previous url type.
		parsedURL.protocol = parsedURL.protocol || 'https:';
		parsedURL.hostname = parsedURL.hostname || '__domain__.invalid';
		return formatUrl( getUrlFromParts( parsedURL ), urlType );
	}

	onError = () => this.setState( { failedToLoad: true } );

	render() {
		const { alt, title, size, tempImage, user, role } = this.props;

		if ( ! user ) {
			return (
				<span
					className="gravatar is-placeholder"
					style={ { width: size, height: size } }
					role={ role }
				/>
			);
		}

		if ( this.state.failedToLoad && ! tempImage ) {
			return <span className="gravatar is-missing" role={ role } />;
		}

		const altText = alt || user.display_name || user.name;
		const avatarURL = tempImage || this.getResizedImageURL( safeImageUrl( user.avatar_URL ) );
		const classes = clsx( 'gravatar', this.props.className );

		return (
			<img
				alt={ altText }
				title={ title }
				className={ classes }
				src={ avatarURL }
				width={ size }
				height={ size }
				onError={ this.onError }
				role={ role }
			/>
		);
	}
}