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 | 4x 2x 4x 4x 1x 103x 309x 103x 309x 375x 309x 6x 6x 6x 6x 103x 564x 103x 439x 4x 4x 125x 127x 125x 337x 2x 2x 103x 1624x 1624x 103x 927x 1624x 187x 187x 187x 103x | import { useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { DeleteSoftwareModal, getDeleteTitle } from './delete-software';
import { FixDeleteModal, getDeleteFixTitle, isDeleteFix } from './fix-delete-confirmation';
import { getSoftwareActionLabels } from './labels';
import { useCanIgnore } from './store';
import { fixThreats, ignoreThreats, unignoreThreat, useGetThreatBusy } from './threat-actions';
import type { ScanThreat } from './types';
import type { Action } from '@wordpress/dataviews';
type LinkKey = 'update' | 'deactivate' | 'details';
type OpenThreat = ( item: ScanThreat ) => void;
/**
* Focus a threat's row when focus was lost, as when the button that started its action goes away.
*
* @param item - The threat.
*/
function refocusRow( item: ScanThreat ) {
const title = Array.from( document.querySelectorAll< HTMLElement >( '[data-threat-id]' ) ).find(
element => element.dataset.threatId === String( item.id )
);
const { activeElement, body } = title?.ownerDocument ?? {};
if ( title && ( ! activeElement || activeElement === body ) ) {
title.focus();
}
}
/**
* A row's DataViews actions: primary ones show as buttons without hovering, and the ⋯ menu lists them all.
*
* @param open - Opens a threat in the inspector.
* @param canAct - Whether the site's plan can fix and ignore threats.
* @param canIgnore - Whether the host lets the site ignore threats.
* @param getBusy - Gets the action running on a threat.
* @param onBulk - Offers Auto-fix and Ignore on selected rows; gets the threats either starts on.
* @return The actions.
*/
function getThreatRowActions(
open: OpenThreat,
canAct: boolean,
canIgnore: boolean,
getBusy: ReturnType< typeof useGetThreatBusy >,
onBulk?: ( items: ScanThreat[] ) => void
): Action< ScanThreat >[] {
const link =
( key: LinkKey ) =>
( [ item ]: ScanThreat[] ) => {
const url = item?.extension?.actions?.[ key ];
if ( url ) {
window.open( url, key === 'details' ? '_blank' : '_self', 'noopener' );
}
};
const softwareLabel =
( key: 'update' | 'deactivate' | 'delete' ) =>
( [ item ]: ScanThreat[] ) =>
item ? getSoftwareActionLabels( item )[ key ] : '';
const hasLink = ( key: LinkKey ) => ( item: ScanThreat ) => !! item.extension?.actions?.[ key ];
// Starting these locks the row, removing the clicked button and maybe its ⋯ menu.
const locking =
( start: ( items: ScanThreat[], open: OpenThreat ) => unknown ) => ( items: ScanThreat[] ) => {
const [ item ] = items;
Eif ( item ) {
start( items, open );
setTimeout( () => refocusRow( item ) );
}
};
const canFix = ( item: ScanThreat ) =>
canAct && !! item.fixable && item.status !== 'ignored' && item.status !== 'fixed';
const actions: Action< ScanThreat >[] = [
{
id: 'view',
label: __( 'View', 'jetpack-protect-pkg' ),
isPrimary: true,
callback: ( [ item ] ) => item && open( item ),
},
{
id: 'auto-fix',
label: __( 'Auto-fix', 'jetpack-protect-pkg' ),
isPrimary: true,
isEligible: item => canFix( item ) && ! isDeleteFix( item ),
supportsBulk: !! onBulk,
// Not awaited: a fix can take minutes, and its snackbar follows it.
callback: locking( ( items, from ) => {
onBulk?.( items );
fixThreats( items, from );
} ),
},
{
id: 'auto-fix-delete',
label: __( 'Auto-fix', 'jetpack-protect-pkg' ),
isPrimary: true,
isEligible: item => canFix( item ) && isDeleteFix( item ),
modalHeader: ( [ item ] ) => ( item ? getDeleteFixTitle( item ) : '' ),
RenderModal: FixDeleteModal,
},
{
id: 'unignore',
label: __( 'Unignore', 'jetpack-protect-pkg' ),
isPrimary: true,
isEligible: item => canAct && item.status === 'ignored',
callback: locking( ( [ item ], from ) => unignoreThreat( item, from ) ),
},
{
id: 'update',
label: softwareLabel( 'update' ),
isEligible: hasLink( 'update' ),
callback: link( 'update' ),
},
{
id: 'deactivate',
label: softwareLabel( 'deactivate' ),
isEligible: hasLink( 'deactivate' ),
callback: link( 'deactivate' ),
},
{
id: 'delete',
label: softwareLabel( 'delete' ),
modalHeader: ( [ item ] ) => ( item ? getDeleteTitle( item ) : '' ),
isEligible: item => !! item.extension?.actions?.delete,
RenderModal: DeleteSoftwareModal,
},
{
id: 'wordpress-org',
label: __( 'View on WordPress.org', 'jetpack-protect-pkg' ),
isEligible: hasLink( 'details' ),
callback: link( 'details' ),
},
{
id: 'ignore',
label: __( 'Ignore', 'jetpack-protect-pkg' ),
isEligible: item =>
canAct && canIgnore && item.status !== 'ignored' && item.status !== 'fixed',
supportsBulk: !! onBulk,
callback: locking( ( items, from ) => {
onBulk?.( items );
ignoreThreats( items, from );
} ),
},
];
/*
* DataViews can't disable an action for one row, so a busy row keeps only View. A deleting row
* keeps them, since hiding Delete would close its open modal; deleteSoftware refuses a repeat.
*/
const isLocked = ( item: ScanThreat ) => {
const busy = getBusy( item.id );
return !! busy && busy !== 'deleting';
};
return actions.map( action =>
action.id === 'view'
? action
: {
...action,
isEligible: item => ! isLocked( item ) && ( action.isEligible?.( item ) ?? true ),
}
);
}
/**
* A threats table's row actions, updated as actions on its threats start and finish.
*
* @param open - Opens a threat in the inspector.
* @param canAct - Whether the site's plan can fix and ignore threats.
* @param onBulk - Offers Auto-fix and Ignore on selected rows; gets the threats either starts on.
* @return The actions.
*/
export function useThreatRowActions(
open: OpenThreat,
canAct: boolean,
onBulk?: ( items: ScanThreat[] ) => void
): Action< ScanThreat >[] {
const canIgnore = useCanIgnore();
const getBusy = useGetThreatBusy();
return useMemo(
() => getThreatRowActions( open, canAct, canIgnore, getBusy, onBulk ),
[ open, canAct, canIgnore, getBusy, onBulk ]
);
}
|