Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 388 | 7x 164x 164x 164x 188x 188x 188x 102x 86x 6x 1x 5x 188x 188x 6x 182x 88x 214x 86x 93x 93x 188x 156x 188x 15x 7x 7x 15x 15x 2x 13x 7x 7x 33x 33x 33x 33x 33x 709x 33x 13x 13x 13x 33x 1x 2x 33x 33x 102x 33x 33x 33x 33x 33x 2x 2x 33x 77x 33x | import { getThreatType, ThreatSeverityBadge, type Threat } from '@automattic/jetpack-scan';
import { DataViews, filterSortAndPaginate, type Field, type View } from '@wordpress/dataviews';
import { dateI18n } from '@wordpress/date';
import { useCallback, useEffect, useMemo, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { chevronDown } from '@wordpress/icons';
import { Collapsible, Icon, Spinner, Stack } from '@wordpress/ui';
import useArrowKeyNavigation from '../../components/use-arrow-key-navigation';
import { recordViewChange } from '../../data/threat-events';
import useOpenThreat from '../use-open-threat';
import { getThreatLabel } from './labels';
import { THREAT_PARAM, useSearchParam } from './store';
import { useThreatAction, type ThreatActionState } from './threat-actions';
import ThreatMedia from './threat-media';
import { useThreatRowActions } from './threat-row-actions';
import type { ScanThreat } from './types';
const DEFAULT_LAYOUTS = { table: {} };
/**
* When a threat was found: "Today, 7:45 AM", else "Aug 15, 7:00 AM", in the site's timezone.
*
* @param date - The ISO date.
* @return The formatted date.
*/
function formatDetected( date: string ): string {
const time = dateI18n( 'g:i A', date, undefined );
Iif ( dateI18n( 'Y-m-d', date, undefined ) === dateI18n( 'Y-m-d', new Date(), undefined ) ) {
return sprintf(
/* translators: %s is a time, such as "7:45 AM". */
__( 'Today, %s', 'jetpack-protect-pkg' ),
time
);
}
return dateI18n( 'M j, g:i A', date, undefined );
}
type RowProps = { item: ScanThreat; onOpen: ( item: ScanThreat ) => void; inline?: boolean };
/**
* The threat column: the icon and the threat, as one button that opens the details.
*
* Stacked, the kind of threat sits above what it's in; inline, they share a line with the severity.
*
* @param props - Component props.
* @param props.item - The threat.
* @param props.onOpen - Opens the threat in the inspector.
* @param props.inline - Whether to show the threat on one line, with its severity.
* @return The cell.
*/
function ThreatCell( { item, onOpen, inline = false }: RowProps ) {
const { kind, subject } = getThreatLabel( item );
const onClick = useCallback( () => onOpen( item ), [ item, onOpen ] );
if ( inline ) {
return (
<span className="jp-protect-threats__row">
<button type="button" className="jp-protect-threats__title is-inline" onClick={ onClick }>
<ThreatMedia threat={ item } size={ 32 } />
<span className="jp-protect-threats__label">
{ kind && (
<>
<strong>
{ sprintf(
/* translators: %s is a kind of threat, such as "Vulnerable plugin", before what it's in. */
__( '%s:', 'jetpack-protect-pkg' ),
kind
) }
</strong>{ ' ' }
</>
) }
{ subject }
</span>
</button>
<ThreatSeverityBadge severity={ item.severity } />
</span>
);
}
return (
<button
type="button"
className="jp-protect-threats__title"
data-threat-id={ item.id }
onClick={ onClick }
>
<ThreatMedia threat={ item } size={ 32 } />
<span className="jp-protect-threats__label">
{ kind ? <strong>{ kind }</strong> : subject }
{ kind && <span className="jp-protect-card__muted">{ subject }</span> }
</span>
</button>
);
}
/**
* What a row says while an action runs on its threat.
*
* @param busy - The running action.
* @return The status, such as "Fixing…".
*/
function getBusyLabel( busy: NonNullable< ThreatActionState[ 'busy' ] > ): string {
switch ( busy ) {
case 'fixing':
return __( 'Fixing…', 'jetpack-protect-pkg' );
case 'ignoring':
return __( 'Ignoring…', 'jetpack-protect-pkg' );
case 'unignoring':
return __( 'Unignoring…', 'jetpack-protect-pkg' );
case 'deleting':
return __( 'Deleting…', 'jetpack-protect-pkg' );
}
}
/**
* The date column: the date, or what is happening to the threat while an action runs.
*
* @param props - Component props.
* @param props.item - The threat.
* @param props.date - The ISO date.
* @return The cell.
*/
function DateCell( { item, date }: { item: ScanThreat; date: string } ) {
const { busy } = useThreatAction( item.id );
if ( busy ) {
return (
<span className="jp-protect-threats__busy jp-protect-threats__nowrap">
<Spinner />
{ getBusyLabel( busy ) }
</span>
);
}
return date ? (
<span className="jp-protect-card__muted jp-protect-threats__nowrap">
{ formatDetected( date ) }
</span>
) : null;
}
type ThreatFieldOptions = {
threatLabel: string;
dateLabel: string;
getDate: ( item: ScanThreat ) => string;
/** Show each threat on one line, with its severity, for a list without a Severity column. */
inline?: boolean;
};
/**
* A threats table's columns: severity, the threat, then a date.
*
* @param open - Opens a threat in the inspector.
* @param options - The columns' labels and date.
* @param options.threatLabel - The threat column's heading.
* @param options.dateLabel - The date column's heading.
* @param options.getDate - The ISO date the date column shows.
* @param options.inline - Whether each threat takes one line, with its severity.
* @return The fields.
*/
export function getThreatFields(
open: ( item: ScanThreat ) => void,
{ threatLabel, dateLabel, getDate, inline = false }: ThreatFieldOptions
): Field< ScanThreat >[] {
return [
{
id: 'severity',
label: __( 'Severity', 'jetpack-protect-pkg' ),
type: 'integer',
enableHiding: false,
// Sorting only: a list's filter offers severity levels instead of raw numbers.
filterBy: false,
getValue: ( { item } ) => item.severity ?? 0,
render: ( { item } ) => <ThreatSeverityBadge severity={ item.severity } />,
},
{
id: 'threat',
label: threatLabel,
enableHiding: false,
enableSorting: false,
enableGlobalSearch: true,
getValue: ( { item } ) => {
const { kind, subject } = getThreatLabel( item );
return kind ? `${ kind }: ${ subject }` : subject;
},
render: ( { item } ) => <ThreatCell item={ item } onOpen={ open } inline={ inline } />,
},
{
id: 'date',
label: dateLabel,
type: 'datetime',
enableHiding: false,
getValue: ( { item } ) => getDate( item ),
render: ( { item } ) => <DateCell item={ item } date={ getDate( item ) } />,
},
];
}
/**
* A threats table's starting view.
*
* @param sortField - The column to sort by, descending.
* @return The view.
*/
export function createThreatView( sortField: 'severity' | 'date' ): View {
return {
type: 'table',
search: '',
page: 1,
perPage: 20,
sort: { field: sortField, direction: 'desc' },
fields: [ 'severity', 'threat', 'date' ],
layout: {
styles: {
severity: { width: '1%', align: 'start' },
threat: { width: '100%' },
date: { width: '1%' },
},
},
};
}
export const LEVELS = [
{ value: 'critical', label: __( 'Critical', 'jetpack-protect-pkg' ) },
{ value: 'high', label: __( 'High', 'jetpack-protect-pkg' ) },
{ value: 'low', label: __( 'Low', 'jetpack-protect-pkg' ) },
];
const TYPES = [
{ value: 'plugins', label: __( 'Plugin', 'jetpack-protect-pkg' ) },
{ value: 'themes', label: __( 'Theme', 'jetpack-protect-pkg' ) },
{ value: 'core', label: __( 'WordPress', 'jetpack-protect-pkg' ) },
{ value: 'file', label: __( 'File', 'jetpack-protect-pkg' ) },
];
/**
* A threat's severity level, matching the badge: Critical from 5, High from 3, else Low.
*
* @param item - The threat.
* @return The level.
*/
export function getLevel( item: ScanThreat ): string {
const severity = item.severity ?? 0;
if ( severity >= 5 ) {
return 'critical';
}
return severity >= 3 ? 'high' : 'low';
}
// Filters only; the row already shows both.
const FILTER_FIELDS: Field< ScanThreat >[] = [
{
id: 'level',
label: __( 'Severity', 'jetpack-protect-pkg' ),
elements: LEVELS,
filterBy: { operators: [ 'isAny' ] },
enableHiding: false,
enableSorting: false,
getValue: ( { item } ) => getLevel( item ),
},
{
id: 'type',
label: __( 'Type', 'jetpack-protect-pkg' ),
elements: TYPES,
filterBy: { operators: [ 'isAny' ] },
enableHiding: false,
enableSorting: false,
getValue: ( { item } ) => getThreatType( item as Threat ) ?? '',
},
];
const NO_SELECTION: string[] = [];
type ThreatsListProps = {
threats: ScanThreat[];
/** Whether the site's plan can fix and ignore threats. */
canAct: boolean;
};
/**
* The threats a scan found, under a heading that collapses them, with search, filters and bulk actions.
*
* Choosing a threat opens its details in the inspector.
*
* @param props - Component props.
* @param props.threats - The active threats.
* @param props.canAct - Whether the site can fix threats, which is what selecting rows is for.
* @return The list.
*/
export default function ThreatsList( { threats, canAct }: ThreatsListProps ) {
// As the title field, the threat column also names each row's checkbox.
const [ view, setView ] = useState< View >( () => ( {
...createThreatView( 'severity' ),
titleField: 'threat',
fields: [ 'date' ],
} ) );
const [ isOpen, setIsOpen ] = useState( true );
const [ selection, setSelection ] = useState< string[] >( NO_SELECTION );
const [ selected ] = useSearchParam( THREAT_PARAM );
const open = useOpenThreat();
const getItemId = useCallback( ( item: ScanThreat ) => String( item.id ), [] );
// Threats that leave the list leave the selection, so Undo doesn't bring them back checked.
useEffect( () => {
setSelection( current => {
const live = current.filter( id => threats.some( item => getItemId( item ) === id ) );
return live.length === current.length ? current : live;
} );
}, [ threats, getItemId ] );
const deselect = useCallback(
( items: ScanThreat[] ) =>
setSelection( current =>
current.filter( id => ! items.some( item => getItemId( item ) === id ) )
),
[ getItemId ]
);
const fields = useMemo(
() => [
...getThreatFields( open, {
threatLabel: __( 'Vulnerabilities', 'jetpack-protect-pkg' ),
dateLabel: __( 'Detected on', 'jetpack-protect-pkg' ),
getDate: item => item.firstDetected ?? '',
inline: true,
} ),
...FILTER_FIELDS,
],
[ open ]
);
const actions = useThreatRowActions( open, canAct, deselect );
const { data, paginationInfo } = useMemo(
() => filterSortAndPaginate( threats, view, fields ),
[ threats, view, fields ]
);
const { totalPages } = paginationInfo;
const onChangeView = useCallback(
( next: View ) => {
recordViewChange( view, next, totalPages );
setView( next );
},
[ view, totalPages ]
);
// Collapsed, the arrow keys would open threats the reader can't see.
useArrowKeyNavigation( data, isOpen ? selected : undefined, getItemId, open );
const hasSelectedRows = data.some( item => selection.includes( getItemId( item ) ) );
return (
<Collapsible.Root open={ isOpen } onOpenChange={ setIsOpen } className="jp-protect-threats">
<h3 className="jp-protect-threats__heading">
<Collapsible.Trigger className="jp-protect-threats__toggle">
{ __( 'Review and fix threats', 'jetpack-protect-pkg' ) }
<Icon icon={ chevronDown } size={ 24 } />
</Collapsible.Trigger>
</h3>
<Collapsible.Panel>
<DataViews
data={ data }
fields={ fields }
actions={ actions }
view={ view }
onChangeView={ onChangeView }
paginationInfo={ paginationInfo }
defaultLayouts={ DEFAULT_LAYOUTS }
getItemId={ getItemId }
selection={ selection }
onChangeSelection={ setSelection }
>
<Stack
direction="row"
align="center"
justify="space-between"
gap="xs"
className="jp-protect-threats__toolbar"
>
<Stack direction="row" align="center" gap="sm">
<DataViews.Search />
<DataViews.FiltersToggle />
</Stack>
<DataViews.ViewConfig />
</Stack>
<DataViews.FiltersToggled className="jp-protect-threats__filters" />
<DataViews.Layout />
{ /* Only for bulk actions or more pages; otherwise it repeats the header's select all. */ }
{ ( hasSelectedRows || paginationInfo.totalPages > 1 ) && <DataViews.Footer /> }
</DataViews>
</Collapsible.Panel>
</Collapsible.Root>
);
}
|