File size: 8,343 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
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
import { CompactCard, Count } from '@automattic/components';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { uniqBy } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import FormInputCheckbox from 'calypso/components/forms/form-checkbox';
import { withLocalizedMoment } from 'calypso/components/localized-moment';
import Notice from 'calypso/components/notice';
import {
	ACTIVATE_PLUGIN,
	DEACTIVATE_PLUGIN,
	DISABLE_AUTOUPDATE_PLUGIN,
	ENABLE_AUTOUPDATE_PLUGIN,
	REMOVE_PLUGIN,
	UPDATE_PLUGIN,
} from 'calypso/lib/plugins/constants';
import PluginActivateToggle from 'calypso/my-sites/plugins/plugin-activate-toggle';
import PluginAutoupdateToggle from 'calypso/my-sites/plugins/plugin-autoupdate-toggle';
import PluginIcon from 'calypso/my-sites/plugins/plugin-icon/plugin-icon';
import { siteObjectsToSiteIds } from 'calypso/my-sites/plugins/utils';
import { getPluginOnSites } from 'calypso/state/plugins/installed/selectors';
import { isMarketplaceProduct } from 'calypso/state/products-list/selectors';

import './style.scss';

class PluginItem extends Component {
	static propTypes = {
		plugin: PropTypes.object,
		sites: PropTypes.array,
		isSelected: PropTypes.bool,
		isSelectable: PropTypes.bool,
		onClick: PropTypes.func,
		pluginLink: PropTypes.string,
		allowedActions: PropTypes.shape( {
			activation: PropTypes.bool,
			autoupdate: PropTypes.bool,
		} ),
		isAutoManaged: PropTypes.bool,
		progress: PropTypes.array,
	};

	static defaultProps = {
		allowedActions: {
			activation: true,
			autoupdate: true,
		},
		progress: [],
		isAutoManaged: false,
	};

	ago( date ) {
		return this.props.moment.utc( date, 'YYYY-MM-DD hh:mma' ).fromNow();
	}

	doing() {
		const { translate, progress } = this.props;
		const log = progress[ 0 ];
		const uniqLogs = uniqBy( progress, function ( uniqLog ) {
			return uniqLog.siteId;
		} );
		const translationArgs = {
			args: { count: uniqLogs.length },
			count: uniqLogs.length,
		};

		let message;
		switch ( log && log.action ) {
			case UPDATE_PLUGIN:
				message = this.props.selectedSite
					? translate( 'Updating', { context: 'plugin' } )
					: translate(
							'Updating on %(count)s site',
							'Updating on %(count)s sites',
							translationArgs
					  );
				break;

			case ACTIVATE_PLUGIN:
				message = this.props.selectedSite
					? translate( 'Activating', { context: 'plugin' } )
					: translate(
							'Activating on %(count)s site',
							'Activating on %(count)s sites',
							translationArgs
					  );
				break;

			case DEACTIVATE_PLUGIN:
				message = this.props.selectedSite
					? translate( 'Deactivating', { context: 'plugin' } )
					: translate(
							'Deactivating on %(count)s site',
							'Deactivating on %(count)s sites',
							translationArgs
					  );
				break;

			case ENABLE_AUTOUPDATE_PLUGIN:
				message = this.props.selectedSite
					? translate( 'Enabling autoupdates' )
					: translate(
							'Enabling autoupdates on %(count)s site',
							'Enabling autoupdates on %(count)s sites',
							translationArgs
					  );
				break;

			case DISABLE_AUTOUPDATE_PLUGIN:
				message = this.props.selectedSite
					? translate( 'Disabling autoupdates' )
					: translate(
							'Disabling autoupdates on %(count)s site',
							'Disabling autoupdates on %(count)s sites',
							translationArgs
					  );

				break;
			case REMOVE_PLUGIN:
				message = this.props.selectedSite
					? translate( 'Removing' )
					: translate(
							'Removing from %(count)s site',
							'Removing from %(count)s sites',
							translationArgs
					  );
		}
		return message;
	}

	renderUpdateFlag() {
		const { pluginsOnSites, sites, translate } = this.props;
		const recentlyUpdated = sites.some( function ( site ) {
			const sitePlugin = pluginsOnSites?.sites[ site.ID ];
			return sitePlugin?.update?.recentlyUpdated;
		} );

		if ( recentlyUpdated ) {
			return (
				<Notice
					isCompact
					icon="checkmark"
					status="is-success"
					inline
					text={ translate( 'Updated' ) }
				/>
			);
		}

		const updated_versions = sites
			.map( ( site ) => {
				const sitePlugin = pluginsOnSites?.sites[ site.ID ];
				return sitePlugin?.update?.new_version;
			} )
			.filter( ( version ) => version );

		return (
			<Notice
				isCompact
				icon="sync"
				inline
				text={ translate( 'Version %(newPluginVersion)s is available', {
					args: { newPluginVersion: updated_versions[ 0 ] },
				} ) }
			/>
		);
	}

	hasUpdate() {
		const { pluginsOnSites, sites } = this.props;

		return sites.some( ( site ) => {
			const sitePlugin = pluginsOnSites?.sites[ site.ID ];
			return sitePlugin?.update && site.canUpdateFiles;
		} );
	}

	pluginMeta( pluginData ) {
		const { progress, translate } = this.props;
		if ( progress.length ) {
			const message = this.doing();
			if ( message ) {
				return <Notice isCompact status="is-info" text={ message } inline />;
			}
		}
		if ( this.props.isAutoManaged ) {
			return (
				<div className="plugin-item__last-updated">
					{ translate( 'Auto-managed on this site' ) }
				</div>
			);
		}

		if ( this.hasUpdate() ) {
			return this.renderUpdateFlag();
		}

		if ( pluginData.last_updated ) {
			return (
				<div className="plugin-item__last-updated">
					{ translate( 'Last updated %(ago)s', {
						args: { ago: this.ago( pluginData.last_updated ) },
					} ) }
				</div>
			);
		}

		return null;
	}

	renderActions() {
		const { activation: canToggleActivation, autoupdate: canToggleAutoupdate } =
			this.props.allowedActions;

		return (
			<div className="plugin-item__actions">
				{ canToggleActivation && (
					<PluginActivateToggle
						plugin={ this.props.plugin }
						disabled={ this.props.isSelectable }
						site={ this.props.selectedSite }
					/>
				) }
				{ canToggleAutoupdate && (
					<PluginAutoupdateToggle
						plugin={ this.props.plugin }
						disabled={ this.props.isSelectable }
						site={ this.props.selectedSite }
						wporg={ !! this.props.plugin.wporg }
						isMarketplaceProduct={ this.props.isMarketplaceProduct }
					/>
				) }
			</div>
		);
	}

	renderSiteCount() {
		const { sites, translate } = this.props;
		return (
			<div className="plugin-item__count">
				{ translate( 'Sites {{count/}}', {
					components: {
						count: <Count count={ sites.length } />,
					},
				} ) }
			</div>
		);
	}

	renderPlaceholder() {
		return (
			// eslint-disable-next-line wpcalypso/jsx-classname-namespace
			<CompactCard className="plugin-item is-placeholder">
				<div className="plugin-item__link">
					<PluginIcon isPlaceholder />
					<div className="plugin-item__info">
						<div className="plugin-item__title is-placeholder" />
					</div>
				</div>
			</CompactCard>
		);
	}

	onItemClick = ( event ) => {
		if ( this.props.isSelectable ) {
			event.preventDefault();
			this.props.onClick( this );
		}
	};

	render() {
		const plugin = this.props.plugin;

		if ( ! plugin ) {
			return this.renderPlaceholder();
		}

		const pluginTitle = <div className="plugin-item__title">{ plugin.name }</div>;

		let pluginActions = null;
		if ( ! this.props.selectedSite ) {
			pluginActions = this.renderSiteCount();
		} else {
			pluginActions = this.renderActions();
		}

		const pluginItemClasses = clsx( 'plugin-item', 'plugin-item-' + plugin.slug );

		return (
			<CompactCard className={ pluginItemClasses }>
				{ ! this.props.isSelectable ? null : (
					<FormInputCheckbox
						className="plugin-item__checkbox"
						id={ plugin.slug }
						title={ plugin.name }
						onClick={ this.props.onClick }
						checked={ this.props.isSelected }
						readOnly
					/>
				) }
				<a
					className="plugin-item__link"
					href={ this.props.pluginLink }
					onClick={ this.onItemClick }
				>
					<PluginIcon image={ plugin.icon } />
					<div className="plugin-item__info">
						{ pluginTitle }
						{ this.pluginMeta( plugin ) }
					</div>
				</a>
				{ pluginActions }
			</CompactCard>
		);
	}
}

export default connect( ( state, { plugin, sites } ) => {
	const siteIds = siteObjectsToSiteIds( sites );

	return {
		pluginsOnSites: getPluginOnSites( state, siteIds, plugin?.slug ),
		isMarketplaceProduct: isMarketplaceProduct( state, plugin?.slug ),
	};
} )( localize( withLocalizedMoment( PluginItem ) ) );