File size: 4,432 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
import { Button } from '@wordpress/components';
import { Operator } from '@wordpress/dataviews';
import { translate } from 'i18n-calypso';
import { useMemo } from 'react';
import PluginIcon from 'calypso/my-sites/plugins/plugin-icon/plugin-icon';
import { useDispatch } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { PLUGINS_STATUS } from 'calypso/state/plugins/installed/status/constants';
import { Plugin } from 'calypso/state/plugins/installed/types';
import { PluginActions } from '../hooks/types';
import PluginActionStatus from '../plugin-management-v2/plugin-action-status';

export function useFields(
	bulkActionDialog: ( action: string, plugins: Array< Plugin > ) => void,
	openPluginSitesPane: ( plugin: Plugin ) => void,
	isListView: boolean
) {
	const dispatch = useDispatch();

	const fields = useMemo(
		() => [
			{
				id: 'status',
				label: translate( 'Status' ),
				getValue: ( { item }: { item: Plugin } ) => {
					return item.status;
				},
				render: () => null,
				elements: [
					{
						value: PLUGINS_STATUS.ACTIVE,
						label: translate( 'Active' ),
					},
					{
						value: PLUGINS_STATUS.INACTIVE,
						label: translate( 'Inactive' ),
					},
					{
						value: PLUGINS_STATUS.UPDATE,
						label: translate( 'Update available' ),
					},
				],
				filterBy: {
					operators: [ 'isAny' as Operator ],
					isPrimary: false,
				},
				enableHiding: false,
				enableSorting: false,
			},
			{
				id: 'icon',
				label: translate( 'Icon' ),
				render: ( { item }: { item: Plugin } ) => (
					<PluginIcon className="plugin-icon" image={ item.icon } size={ isListView ? 52 : 35 } />
				),
				enableHiding: false,
				enableSorting: false,
			},
			{
				id: 'plugins',
				label: translate( 'Installed plugins' ),
				getValue: ( { item }: { item: Plugin } ) => item.name,
				enableGlobalSearch: true,
				render: ( { item }: { item: Plugin } ) => {
					let pluginActionStatus = null;

					if ( item.allStatuses?.length ) {
						pluginActionStatus = (
							<PluginActionStatus
								currentSiteStatuses={ item.allStatuses }
								selectedSite={ undefined }
							/>
						);
					}

					return (
						<>
							<div className="plugin-name-container">{ item.name }</div>
							{ pluginActionStatus }
						</>
					);
				},
				enableSorting: true,
			},
			{
				id: 'sites',
				label: translate( 'Sites' ),
				enableHiding: false,
				getValue: ( { item }: { item: Plugin } ) => {
					// Used exclusively for sorting
					return item.sites && Object.keys( item.sites ).length;
				},
				render: ( { item }: { item: Plugin } ) => {
					const numberOfSites = item.sites && Object.keys( item.sites ).length;
					return (
						<Button
							className="sites-manage-plugin-button"
							onClick={ () => {
								dispatch(
									recordTracksEvent( 'calypso_plugins_manage_list_plugin_sitecount_click', {
										plugin_slug: item.slug,
										site_count: numberOfSites,
									} )
								);

								openPluginSitesPane( item );
							} }
						>
							{ numberOfSites }
							{ isListView && ( numberOfSites > 1 ? translate( ' sites' ) : translate( ' site' ) ) }
						</Button>
					);
				},
			},
			{
				id: 'update',
				label: translate( 'Update available' ),
				getValue: ( { item }: { item: Plugin } ) => {
					// Used exclusively for sorting
					return item.status?.includes( PLUGINS_STATUS.UPDATE ) ? 'a' : 'b';
				},
				enableHiding: false,
				render: ( { item }: { item: Plugin } ) => {
					if (
						item.status?.includes( PLUGINS_STATUS.UPDATE ) &&
						item?.update?.new_version &&
						! isListView
					) {
						return (
							<Button
								variant="secondary"
								onClick={ () => {
									dispatch(
										recordTracksEvent( 'calypso_plugins_manage_list_plugin_updateavailable_click', {
											plugin_slug: item.slug,
										} )
									);

									bulkActionDialog( PluginActions.UPDATE, [ item ] );
								} }
							>
								{ translate( 'Update to version %(version)s', {
									args: {
										version: item?.update?.new_version
											? item.update.new_version.split( '-' ).slice( 0, 2 ).join( '-' )
											: '',
									},
								} ) }
							</Button>
						);
					}
				},
			},
		],
		// eslint-disable-next-line react-hooks/exhaustive-deps
		[ bulkActionDialog, openPluginSitesPane, isListView ]
	);

	return fields;
}