-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use inline button for primary fixer action
- Loading branch information
1 parent
f919501
commit 7a4a251
Showing
10 changed files
with
289 additions
and
352 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
projects/js-packages/components/components/threat-fixer-button/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { Button, Text, ActionPopover } from '@automattic/jetpack-components'; | ||
import { Threat } from '@automattic/jetpack-scan'; | ||
import { ExternalLink } from '@wordpress/components'; | ||
import { createInterpolateElement, useCallback, useMemo, useState } from '@wordpress/element'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { PAID_PLUGIN_SUPPORT_URL } from '../threats-data-views/constants'; | ||
import styles from './styles.module.scss'; | ||
import { fixerStatusIsStale } from './utils'; | ||
|
||
/** | ||
* ThreatFixerButton | ||
* | ||
* @param {object} props - Component props. | ||
* @param {object} props.threat - The threat. | ||
* @param {Function} props.onClick - The onClick function. | ||
* @param {string} props.className - The className. | ||
* | ||
* @return {JSX.Element} The component. | ||
*/ | ||
export default function ThreatFixerButton( { | ||
threat, | ||
className, | ||
onClick, | ||
}: { | ||
threat: Threat; | ||
className?: string; | ||
onClick: ( items: Threat[] ) => void; | ||
} ): JSX.Element { | ||
const [ isPopoverVisible, setIsPopoverVisible ] = useState( false ); | ||
|
||
const [ anchor, setAnchor ] = useState( null ); | ||
|
||
const children = useMemo( () => { | ||
if ( ! threat.fixable ) { | ||
return null; | ||
} | ||
if ( threat.fixer && threat.fixer.error ) { | ||
return __( 'Error', 'jetpack' ); | ||
} | ||
if ( threat.fixer && threat.fixer.status === 'in_progress' ) { | ||
return __( 'Fixing…', 'jetpack' ); | ||
} | ||
if ( threat.fixable.fixer === 'delete' ) { | ||
return __( 'Delete', 'jetpack' ); | ||
} | ||
if ( threat.fixable.fixer === 'update' ) { | ||
return __( 'Update', 'jetpack' ); | ||
} | ||
return __( 'Fix', 'jetpack' ); | ||
}, [ threat.fixable, threat.fixer ] ); | ||
|
||
const errorMessage = useMemo( () => { | ||
if ( threat.fixer && fixerStatusIsStale( threat.fixer ) ) { | ||
return __( 'Fixer is taking longer than expected.', 'jetpack' ); | ||
} | ||
|
||
if ( threat.fixer && 'error' in threat.fixer && threat.fixer.error ) { | ||
return __( 'An error occurred auto-fixing this threat.', 'jetpack' ); | ||
} | ||
|
||
return null; | ||
}, [ threat.fixer ] ); | ||
|
||
const handleClick = useCallback( | ||
( event: React.MouseEvent ) => { | ||
event.stopPropagation(); | ||
if ( errorMessage && ! isPopoverVisible ) { | ||
setIsPopoverVisible( true ); | ||
return; | ||
} | ||
onClick( [ threat ] ); | ||
}, | ||
[ onClick, errorMessage, isPopoverVisible, threat ] | ||
); | ||
|
||
const closePopover = useCallback( () => { | ||
setIsPopoverVisible( false ); | ||
}, [] ); | ||
|
||
if ( ! threat.fixable ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div> | ||
<Button | ||
size="small" | ||
weight="regular" | ||
variant="secondary" | ||
onClick={ handleClick } | ||
children={ children } | ||
className={ className } | ||
disabled={ threat.fixer && threat.fixer.status === 'in_progress' && ! errorMessage } | ||
isLoading={ threat.fixer && threat.fixer.status === 'in_progress' } | ||
isDestructive={ | ||
( threat.fixable && threat.fixable.fixer === 'delete' ) || | ||
( threat.fixer && threat.fixer.error ) || | ||
( threat.fixer && fixerStatusIsStale( threat.fixer ) ) | ||
} | ||
style={ { minWidth: '72px' } } | ||
ref={ setAnchor } | ||
/> | ||
{ isPopoverVisible && ( | ||
<ActionPopover | ||
anchor={ anchor } | ||
buttonContent={ __( 'Retry Fix', 'jetpack' ) } | ||
hideCloseButton={ true } | ||
noArrow={ false } | ||
onClick={ handleClick } | ||
onClose={ closePopover } | ||
title={ __( 'Auto-fix error', 'jetpack' ) } | ||
> | ||
<Text> | ||
{ createInterpolateElement( | ||
sprintf( | ||
/* translators: placeholder is an error message. */ | ||
__( | ||
'%s Please try again or <supportLink>contact support</supportLink>.', | ||
'jetpack' | ||
), | ||
errorMessage | ||
), | ||
{ | ||
supportLink: ( | ||
<ExternalLink | ||
href={ PAID_PLUGIN_SUPPORT_URL } | ||
className={ styles[ 'support-link' ] } | ||
/> | ||
), | ||
} | ||
) } | ||
</Text> | ||
</ActionPopover> | ||
) } | ||
</div> | ||
); | ||
} |
30 changes: 30 additions & 0 deletions
30
projects/js-packages/components/components/threat-fixer-button/stories/index.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import ThreatFixerButton from '../index.js'; | ||
|
||
export default { | ||
title: 'JS Packages/Components/Threat Fixer Button', | ||
component: ThreatFixerButton, | ||
}; | ||
|
||
export const Default = args => <ThreatFixerButton { ...args } />; | ||
Default.args = { | ||
threat: { fixable: { fixer: 'edit' } }, | ||
onClick: () => alert( 'Edit fixer callback triggered' ), // eslint-disable-line no-alert | ||
}; | ||
|
||
export const Update = args => <ThreatFixerButton { ...args } />; | ||
Update.args = { | ||
threat: { fixable: { fixer: 'update' } }, | ||
onClick: () => alert( 'Update fixer callback triggered' ), // eslint-disable-line no-alert | ||
}; | ||
|
||
export const Delete = args => <ThreatFixerButton { ...args } />; | ||
Delete.args = { | ||
threat: { fixable: { fixer: 'delete' } }, | ||
onClick: () => alert( 'Delete fixer callback triggered' ), // eslint-disable-line no-alert | ||
}; | ||
|
||
export const Loading = args => <ThreatFixerButton { ...args } />; | ||
Loading.args = { | ||
threat: { fixable: { fixer: 'edit' }, fixer: { status: 'in_progress' } }, | ||
onClick: () => alert( 'Fixer callback triggered' ), // eslint-disable-line no-alert | ||
}; |
9 changes: 9 additions & 0 deletions
9
projects/js-packages/components/components/threat-fixer-button/styles.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.support-link { | ||
color: inherit; | ||
|
||
&:focus, | ||
&:hover { | ||
color: inherit; | ||
box-shadow: none; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
projects/js-packages/components/components/threat-fixer-button/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ThreatFixStatus } from '@automattic/jetpack-scan'; | ||
|
||
const FIXER_IS_STALE_THRESHOLD = 1000 * 60 * 60 * 24; // 24 hours | ||
|
||
export const fixerTimestampIsStale = ( lastUpdatedTimestamp: string ) => { | ||
const now = new Date(); | ||
const lastUpdated = new Date( lastUpdatedTimestamp ); | ||
return now.getTime() - lastUpdated.getTime() >= FIXER_IS_STALE_THRESHOLD; | ||
}; | ||
|
||
export const fixerStatusIsStale = ( fixerStatus: ThreatFixStatus ) => { | ||
return ( | ||
'status' in fixerStatus && | ||
fixerStatus.status === 'in_progress' && | ||
fixerTimestampIsStale( fixerStatus.last_updated ) | ||
); | ||
}; |
137 changes: 0 additions & 137 deletions
137
projects/js-packages/components/components/threats-data-views/fixer-status.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.