File size: 3,063 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
import styled from '@emotion/styled';
import { Modal } from '@wordpress/components';
import { useI18n } from '@wordpress/react-i18n';
import { translate } from 'i18n-calypso';
import { useState } from 'react';
import { ScreenShotsTimeLine } from 'calypso/data/site-profiler/types';

const Container = styled.div`
	width: 100%;
	box-sizing: border-box;
	border: 1px solid var( --studio-gray-5 );
	padding: 24px;
	border-radius: 4px;
	overflow: hidden;
	background: var( --studio-white );
`;

const Timeline = styled.div`
	width: 100%;
	display: flex;
	flex-direction: row;
	gap: 1.5rem;
	text-align: center;
	overflow: auto;
	padding: 0 2px;
`;

const H2 = styled.h2`
	font-weight: 500;
	font-size: 1rem;
	margin-bottom: 8px;
`;

const Thumbnail = styled.img`
	border: 1px solid var( --studio-gray-0 );
	border-radius: 6px;
	width: 100%;
	min-width: 60px;
	cursor: pointer;
`;

const Tick = styled.p`
	margin: 6px 0 0;
	color: var( --studio-gray-80 );
	font-size: 0.875rem;
`;
const Img = styled.img`
	width: 100%;
	height: auto;
`;

const ScreenshotModal = styled( Modal )`
	@media ( min-width: 600px ) {
		max-height: initial;

		.components-modal__content {
			padding: 0;
			margin-top: 0;
		}

		.components-modal__header {
			button {
				position: relative;
			}

			button::before,
			button::after {
				content: '';
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				border-radius: 50%;
				background-color: black;
				mix-blend-mode: screen;
			}

			button::before {
				background-color: white;
				mix-blend-mode: difference;
			}

			button::after {
				background-color: black;
				mix-blend-mode: screen;
			}

			button svg {
				fill: white;
				width: 16px;
				height: 16px;
				mix-blend-mode: difference;
			}
		}
	}
`;

type Props = { screenshots: ScreenShotsTimeLine[] };

type OverlayState = {
	isOpen: boolean;
	screenshot?: ScreenShotsTimeLine;
	timing?: string;
};

export const ScreenshotTimeline = ( { screenshots }: Props ) => {
	const { __ } = useI18n();
	const [ overlay, setOverlay ] = useState< OverlayState >( {
		isOpen: false,
	} );
	if ( ! screenshots || ! screenshots.length ) {
		return null;
	}

	return (
		<Container>
			<H2>{ translate( 'Timeline' ) }</H2>
			<p>{ translate( 'How your site appears to users while loading.' ) }</p>
			{ overlay.isOpen && overlay.screenshot && (
				<ScreenshotModal
					onRequestClose={ () => setOverlay( { isOpen: false } ) }
					contentLabel={ __( 'Screenshot preview' ) }
				>
					<Img alt={ overlay.timing } src={ overlay.screenshot.data } />
				</ScreenshotModal>
			) }
			<Timeline>
				{ screenshots.map( ( screenshot, index ) => {
					const timing = `${ ( screenshot.timing / 1000 ).toFixed( 1 ) }s`;
					return (
						<div key={ index }>
							<Thumbnail
								alt={ timing }
								src={ screenshot.data }
								onClick={ () =>
									setOverlay( { isOpen: true, screenshot: screenshot, timing: timing } )
								}
							/>
							<Tick>{ timing }</Tick>
						</div>
					);
				} ) }
			</Timeline>
		</Container>
	);
};