File size: 3,844 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { DEFAULT_THUMBNAIL_SIZE } from '@automattic/components';
import styled from '@emotion/styled';
import { addQueryArgs } from '@wordpress/url';
import type { SitesDisplayMode } from './sites-display-mode-switcher';
import type { SiteExcerptData } from '@automattic/sites';

const LARGE_ICON_PX = 64;
const SMALL_ICON_PX = 32;
const ICON_BORDER_PX = 3;

const headerImageSize = {
	width: '100%',
	height: 'auto',
	aspectRatio: '5/2',
};

const Container = styled.div( {
	userSelect: 'none',
	position: 'relative',
	top: 0,
	left: 0,
	width: '100%',
	height: '100%',
	overflow: 'hidden',

	// Using flex to remove an unexplained gap between the header image and site icon
	display: 'flex',
	flexDirection: 'column',
	alignItems: 'start',
} );

const HeaderImage = styled.img( {
	...headerImageSize,
	objectFit: 'cover',
} );

const ColorGradient = styled.div( {
	...headerImageSize,

	'::before': {
		content: '""',
		display: 'block',
		width: '100%',
		height: '100%',
		background: 'linear-gradient( 80.15deg, rgba( 0, 0, 0, .3 ) 0%, rgba( 0, 0, 0, 0 ) 100% )',
	},
} );

const SiteIconContainer = styled.div< { isSmall: boolean } >(
	{
		background: '#fff',
		padding: ICON_BORDER_PX,
		borderRadius: '4px',
		position: 'relative',
	},
	( { isSmall } ) =>
		isSmall
			? {
					left: '8px',
					top: `${ -SMALL_ICON_PX / 2 - ICON_BORDER_PX }px`,
			  }
			: {
					left: '32px',
					top: `${ -LARGE_ICON_PX / 2 - ICON_BORDER_PX }px`,
			  }
);

const SiteIcon = styled.img( {
	borderRadius: '2px',
	display: 'block',
} );

interface P2ThumbnailProps {
	site: SiteExcerptData;
	displayMode: SitesDisplayMode;
	alt: string;
	sizesAttr?: string;
}

export function P2Thumbnail( { site, displayMode, alt, sizesAttr }: P2ThumbnailProps ) {
	if ( ! site.p2_thumbnail_elements ) {
		return null;
	}

	const { color_link, color_sidebar_background, header_image } = site.p2_thumbnail_elements;

	function renderContents() {
		const isSmall = displayMode === 'list';

		return (
			<>
				{ header_image ? (
					<HeaderImage { ...getHeaderImgProps( isSmall, header_image ) } sizes={ sizesAttr } />
				) : (
					<ColorGradient style={ { backgroundColor: color_link } } />
				) }
				{ site.icon && (
					<SiteIconContainer isSmall={ isSmall }>
						<SiteIcon { ...getIconImgProps( isSmall, site.icon.img ) } />
					</SiteIconContainer>
				) }
			</>
		);
	}

	return (
		<Container
			role="img"
			aria-label={ alt }
			style={ { backgroundColor: color_sidebar_background } }
		>
			{ renderContents() }
		</Container>
	);
}

function getHeaderImgProps( isSmall: boolean, imgSrc: string ) {
	const param = getWidthParam( imgSrc );

	if ( ! param ) {
		return { src: imgSrc };
	}

	if ( isSmall ) {
		return {
			src: addQueryArgs( imgSrc, { [ param ]: DEFAULT_THUMBNAIL_SIZE.width } ),
			srcSet: addQueryArgs( imgSrc, { [ param ]: 2 * DEFAULT_THUMBNAIL_SIZE.width } ) + ' 2x',
		};
	}

	return {
		src: imgSrc,
		srcSet: [
			addQueryArgs( imgSrc, { [ param ]: 360 } ) + ' 360w',
			addQueryArgs( imgSrc, { [ param ]: 720 } ) + ' 720w', // 720px is the recommended header width in the P2 customizer
		].join( ',' ),
	};
}

function getIconImgProps( isSmall: boolean, imgSrc: string ) {
	const width = isSmall ? SMALL_ICON_PX : LARGE_ICON_PX;

	const param = getWidthParam( imgSrc );

	if ( ! param ) {
		return {
			src: imgSrc,
			width: `${ width }px`,
			height: `${ width }px`,
		};
	}

	return {
		src: addQueryArgs( imgSrc, { [ param ]: width } ),
		srcSet: addQueryArgs( imgSrc, { [ param ]: 2 * width } ) + ' 2x',
		width: `${ width }px`,
		height: `${ width }px`,
	};
}

function getWidthParam( imgSrc: string ) {
	const { hostname } = new URL( imgSrc, 'http://example.com' );
	if ( hostname.endsWith( 'gravatar.com' ) ) {
		return 's';
	}
	if ( hostname.endsWith( 'files.wordpress.com' ) ) {
		return 'w';
	}
	return null;
}