File size: 1,978 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
import { MShotsImage } from '@automattic/onboarding';
import { useViewportMatch } from '@wordpress/compose';
import photon from 'photon';
import { getDesignPreviewUrl, getMShotOptions } from '../../utils';
import type { Design, StyleVariation } from '../../types';
import type { FC } from 'react';

const makeOptionId = ( { slug }: Design ): string => `design-picker__option-name__${ slug }`;

interface DesignPreviewImageProps {
	design: Design;
	imageOptimizationExperiment?: boolean;
	locale?: string;
	styleVariation?: StyleVariation;
	oldHighResImageLoading?: boolean; // Temporary for A/B test.
}

const DesignPreviewImage: FC< DesignPreviewImageProps > = ( {
	design,
	locale,
	styleVariation,
	oldHighResImageLoading,
} ) => {
	const isMobile = useViewportMatch( 'small', '<' );

	if ( design?.design_tier === 'partner' && design.screenshot ) {
		const fit = '479,360';
		// We're stubbing out the high res version here as part of a size reduction experiment.
		// See #88786 and TODO for discussion / info.
		const themeImgSrc = photon( design.screenshot, { fit } ) || design.screenshot;
		const themeImgSrcDoubleDpi = photon( design.screenshot, { fit, zoom: 2 } ) || design.screenshot;

		if ( oldHighResImageLoading ) {
			return (
				<img
					src={ themeImgSrc }
					srcSet={ `${ themeImgSrcDoubleDpi } 2x` }
					alt={ design.description }
				/>
			);
		}

		return (
			<img
				loading="lazy"
				src={ themeImgSrc }
				srcSet={ `${ themeImgSrc }` }
				alt={ design.description }
			/>
		);
	}

	return (
		<MShotsImage
			url={ getDesignPreviewUrl( design, {
				use_screenshot_overrides: true,
				style_variation: styleVariation,
				...( locale && { language: locale } ),
			} ) }
			aria-labelledby={ makeOptionId( design ) }
			alt=""
			options={ getMShotOptions( {
				scrollable: false,
				highRes: true,
				isMobile: false,
				oldHighResImageLoading: ! isMobile,
			} ) }
			scrollable={ false }
		/>
	);
};

export default DesignPreviewImage;