File size: 1,430 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
import clsx from 'clsx';
import { some } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import GalleryShortcode from 'calypso/components/gallery-shortcode';
import { generateGalleryShortcode } from 'calypso/lib/media/utils';

export default class EditorMediaModalGalleryPreviewShortcode extends Component {
	static propTypes = {
		siteId: PropTypes.number,
		settings: PropTypes.object,
	};

	state = {
		isLoading: true,
		shortcode: generateGalleryShortcode( this.props.settings ),
	};

	isMounted = false;

	static getDerivedStateFromProps( nextProps, prevState ) {
		const shortcode = generateGalleryShortcode( nextProps.settings );
		if ( prevState.shortcode === shortcode ) {
			return null;
		}

		return { isLoading: true, shortcode };
	}

	componentDidMount() {
		this.isMounted = true;
	}

	componentWillUnmount() {
		this.isMounted = false;
	}

	setLoaded = () => {
		if ( ! this.isMounted ) {
			return;
		}

		this.setState( { isLoading: false } );
	};

	render() {
		const { siteId, settings } = this.props;
		const { isLoading, shortcode } = this.state;
		const classes = clsx( 'editor-media-modal-gallery__preview-shortcode', {
			'is-loading': isLoading || some( settings.items, 'transient' ),
		} );

		return (
			<div className={ classes }>
				<GalleryShortcode siteId={ siteId } onLoad={ this.setLoaded }>
					{ shortcode }
				</GalleryShortcode>
			</div>
		);
	}
}