-
Notifications
You must be signed in to change notification settings - Fork 862
Expand file tree
/
Copy pathscan-admin-section-hero.tsx
More file actions
186 lines (172 loc) · 6.17 KB
/
scan-admin-section-hero.tsx
File metadata and controls
186 lines (172 loc) · 6.17 KB
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
import { Text, Button, useBreakpointMatch } from '@automattic/jetpack-components';
import { Tooltip } from '@wordpress/components';
import { dateI18n } from '@wordpress/date';
import { __, _n, sprintf } from '@wordpress/i18n';
import clsx from 'clsx';
import { useCallback, useState, useMemo } from 'react';
import AdminSectionHero from '../../components/admin-section-hero';
import ErrorAdminSectionHero from '../../components/error-admin-section-hero';
import OnboardingPopover from '../../components/onboarding-popover';
import useScanStatusQuery, { isScanInProgress } from '../../data/scan/use-scan-status-query';
import useAnalyticsTracks from '../../hooks/use-analytics-tracks';
import useFixers from '../../hooks/use-fixers';
import useModal from '../../hooks/use-modal';
import usePlan from '../../hooks/use-plan';
import useWafData from '../../hooks/use-waf-data';
import ScanningAdminSectionHero from './scanning-admin-section-hero';
import styles from './styles.module.scss';
const ScanAdminSectionHero: React.FC = ( { size = 'normal' }: { size?: 'normal' | 'large' } ) => {
const { recordEvent } = useAnalyticsTracks();
const { hasPlan, upgradePlan } = usePlan();
const { setModal } = useModal();
const [ isSm ] = useBreakpointMatch( 'sm' );
const { data: status } = useScanStatusQuery();
const { isThreatFixInProgress, isThreatFixStale } = useFixers();
const getScan = useCallback( () => {
recordEvent( 'jetpack_protect_scan_header_get_scan_link_click' );
upgradePlan();
}, [ recordEvent, upgradePlan ] );
const { globalStats } = useWafData();
const totalVulnerabilities = parseInt( globalStats?.totalVulnerabilities );
const totalVulnerabilitiesFormatted = isNaN( totalVulnerabilities )
? '50,000'
: totalVulnerabilities.toLocaleString();
const numThreats = status.threats.length;
// Popover anchor
const [ dailyScansPopoverAnchor, setDailyScansPopoverAnchor ] = useState( null );
const [ showAutoFixersPopoverAnchor, setShowAutoFixersPopoverAnchor ] = useState( null );
// List of fixable threats that do not have a fix in progress
const fixableList = useMemo( () => {
return status.threats.filter( threat => {
const threatId = typeof threat.id === 'string' ? parseInt( threat.id ) : threat.id;
return (
threat.fixable && ! isThreatFixInProgress( threatId ) && ! isThreatFixStale( threatId )
);
} );
}, [ status.threats, isThreatFixInProgress, isThreatFixStale ] );
const scanning = isScanInProgress( status );
let lastCheckedLocalTimestamp = null;
if ( status.lastChecked ) {
// Convert the lastChecked UTC date to a local timestamp
lastCheckedLocalTimestamp = new Date( status.lastChecked + ' UTC' ).getTime();
}
let heading = __( "Don't worry about a thing", 'jetpack-protect' );
if ( numThreats > 0 ) {
if ( hasPlan ) {
heading = sprintf(
/* translators: %s: Total number of threats */
_n( '%1$s active threat', '%1$s active threats', numThreats, 'jetpack-protect' ),
numThreats
);
} else {
heading = sprintf(
/* translators: %s: Total number of vulnerabilities */
_n(
'%1$s active vulnerability',
'%1$s active vulnerabilities',
numThreats,
'jetpack-protect'
),
numThreats
);
}
}
const handleShowAutoFixersClick = threatList => {
return event => {
event.preventDefault();
setModal( {
type: 'FIX_ALL_THREATS',
props: { threatList },
} );
};
};
if ( scanning ) {
return <ScanningAdminSectionHero size={ size } />;
}
if ( status.error ) {
return (
<ErrorAdminSectionHero
baseErrorMessage={ __( 'We are having problems scanning your site.', 'jetpack-protect' ) }
errorMessage={ status.errorMessage }
errorCode={ status.errorCode }
/>
);
}
return (
<AdminSectionHero>
<AdminSectionHero.Main
className={ clsx( styles[ 'hero-main' ], {
[ styles[ 'hero-main--large' ] ]: size === 'large',
} ) }
>
<Text className={ styles[ 'last-checked' ] } mb={ 2 } ref={ setDailyScansPopoverAnchor }>
{ lastCheckedLocalTimestamp
? sprintf(
// translators: %s: date and time of the last scan
__( '%s results', 'jetpack-protect' ),
dateI18n( 'F jS, g:i A', lastCheckedLocalTimestamp, false )
)
: __( 'Most recent results', 'jetpack-protect' ) }
</Text>
<OnboardingPopover
id={ hasPlan ? 'paid-daily-and-manual-scans' : 'free-daily-scans' }
position={ isSm ? 'bottom right' : 'middle right' }
anchor={ dailyScansPopoverAnchor }
/>
<AdminSectionHero.Heading icon={ numThreats > 0 ? 'error' : 'success' }>
{ heading }
</AdminSectionHero.Heading>
{ hasPlan ? (
<Text>
{ __(
"We actively review your site's files line-by-line to identify threats and vulnerabilities.",
'jetpack-protect'
) }
</Text>
) : (
<>
<Text mb={ 4 }>
{ sprintf(
// translators: placeholder is the number of total vulnerabilities i.e. "22,000".
__(
'Every day we check your plugins, themes, and WordPress version against our %s listed vulnerabilities powered by WPScan, an Automattic brand.',
'jetpack-protect'
),
totalVulnerabilitiesFormatted
) }
</Text>
<Tooltip
text={ __(
'Upgrade Jetpack Protect to get access to advanced malware scanning with one-click fixes for most threats.',
'jetpack-protect'
) }
>
<Button onClick={ getScan }>
{ __( 'Upgrade to unlock malware scanning', 'jetpack-protect' ) }
</Button>
</Tooltip>
</>
) }
{ fixableList.length > 0 && (
<>
<div className={ styles[ 'auto-fixers' ] } ref={ setShowAutoFixersPopoverAnchor }>
<Button onClick={ handleShowAutoFixersClick( fixableList ) }>
{ sprintf(
/* translators: Translates to Show auto fixers $s: Number of fixable threats. */
__( 'Show auto fixers (%s)', 'jetpack-protect' ),
fixableList.length
) }
</Button>
</div>
<OnboardingPopover
id="paid-fix-all-threats"
position={ isSm ? 'bottom right' : 'middle right' }
anchor={ showAutoFixersPopoverAnchor }
/>
</>
) }
</AdminSectionHero.Main>
</AdminSectionHero>
);
};
export default ScanAdminSectionHero;