-
Notifications
You must be signed in to change notification settings - Fork 2k
/
plugin-item.jsx
387 lines (345 loc) · 8.96 KB
/
plugin-item.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
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import classNames from 'classnames';
import { flowRight as compose, isEqual, uniqBy } from 'lodash';
import { localize, moment } from 'i18n-calypso';
/**
* Internal dependencies
*/
import CompactCard from 'components/card/compact';
import PluginIcon from 'my-sites/plugins/plugin-icon/plugin-icon';
import PluginActivateToggle from 'my-sites/plugins/plugin-activate-toggle';
import PluginAutoupdateToggle from 'my-sites/plugins/plugin-autoupdate-toggle';
import Count from 'components/count';
import Notice from 'components/notice';
import PluginNotices from 'lib/plugins/notices';
import { errorNotice } from 'state/notices/actions';
/**
* Style dependencies
*/
import './style.scss';
function checkPropsChange( nextProps, propArr ) {
let i;
for ( i = 0; i < propArr.length; i++ ) {
const prop = propArr[ i ];
if ( ! isEqual( nextProps[ prop ], this.props[ prop ] ) ) {
return true;
}
}
return false;
}
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,
notices: PropTypes.shape( {
completed: PropTypes.array,
errors: PropTypes.array,
inProgress: PropTypes.array,
} ),
hasAllNoManageSites: PropTypes.bool,
hasUpdate: PropTypes.func,
};
static defaultProps = {
allowedActions: {
activation: true,
autoupdate: true,
},
progress: [],
isAutoManaged: false,
hasUpdate: () => false,
};
shouldComponentUpdate( nextProps ) {
const propsToCheck = [
'plugin',
'sites',
'selectedSite',
'isMock',
'isSelectable',
'isSelected',
];
if ( checkPropsChange.call( this, nextProps, propsToCheck ) ) {
return true;
}
if ( this.props.hasAllNoManageSites !== nextProps.hasAllNoManageSites ) {
return true;
}
if (
this.props.notices &&
PluginNotices.shouldComponentUpdateNotices( this.props.notices, nextProps.notices )
) {
return true;
}
return false;
}
ago( date ) {
return 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.site.ID;
} );
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 { sites, translate } = this.props;
const recentlyUpdated = sites.some( function( site ) {
return site.plugin && site.plugin.update && site.plugin.update.recentlyUpdated;
} );
if ( recentlyUpdated ) {
return (
<Notice
isCompact
icon="checkmark"
status="is-success"
inline={ true }
text={ translate( 'Updated' ) }
/>
);
}
const updated_versions = this.props.plugin.sites
.map( site => {
if ( site.plugin.update && site.plugin.update.new_version ) {
return site.plugin.update.new_version;
}
return false;
} )
.filter( version => version );
return (
<Notice
isCompact
icon="sync"
inline={ true }
text={ translate( 'Version %(newPluginVersion)s is available', {
args: { newPluginVersion: updated_versions[ 0 ] },
} ) }
/>
);
}
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={ true } />;
}
}
if ( this.props.isAutoManaged ) {
return (
<div className="plugin-item__last-updated">
{ translate( 'Auto-managed on this site' ) }
</div>
);
}
if ( this.props.hasUpdate( pluginData ) ) {
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;
}
showNoManageNotice() {
this.props.errorNotice(
this.props.translate(
'Jetpack Manage is disabled for all the sites where this plugin is installed'
),
{ id: 'plugin-no-manage-error' } // Display the notice only once on repeated clicks
);
}
renderActions() {
const {
activation: canToggleActivation,
autoupdate: canToggleAutoupdate,
} = this.props.allowedActions;
return (
<div className="plugin-item__actions">
{ canToggleActivation && (
<PluginActivateToggle
isMock={ this.props.isMock }
plugin={ this.props.plugin }
disabled={ this.props.isSelectable }
site={ this.props.selectedSite }
notices={ this.props.notices }
/>
) }
{ canToggleAutoupdate && (
<PluginAutoupdateToggle
isMock={ this.props.isMock }
plugin={ this.props.plugin }
disabled={ this.props.isSelectable }
site={ this.props.selectedSite }
notices={ this.props.notices }
wporg={ !! this.props.plugin.wporg }
/>
) }
</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.hasAllNoManageSites ) {
event.preventDefault();
this.showNoManageNotice();
} else if ( this.props.isSelectable ) {
event.preventDefault();
this.props.onClick( this );
}
};
render() {
const plugin = this.props.plugin;
if ( ! plugin ) {
return this.renderPlaceholder();
}
const disabled = this.props.hasAllNoManageSites;
const pluginTitle = <div className="plugin-item__title">{ plugin.name }</div>;
let pluginActions = null;
if ( ! this.props.selectedSite ) {
pluginActions = this.renderSiteCount();
} else if ( ! disabled ) {
pluginActions = this.renderActions();
}
const pluginItemClasses = classNames( 'plugin-item', 'plugin-item-' + plugin.slug, {
disabled,
} );
return (
<CompactCard className={ pluginItemClasses }>
{ disabled || ! this.props.isSelectable ? null : (
<input
className="plugin-item__checkbox"
id={ plugin.slug }
type="checkbox"
onClick={ this.props.onClick }
checked={ this.props.isSelected }
readOnly={ true }
/>
) }
<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 compose(
connect(
null,
{ errorNotice }
),
localize
)( PluginItem );