File size: 8,226 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
import { Button } from '@automattic/components';
import { Icon, plugins } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import FormInputCheckbox from 'calypso/components/forms/form-checkbox';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import { INSTALL_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 PluginInstallButton from 'calypso/my-sites/plugins/plugin-install-button';
import UpdatePlugin from 'calypso/my-sites/plugins/plugin-management-v2/update-plugin';
import { useDispatch, useSelector } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { getBillingInterval } from 'calypso/state/marketplace/billing-interval/selectors';
import {
	isPluginActionInProgress,
	getPluginOnSite,
} from 'calypso/state/plugins/installed/selectors';
import { isMarketplaceProduct } from 'calypso/state/products-list/selectors';
import PluginActionStatus from '../plugin-action-status';
import { getAllowedPluginActions } from '../utils/get-allowed-plugin-actions';
import { getPluginActionStatuses } from '../utils/get-plugin-action-statuses';
import type { PluginComponentProps } from '../types';
import type { SiteDetails } from '@automattic/data-stores';
import type { MomentInput } from 'moment';
import type { MouseEventHandler, PropsWithChildren } from 'react';

import './style.scss';

interface Props {
	item: PluginComponentProps;
	columnKey: string;
	selectedSite?: SiteDetails;
	isSmallScreen?: boolean;
	className?: string;
	updatePlugin?: ( plugin: PluginComponentProps ) => void;
	siteCount?: number;
}

export default function PluginRowFormatter( {
	item,
	columnKey,
	selectedSite,
	isSmallScreen,
	className,
	updatePlugin,
}: Props ) {
	const translate = useTranslate();
	const dispatch = useDispatch();

	const billingPeriod = useSelector( getBillingInterval );
	const pluginId = item.id || item.slug; // Plugin ID only available on Site plugin item object

	const PluginDetailsButton = (
		props: PropsWithChildren< { className: string; onClick?: MouseEventHandler } >
	) => {
		return (
			<Button
				borderless
				compact
				href={ `/plugins/${ item.slug }${ selectedSite ? `/${ selectedSite.domain }` : '' }` }
				{ ...props }
			/>
		);
	};

	const trackPluginDetailsButtonClick =
		( siteId: number | undefined, pluginSlug: string ) => () => {
			dispatch(
				recordTracksEvent( 'calypso_plugin_details_click', { site: siteId, plugin: pluginSlug } )
			);
		};

	const trackPluginSiteCountButtonClick =
		( siteId: number | undefined, pluginSlug: string ) => () => {
			dispatch(
				recordTracksEvent( 'calypso_plugin_site_count_click', { site: siteId, plugin: pluginSlug } )
			);
		};

	const moment = useLocalizedMoment();

	const ago = ( date: MomentInput ) => {
		return moment.utc( date, 'YYYY-MM-DD hh:mma' ).fromNow();
	};

	let canActivate;
	let canUpdate;

	const installInProgress = useSelector(
		( state ) =>
			selectedSite && isPluginActionInProgress( state, selectedSite.ID, pluginId, INSTALL_PLUGIN )
	);

	const { activation, autoupdate } = useSelector( ( state ) =>
		getAllowedPluginActions( state, item, selectedSite )
	);

	if ( selectedSite ) {
		canActivate = activation;
		canUpdate = autoupdate;
	}

	const pluginOnSite = useSelector(
		( state ) => selectedSite && getPluginOnSite( state, selectedSite.ID, item.slug )
	);

	const siteCount = item?.sites && Object.keys( item.sites ).length;

	const isFromMarketplace = useSelector( ( state ) => isMarketplaceProduct( state, item?.slug ) );
	const allStatuses = useSelector( ( state ) => getPluginActionStatuses( state ) );

	let currentSiteStatuses = allStatuses.filter(
		( status ) => status.pluginId === pluginId && status.action !== UPDATE_PLUGIN
	);

	if ( 'site-name' === columnKey ) {
		currentSiteStatuses = currentSiteStatuses.filter(
			( status ) => parseInt( status.siteId ) === selectedSite?.ID
		);
	}

	const pluginActionStatus =
		currentSiteStatuses.length > 0 ? (
			<PluginActionStatus
				currentSiteStatuses={ currentSiteStatuses }
				selectedSite={ selectedSite }
			/>
		) : null;

	switch ( columnKey ) {
		case 'site-name':
			return (
				<span className="plugin-row-formatter__row-container">
					<span className="plugin-row-formatter__site-name">{ selectedSite?.domain }</span>
					{ /* Overlay for small screen is added in the card component */ }
					{ ! isSmallScreen && <span className="plugin-row-formatter__overlay"></span> }
					{ pluginActionStatus && (
						<div className="plugin-row-formatter__action-status">{ pluginActionStatus }</div>
					) }
				</span>
			);
		case 'plugin':
			return isSmallScreen ? (
				<>
					<PluginDetailsButton
						className="plugin-row-formatter__plugin-name-card"
						onClick={ trackPluginDetailsButtonClick( selectedSite?.ID, item.slug ) }
					>
						{ item.name }
					</PluginDetailsButton>
					{ pluginActionStatus }
				</>
			) : (
				<span className="plugin-row-formatter__row-container">
					<span className="plugin-row-formatter__plugin-details">
						{ item?.isSelectable && (
							<FormInputCheckbox
								className="plugin-row-formatter__checkbox"
								id={ item.slug }
								onClick={ item.onClick }
								checked={ item.isSelected }
								readOnly
							/>
						) }
						{ item.icon ? (
							<img
								className="plugin-row-formatter__plugin-icon"
								src={ item.icon }
								alt={ item.name }
							/>
						) : (
							<Icon
								size={ 32 }
								icon={ plugins }
								className="plugin-row-formatter__plugin-icon plugin-default-icon"
							/>
						) }
						<div className="plugin-row-formatter__plugin-name-container">
							<PluginDetailsButton
								className="plugin-row-formatter__plugin-name"
								onClick={ trackPluginDetailsButtonClick( selectedSite?.ID, item.slug ) }
							>
								{ item.name }
							</PluginDetailsButton>
							<span className="plugin-row-formatter__overlay"></span>
							{ pluginActionStatus }
						</div>
					</span>
				</span>
			);
		case 'sites':
			return isSmallScreen ? (
				<>
					{ translate(
						'Installed on %(count)d site',
						'Installed on %(count)d sites', // plural version of the string
						{
							count: siteCount,
							args: { count: siteCount },
						}
					) }
				</>
			) : (
				<PluginDetailsButton
					className="plugin-row-formatter__sites-count-button"
					onClick={ trackPluginSiteCountButtonClick( selectedSite?.ID, item.slug ) }
				>
					{ siteCount }
				</PluginDetailsButton>
			);
		case 'activate':
			return canActivate ? (
				<div className="plugin-row-formatter__toggle">
					<PluginActivateToggle
						isJetpackCloud
						plugin={ pluginOnSite }
						site={ selectedSite }
						disabled={ !! item?.isSelectable }
					/>
				</div>
			) : null;
		case 'autoupdate':
			return canUpdate ? (
				<div className="plugin-row-formatter__toggle">
					<PluginAutoupdateToggle
						plugin={ pluginOnSite }
						site={ selectedSite }
						wporg={ !! item.wporg }
						isMarketplaceProduct={ isFromMarketplace }
						disabled={ !! item?.isSelectable }
					/>
				</div>
			) : null;
		case 'last-updated':
			return item?.update && item?.last_updated ? (
				<span className="plugin-row-formatter__last-updated">
					{ translate( '{{span}}Updated{{/span}} %(ago)s', {
						components: {
							span: <span />,
						},
						args: {
							ago: ago( item.last_updated ),
						},
					} ) }
				</span>
			) : null;
		case 'update':
			return (
				<UpdatePlugin
					plugin={ item }
					selectedSite={ selectedSite }
					className={ className }
					updatePlugin={ updatePlugin }
				/>
			);
		case 'install':
			return (
				<div className="plugin-row-formatter__install-plugin">
					<PluginInstallButton
						isEmbed
						isJetpackCloud
						selectedSite={ selectedSite }
						plugin={ item }
						isInstalling={ installInProgress }
						billingPeriod={ billingPeriod }
					/>
				</div>
			);
		case 'bulk-actions':
			return null;
		default:
			return null;
	}
}