-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathindex.jsx
403 lines (362 loc) · 11.4 KB
/
index.jsx
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import { Card, Button, Gridicon } from '@automattic/components';
import {
DesignPreviewImage,
PREMIUM_THEME,
ThemeCard,
isDefaultGlobalStylesVariationSlug,
isLockedStyleVariation,
} from '@automattic/design-picker';
import { localize } from 'i18n-calypso';
import { isEmpty, isEqual } from 'lodash';
import photon from 'photon';
import PropTypes from 'prop-types';
import { Component, createRef } from 'react';
import { connect } from 'react-redux';
import ThemeTierBadge from 'calypso/components/theme-tier/theme-tier-badge';
import { decodeEntities } from 'calypso/lib/formatting';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { useSiteGlobalStylesStatus } from 'calypso/state/sites/hooks/use-site-global-styles-status';
import { getSiteSlug } from 'calypso/state/sites/selectors';
import { updateThemes } from 'calypso/state/themes/actions/theme-update';
import { isExternallyManagedTheme as getIsExternallyManagedTheme } from 'calypso/state/themes/selectors';
import { setThemesBookmark } from 'calypso/state/themes/themes-ui/actions';
import ThemeMoreButton from './more-button';
import './style.scss';
const noop = () => {};
export class Theme extends Component {
static propTypes = {
theme: PropTypes.shape( {
// Theme ID (theme-slug)
id: PropTypes.string.isRequired,
// Theme name
name: PropTypes.string.isRequired,
// Theme screenshot URL
screenshot: PropTypes.string,
author: PropTypes.string,
author_uri: PropTypes.string,
demo_uri: PropTypes.string,
stylesheet: PropTypes.string,
taxonomies: PropTypes.object,
update: PropTypes.object,
soft_launched: PropTypes.bool,
isCustomGeneratedTheme: PropTypes.bool,
} ),
// If true, highlight this theme as active
active: PropTypes.bool,
// If true, highlight this theme in a loading state
loading: PropTypes.bool,
// If true, render a placeholder
isPlaceholder: PropTypes.bool,
// URL the screenshot link points to
screenshotClickUrl: PropTypes.string,
// Called when theme screenshot is clicked
onScreenshotClick: PropTypes.func,
// Called when theme style variation is clicked
onStyleVariationClick: PropTypes.func,
// Called when the more button is clicked
onMoreButtonClick: PropTypes.func,
// Called when a more button item is clicked
onMoreButtonItemClick: PropTypes.func,
buttonContents: PropTypes.objectOf(
PropTypes.shape( {
label: PropTypes.string,
header: PropTypes.string,
action: PropTypes.func,
getUrl: PropTypes.func,
} )
),
// Index of theme in results list
index: PropTypes.number,
// Label to show on screenshot hover.
actionLabel: PropTypes.string,
// Translate function,
translate: PropTypes.func,
// Themes bookmark items.
setThemesBookmark: PropTypes.func,
bookmarkRef: PropTypes.oneOfType( [
PropTypes.func,
PropTypes.shape( { current: PropTypes.any } ),
] ),
siteId: PropTypes.number,
isUpdating: PropTypes.bool,
isUpdated: PropTypes.bool,
errorOnUpdate: PropTypes.bool,
softLaunched: PropTypes.bool,
selectedStyleVariation: PropTypes.object,
shouldLimitGlobalStyles: PropTypes.bool,
};
static defaultProps = {
isPlaceholder: false,
buttonContents: {},
onMoreButtonClick: noop,
onMoreButtonItemClick: noop,
actionLabel: '',
active: false,
};
constructor( props ) {
super( props );
this.state = {
isScreenshotLoaded: false,
};
}
prevThemeThumbnailRef = createRef( null );
themeThumbnailRef = createRef( null );
shouldComponentUpdate( nextProps ) {
const themeThumbnailRefUpdated = this.themeThumbnailRef.current !== this.themeThumbnailRef.prev;
if ( themeThumbnailRefUpdated ) {
this.prevThemeThumbnailRef.current = this.themeThumbnailRef.current;
}
return (
nextProps.theme.id !== this.props.theme.id ||
nextProps.active !== this.props.active ||
nextProps.loading !== this.props.loading ||
! isEqual(
Object.keys( nextProps.buttonContents ),
Object.keys( this.props.buttonContents )
) ||
nextProps.screenshotClickUrl !== this.props.screenshotClickUrl ||
nextProps.onScreenshotClick !== this.props.onScreenshotClick ||
nextProps.onStyleVariationClick !== this.props.onStyleVariationClick ||
nextProps.onMoreButtonClick !== this.props.onMoreButtonClick ||
nextProps.onMoreButtonItemClick !== this.props.onMoreButtonItemClick ||
themeThumbnailRefUpdated
);
}
onScreenshotClick = () => {
const { onScreenshotClick } = this.props;
if ( typeof onScreenshotClick === 'function' ) {
onScreenshotClick( this.props.theme.id, this.props.index );
}
};
onStyleVariationClick = ( variation ) => {
this.props.onStyleVariationClick?.( this.props.theme.id, this.props.index, variation );
};
renderPlaceholder() {
/* eslint-disable wpcalypso/jsx-classname-namespace */
return (
<Card className="theme theme-card is-placeholder">
<div className="theme__content" />
</Card>
);
/* eslint-enable wpcalypso/jsx-classname-namespace */
}
renderScreenshot() {
const { isExternallyManagedTheme, selectedStyleVariation, theme, siteSlug, translate } =
this.props;
const { isScreenshotLoaded } = this.state;
const { description, screenshot } = theme;
if ( theme.isCustomGeneratedTheme ) {
return (
<iframe
scrolling="no"
loading="lazy"
title={ translate( 'Custom Theme Preview' ) }
className="theme__site-preview"
src={ `//${ siteSlug }/?hide_banners=true&preview_overlay=true&preview=true&cys-hide-admin-bar=1` }
/>
);
}
if ( ! screenshot ) {
return (
<div className="theme__no-screenshot">
<Gridicon icon="themes" size={ 48 } />
</div>
);
}
// mShots don't work well with SSR, since it shows a placeholder image by default
// the snapshot request is completed.
//
// With that in mind, we only use mShots for non-default style variations to ensure
// that there is no flash of image transition from static image to mShots on page load.
if (
! isDefaultGlobalStylesVariationSlug( selectedStyleVariation?.slug ) &&
! isExternallyManagedTheme
) {
const { id: themeId, stylesheet } = theme;
return (
<DesignPreviewImage
design={ { slug: themeId, recipe: { stylesheet } } }
styleVariation={ selectedStyleVariation }
/>
);
}
const fit = '479,360';
const themeImgSrc = photon( screenshot, { fit } ) || screenshot;
const themeImgSrcDoubleDpi = photon( screenshot, { fit, zoom: 2 } ) || screenshot;
return (
<img
loading="lazy"
alt={ isScreenshotLoaded ? decodeEntities( description ) : '' }
className="theme__img"
src={ themeImgSrc }
srcSet={ `${ themeImgSrcDoubleDpi } 2x` }
onLoad={ () => {
this.setState( { isScreenshotLoaded: true } );
} }
/>
);
}
onUpsellClick = () => {
this.props.recordTracksEvent( 'calypso_upgrade_nudge_cta_click', {
cta_name: 'theme-upsell-popup',
theme: this.props.theme.id,
} );
};
setBookmark = () => {
this.props.setThemesBookmark( this.props.theme.id );
};
updateTheme = () => {
this.props.updateThemes( [ this.props.theme.id ], this.props.siteId );
};
renderUpdateAlert = () => {
const { isUpdated, isUpdating, errorOnUpdate, theme, translate } = this.props;
if ( ! theme.update && ! isUpdated && ! isUpdating && ! errorOnUpdate ) {
return;
}
let content;
let alertType;
if ( errorOnUpdate ) {
alertType = 'danger';
content = (
<div>
<span>
<Gridicon icon="cross" size={ 18 } />
{ translate( 'Failed to update Theme.' ) }
</span>
</div>
);
} else if ( isUpdated ) {
alertType = 'success';
content = (
<div>
<span>
<Gridicon icon="checkmark" size={ 18 } />
{ translate( 'Theme updated!' ) }
</span>
</div>
);
} else if ( isUpdating ) {
alertType = 'info';
content = (
<div>
<span>
<Gridicon className="theme__updating-animated" icon="refresh" size={ 18 } />
{ translate( 'Updating theme.' ) }
</span>
</div>
);
} else if ( theme.update ) {
alertType = 'warning';
content = (
<div>
<span>
<Gridicon icon="refresh" size={ 18 } />
{ translate( 'New version available.' ) }
</span>
<Button onClick={ this.updateTheme } primary className="theme__button-link" borderless>
{ translate( 'Update now' ) }
</Button>
</div>
);
}
return (
<div className="theme__update-alert">
<div className={ `${ alertType } theme__update-alert-content` }>{ content }</div>
</div>
);
};
renderMoreButton = () => {
const { active, buttonContents, index, theme, siteId } = this.props;
let moreOptions;
if ( active && buttonContents.info ) {
moreOptions = { info: buttonContents.info };
} else if ( buttonContents.deleteTheme ) {
moreOptions = { deleteTheme: buttonContents.deleteTheme };
} else {
moreOptions = {};
}
if ( isEmpty( moreOptions ) ) {
return null;
}
return (
<ThemeMoreButton
index={ index }
siteId={ siteId }
themeId={ theme.id }
themeName={ theme.name }
hasStyleVariations={ !! theme?.style_variations?.length }
active={ active }
onMoreButtonClick={ this.props.onMoreButtonClick }
onMoreButtonItemClick={ this.props.onMoreButtonItemClick }
options={ moreOptions }
/>
);
};
renderBadge = () => {
const { selectedStyleVariation, shouldLimitGlobalStyles, theme, siteId, siteSlug } = this.props;
const isPremiumTheme = theme.theme_tier?.slug === PREMIUM_THEME;
const isLocked = isLockedStyleVariation( {
isPremiumTheme,
styleVariationSlug: selectedStyleVariation?.slug,
shouldLimitGlobalStyles,
} );
return (
<ThemeTierBadge
siteId={ siteId }
siteSlug={ siteSlug }
themeId={ theme.id }
isLockedStyleVariation={ isLocked }
isThemeList
/>
);
};
render() {
const { selectedStyleVariation, theme } = this.props;
const { name, style_variations = [] } = theme;
if ( this.props.isPlaceholder ) {
return this.renderPlaceholder();
}
return (
<ThemeCard
ref={ this.props.bookmarkRef }
name={ name }
image={ this.renderScreenshot() }
imageClickUrl={ this.props.screenshotClickUrl }
imageActionLabel={ this.props.actionLabel }
banner={ this.renderUpdateAlert() }
badge={ this.renderBadge() }
styleVariations={ style_variations }
selectedStyleVariation={ selectedStyleVariation }
optionsMenu={ this.renderMoreButton() }
isActive={ this.props.active }
isLoading={ this.props.loading }
isSoftLaunched={ this.props.softLaunched }
onClick={ this.setBookmark }
onImageClick={ this.onScreenshotClick }
onStyleVariationClick={ this.onStyleVariationClick }
onStyleVariationMoreClick={ this.onStyleVariationClick }
/>
);
}
}
const ConnectedTheme = connect(
( state, { theme, siteId } ) => {
const {
themes: { themesUpdate },
} = state;
const { themesUpdateFailed, themesUpdating, themesUpdated } = themesUpdate;
const isExternallyManagedTheme = getIsExternallyManagedTheme( state, theme.id );
return {
errorOnUpdate: themesUpdateFailed && themesUpdateFailed.indexOf( theme.id ) > -1,
isUpdating: themesUpdating && themesUpdating.indexOf( theme.id ) > -1,
isUpdated: themesUpdated && themesUpdated.indexOf( theme.id ) > -1,
isExternallyManagedTheme,
siteSlug: getSiteSlug( state, siteId ),
};
},
{ recordTracksEvent, setThemesBookmark, updateThemes }
)( localize( Theme ) );
export default ( props ) => {
const { shouldLimitGlobalStyles } = useSiteGlobalStylesStatus( props.siteId );
return <ConnectedTheme { ...props } shouldLimitGlobalStyles={ shouldLimitGlobalStyles } />;
};