Skip to content

Commit

Permalink
Adjust approach to bringing label prop to the FixAllThreatsModal
Browse files Browse the repository at this point in the history
  • Loading branch information
dkmyta committed Jul 12, 2024
1 parent 3f5e9bf commit a45b5d6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ThreatFixHeader from '../threat-fix-header';
import UserConnectionGate from '../user-connection-gate';
import styles from './styles.module.scss';

const FixThreatModal = ( { id, fixable, title, icon, severity } ) => {
const FixThreatModal = ( { id, fixable, label, icon, severity } ) => {
const { setModal, fixThreats } = useDispatch( STORE_ID );
const threatsUpdating = useSelect( select => select( STORE_ID ).getThreatsUpdating() );

Expand Down Expand Up @@ -39,7 +39,7 @@ const FixThreatModal = ( { id, fixable, title, icon, severity } ) => {

<div className={ styles.list }>
<ThreatFixHeader
threat={ { id, fixable, title, icon, severity } }
threat={ { id, fixable, label, icon, severity } }
fixAllDialog={ false }
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default function ThreatFixHeader( { threat, fixAllDialog, onCheckFix } )
<Icon icon={ threat.icon } className={ styles.threat__icon } />
<div className={ styles.threat__summary }>
<Text className={ styles.threat__summary__label } mb={ 1 }>
{ threat.title }
{ threat.label }
</Text>
<Text className={ styles.threat__summary__title }>
{ getFixerMessage( threat.fixable ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import styles from './styles.module.scss';

const ThreatAccordionItem = ( {
description,
filename,
fixedIn,
icon,
id,
label,
name,
source,
table,
title,
type,
version,
} ) => {
const { adminUrl } = window.jetpackProtectInitialState || {};
const { run } = useProductCheckoutWorkflow( {
Expand All @@ -36,32 +34,10 @@ const ThreatAccordionItem = ( {
</Button>
) : null;

/**
* Get Label
*
* @returns {string} Threat label based on the assumed threat type (extension, file, database, etc).
*/
const getLabel = useCallback( () => {
if ( name && version ) {
// Extension threat i.e. "Woocommerce (3.0.0)"
return `${ name } (${ version })`;
}

if ( filename ) {
// File threat i.e. "index.php"
return filename.split( '/' ).pop();
}

if ( table ) {
// Database threat i.e. "wp_posts"
return table;
}
}, [ filename, name, table, version ] );

return (
<FreeAccordionItem
id={ id }
label={ getLabel() }
label={ label }
title={ title }
icon={ icon }
onOpen={ useCallback( () => {
Expand Down Expand Up @@ -111,12 +87,25 @@ const FreeList = ( { list } ) => {
return (
<FreeAccordion>
{ list.map(
( { description, fixedIn, icon, id, name, source, table, title, type, version } ) => (
( {
description,
fixedIn,
icon,
id,
label,
name,
source,
table,
title,
type,
version,
} ) => (
<ThreatAccordionItem
description={ description }
fixedIn={ fixedIn }
icon={ icon }
id={ id }
label={ label }
key={ id }
name={ name }
source={ source }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const ThreatAccordionItem = ( {
event.preventDefault();
setModal( {
type: 'FIX_THREAT',
props: { id, fixable, title, icon, severity },
props: { id, fixable, label, icon, severity },
} );
};
};
Expand Down Expand Up @@ -155,25 +155,6 @@ const PaidList = ( { list } ) => {

const [ isSmall ] = useBreakpointMatch( [ 'sm', 'lg' ], [ null, '<' ] );

const getLabel = threat => {
if ( threat.name && threat.version ) {
// Extension threat i.e. "Woocommerce (3.0.0)"
return `${ threat.name } (${ threat.version })`;
}

if ( threat.filename ) {
// File threat i.e. "index.php"
return threat.filename.split( '/' ).pop();
}

if ( threat.table ) {
// Database threat i.e. "wp_posts"
return threat.table;
}
};

list = list.map( threat => ( { label: getLabel( threat ), ...threat } ) );

return (
<>
{ ! isSmall && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,27 @@ const useThreatsList = () => {
};
}, [ core, database, files, plugins, selected, themes ] );

const getLabel = threat => {
if ( threat.name && threat.version ) {
// Extension threat i.e. "Woocommerce (3.0.0)"
return `${ threat.name } (${ threat.version })`;
}

if ( threat.filename ) {
// File threat i.e. "index.php"
return threat.filename.split( '/' ).pop();
}

if ( threat.table ) {
// Database threat i.e. "wp_posts"
return threat.table;
}
};

const list = useMemo( () => {
return [ ...unsortedList ].sort( sortThreats );
return unsortedList
.sort( sortThreats )
.map( threat => ( { label: getLabel( threat ), ...threat } ) );
}, [ unsortedList ] );

return {
Expand Down

0 comments on commit a45b5d6

Please sign in to comment.