-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.tsx
333 lines (320 loc) · 10.2 KB
/
index.tsx
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
import { Button, Gridicon, Spinner } from '@automattic/components';
import { DataViews } from '@wordpress/dataviews';
import { Icon, starFilled } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import { useCallback, useContext, useMemo } from 'react';
import ReactDOM from 'react-dom';
import SitesDashboardContext from 'calypso/a8c-for-agencies/sections/sites/sites-dashboard-context';
import SiteActions from 'calypso/jetpack-cloud/sections/agency-dashboard/sites-overview/site-actions';
import useFormattedSites from 'calypso/jetpack-cloud/sections/agency-dashboard/sites-overview/site-content/hooks/use-formatted-sites';
import SiteStatusContent from 'calypso/jetpack-cloud/sections/agency-dashboard/sites-overview/site-status-content';
import SiteDataField from 'calypso/jetpack-cloud/sections/agency-dashboard/sites-overview/sites-dataviews/site-data-field';
import { JETPACK_MANAGE_ONBOARDING_TOURS_EXAMPLE_SITE } from 'calypso/jetpack-cloud/sections/onboarding-tours/constants';
import TextPlaceholder from 'calypso/jetpack-cloud/sections/partner-portal/text-placeholder';
import SiteSetFavorite from '../site-set-favorite';
import SiteSort from '../site-sort';
import { AllowedTypes, Site } from '../types';
import { SitesDataViewsProps, SiteInfo } from './interfaces';
import './style.scss';
const SitesDataViews = ( {
data,
isLoading,
isLargeScreen,
onSitesViewChange,
sitesViewState,
forceTourExampleSite = false,
className,
}: SitesDataViewsProps ) => {
const translate = useTranslate();
const { showOnlyFavorites } = useContext( SitesDashboardContext );
const totalSites = showOnlyFavorites ? data?.totalFavorites || 0 : data?.total || 0;
const sitesPerPage = sitesViewState.perPage > 0 ? sitesViewState.perPage : 20;
const totalPages = Math.ceil( totalSites / sitesPerPage );
const sites = useFormattedSites( data?.sites ?? [] );
const openSitePreviewPane = useCallback(
( site: Site ) => {
onSitesViewChange( {
...sitesViewState,
selectedSite: site,
type: 'list',
} );
},
[ onSitesViewChange, sitesViewState ]
);
const renderField = useCallback(
( column: AllowedTypes, item: SiteInfo ) => {
if ( isLoading ) {
return <TextPlaceholder />;
}
if ( column ) {
return (
<SiteStatusContent
rows={ item }
type={ column }
isLargeScreen={ isLargeScreen }
isFavorite={ item.isFavorite }
siteError={ item.site.error }
/>
);
}
},
[ isLoading, isLargeScreen ]
);
// todo - refactor: extract fields, along actions, to the upper component
const fields = useMemo(
() => [
{
id: 'status',
header: translate( 'Status' ),
getValue: ( { item }: { item: SiteInfo } ) =>
item.site.error || item.scan.status === 'critical',
render: () => {},
type: 'enumeration',
elements: [
{ value: 1, label: translate( 'Needs Attention' ) },
{ value: 2, label: translate( 'Backup Failed' ) },
{ value: 3, label: translate( 'Backup Warning' ) },
{ value: 4, label: translate( 'Threat Found' ) },
{ value: 5, label: translate( 'Site Disconnected' ) },
{ value: 6, label: translate( 'Site Down' ) },
{ value: 7, label: translate( 'Plugins Needing Updates' ) },
],
filterBy: {
operators: [ 'in' ],
},
enableHiding: false,
enableSorting: false,
},
{
id: 'site',
header: (
<>
<SiteSort isSortable columnKey="site">
<span className="sites-dataview__site-header">
{ translate( 'Site' ).toUpperCase() }
</span>
</SiteSort>
</>
),
getValue: ( { item }: { item: SiteInfo } ) => item.site.value.url,
render: ( { item }: { item: SiteInfo } ) => {
if ( isLoading ) {
return <TextPlaceholder />;
}
const site = item.site.value;
return (
<SiteDataField
site={ site }
isLoading={ isLoading }
onSiteTitleClick={ openSitePreviewPane }
/>
);
},
enableHiding: false,
enableSorting: false,
},
{
id: 'stats',
header: <span className="sites-dataview__stats-header">STATS</span>,
getValue: () => '-',
render: ( { item }: { item: SiteInfo } ) => renderField( 'stats', item ),
enableHiding: false,
enableSorting: false,
},
{
id: 'boost',
header: <span className="sites-dataview__boost-header">BOOST</span>,
getValue: ( { item }: { item: SiteInfo } ) => item.boost.status,
render: ( { item }: { item: SiteInfo } ) => renderField( 'boost', item ),
enableHiding: false,
enableSorting: false,
},
{
id: 'backup',
header: <span className="sites-dataview__backup-header">BACKUP</span>,
getValue: () => '-',
render: ( { item }: { item: SiteInfo } ) => renderField( 'backup', item ),
enableHiding: false,
enableSorting: false,
},
{
id: 'monitor',
header: <span className="sites-dataview__monitor-header">MONITOR</span>,
getValue: () => '-',
render: ( { item }: { item: SiteInfo } ) => renderField( 'monitor', item ),
enableHiding: false,
enableSorting: false,
},
{
id: 'scan',
header: <span className="sites-dataview__scan-header">SCAN</span>,
getValue: () => '-',
render: ( { item }: { item: SiteInfo } ) => renderField( 'scan', item ),
enableHiding: false,
enableSorting: false,
},
{
id: 'plugins',
header: <span className="sites-dataview__plugins-header">PLUGINS</span>,
getValue: () => '-',
render: ( { item }: { item: SiteInfo } ) => renderField( 'plugin', item ),
enableHiding: false,
enableSorting: false,
},
{
id: 'favorite',
header: (
<Icon
className="site-table__favorite-icon sites-dataview__favorites-header"
size={ 24 }
icon={ starFilled }
/>
),
getValue: ( { item }: { item: SiteInfo } ) => item.isFavorite,
render: ( { item }: { item: SiteInfo } ) => {
if ( isLoading ) {
return <TextPlaceholder />;
}
return (
<span className="sites-dataviews__favorite-btn-wrapper">
<SiteSetFavorite
isFavorite={ item.isFavorite || false }
siteId={ item.site.value.blog_id }
siteUrl={ item.site.value.url }
/>
</span>
);
},
enableHiding: false,
enableSorting: false,
},
{
id: 'actions',
getValue: ( { item }: { item: SiteInfo } ) => item.isFavorite,
render: ( { item }: { item: SiteInfo } ) => {
if ( isLoading ) {
return <TextPlaceholder />;
}
return (
<div className="sites-dataviews__actions">
<SiteActions
isLargeScreen={ isLargeScreen }
site={ item.site }
siteError={ item.site.error }
/>
<Button
onClick={ () => openSitePreviewPane( item.site.value ) }
className="site-preview__open"
borderless
>
<Gridicon icon="chevron-right" />
</Button>
</div>
);
},
enableHiding: false,
enableSorting: false,
},
],
[ isLoading, isLargeScreen, openSitePreviewPane, renderField, translate ]
);
// Actions: Pause Monitor, Resume Monitor, Custom Notification, Reset Notification
// todo - refactor: extract actions, along fields, to the upper component
// Currently not in use until bulk selections are properly implemented.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const actions = useMemo(
() => [
{
id: 'pause-monitor',
label: translate( 'Pause Monitor' ),
supportsBulk: true,
isEligible( site: SiteInfo ) {
return site.monitor.status === 'active';
},
callback() {
// todo: pause monitor. Param: sites: SiteInfo[]
},
},
{
id: 'resume-monitor',
label: translate( 'Resume Monitor' ),
supportsBulk: true,
isEligible( site: SiteInfo ) {
return site.monitor.status === 'inactive';
},
callback() {
// todo: resume monitor. Param: sites: SiteInfo[]
},
},
{
id: 'custom-notification',
label: translate( 'Custom Notification' ),
supportsBulk: true,
isEligible( site: SiteInfo ) {
return site.monitor.status === 'active';
},
callback() {
// todo: custom notification. Param: sites: SiteInfo[]
},
},
{
id: 'reset-notification',
label: translate( 'Reset Notification' ),
supportsBulk: true,
isEligible( site: SiteInfo ) {
return site.monitor.status === 'active';
},
callback() {
// todo: reset notification. Param: sites: SiteInfo[]
},
},
],
[ translate ]
);
// Until the DataViews package is updated to support the spinner, we need to manually add the (loading) spinner to the table wrapper for now.
const SpinnerWrapper = () => {
return (
<div className="spinner-wrapper">
<Spinner />
</div>
);
};
const dataviewsWrapper = document.getElementsByClassName( 'dataviews-wrapper' )[ 0 ];
if ( dataviewsWrapper ) {
// Remove any existing spinner if present
const existingSpinner = dataviewsWrapper.querySelector( '.spinner-wrapper' );
if ( existingSpinner ) {
existingSpinner.remove();
}
const spinnerWrapper = dataviewsWrapper.appendChild( document.createElement( 'div' ) );
spinnerWrapper.classList.add( 'spinner-wrapper' );
// Render the SpinnerWrapper component inside the spinner wrapper
ReactDOM.hydrate( <SpinnerWrapper />, spinnerWrapper );
//}
}
const urlParams = new URLSearchParams( window.location.search );
const isOnboardingTourActive = urlParams.get( 'tour' ) !== null;
const useExampleDataForTour =
forceTourExampleSite || ( isOnboardingTourActive && ( ! sites || sites.length === 0 ) );
return (
<div className={ className }>
<DataViews
data={ ! useExampleDataForTour ? sites : JETPACK_MANAGE_ONBOARDING_TOURS_EXAMPLE_SITE }
paginationInfo={ { totalItems: totalSites, totalPages: totalPages } }
fields={ fields }
view={ sitesViewState }
search
searchLabel={ translate( 'Search for sites' ) }
getItemId={ ( item: SiteInfo ) => {
item.id = item.site.value.blog_id; // setting the id because of a issue with the DataViews component
return item.id;
} }
onChangeView={ onSitesViewChange }
supportedLayouts={ [ 'table' ] }
actions={ [] } // Replace with actions when bulk selections are implemented.
isLoading={ isLoading }
/>
</div>
);
};
export default SitesDataViews;