1
- import {
2
- fixThreatsMutation ,
3
- fixThreatsStatusQuery ,
4
- siteScanQuery ,
5
- siteScanHistoryQuery ,
6
- } from '@automattic/api-queries' ;
7
- import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query' ;
8
1
import { __experimentalVStack as VStack , Button } from '@wordpress/components' ;
9
2
import { useDispatch } from '@wordpress/data' ;
10
- import { __ } from '@wordpress/i18n' ;
3
+ import { __ , _n } from '@wordpress/i18n' ;
11
4
import { store as noticesStore } from '@wordpress/notices' ;
12
- import { useState , useEffect , useCallback } from 'react' ;
5
+ import { useEffect } from 'react' ;
13
6
import { ButtonStack } from '../../../components/button-stack' ;
14
7
import { Text } from '../../../components/text' ;
8
+ import { useFixThreats } from '../hooks/use-fix-threats' ;
15
9
import { ThreatsDetailCard } from './threats-detail-card' ;
16
10
import type { Threat } from '@automattic/api-core' ;
17
11
import type { RenderModalProps } from '@wordpress/dataviews' ;
@@ -27,69 +21,74 @@ export function BulkFixThreatsModal( { items, closeModal, siteId }: BulkFixThrea
27
21
const bulkFixableIds = new Set ( bulkFixableThreats . map ( ( item ) => item . id ) ) ;
28
22
const remainingThreats = items . filter ( ( item ) => ! bulkFixableIds . has ( item . id ) ) ;
29
23
30
- const queryClient = useQueryClient ( ) ;
31
- const bulkFixThreats = useMutation ( fixThreatsMutation ( siteId ) ) ;
32
24
const { createSuccessNotice, createErrorNotice } = useDispatch ( noticesStore ) ;
33
- const [ isBulkFixInProgress , setIsBulkFixInProgress ] = useState ( false ) ;
34
25
35
- const { data : bulkFixStatusData } = useQuery ( {
36
- ...fixThreatsStatusQuery ( siteId , Array . from ( bulkFixableIds ) ) ,
37
- refetchInterval : isBulkFixInProgress ? 2000 : false ,
38
- enabled : isBulkFixInProgress ,
39
- } ) ;
40
-
41
- const handleBulkFixed = useCallback (
42
- ( message : string ) => {
43
- queryClient . invalidateQueries ( siteScanQuery ( siteId ) ) ;
44
- queryClient . invalidateQueries ( siteScanHistoryQuery ( siteId ) ) ;
45
- closeModal ?.( ) ;
46
- createSuccessNotice ( message , { type : 'snackbar' } ) ;
47
- } ,
48
- [ closeModal , createSuccessNotice , queryClient , siteId ]
26
+ const { startFix, isFixing, status, error } = useFixThreats (
27
+ siteId ,
28
+ Array . from ( bulkFixableIds )
49
29
) ;
50
30
51
31
useEffect ( ( ) => {
52
- if ( ! bulkFixStatusData ?. threats ) {
53
- return ;
54
- }
32
+ if ( status . isComplete && ! isFixing ) {
33
+ closeModal ?.( ) ;
55
34
56
- if ( isBulkFixInProgress ) {
57
- const pendingThreats = bulkFixStatusData . threats . filter (
58
- ( threat ) => threat ?. status === 'in_progress'
59
- ) ;
60
- if ( pendingThreats . length > 0 ) {
61
- return ;
35
+ if ( status . allFixed ) {
36
+ createSuccessNotice (
37
+ _n ( 'Threat fixed.' , 'All threats were successfully fixed.' , bulkFixableThreats . length ) ,
38
+ {
39
+ type : 'snackbar' ,
40
+ }
41
+ ) ;
42
+ } else {
43
+ createErrorNotice (
44
+ _n (
45
+ 'Failed to fix threat. Please contact support.' ,
46
+ 'Not all threats could be fixed. Please contact support.' ,
47
+ bulkFixableThreats . length
48
+ ) ,
49
+ {
50
+ type : 'snackbar' ,
51
+ }
52
+ ) ;
62
53
}
54
+ }
55
+ } , [
56
+ status ,
57
+ isFixing ,
58
+ closeModal ,
59
+ createSuccessNotice ,
60
+ createErrorNotice ,
61
+ bulkFixableThreats . length ,
62
+ ] ) ;
63
63
64
- const fixedThreats = bulkFixStatusData . threats . filter (
65
- ( threat ) => threat ?. status === 'fixed'
64
+ useEffect ( ( ) => {
65
+ if ( error ) {
66
+ closeModal ?.( ) ;
67
+ createErrorNotice (
68
+ _n (
69
+ 'Error fixing threat. Please contact support.' ,
70
+ 'Error fixing threats. Please contact support.' ,
71
+ bulkFixableThreats . length
72
+ ) ,
73
+ {
74
+ type : 'snackbar' ,
75
+ }
66
76
) ;
67
- const allFixed = fixedThreats . length === bulkFixStatusData . threats . length ;
68
- const message = allFixed
69
- ? __ ( 'All threats were successfully fixed.' )
70
- : __ ( 'Not all threats could be fixed. Please contact our support.' ) ;
71
-
72
- setIsBulkFixInProgress ( false ) ;
73
- handleBulkFixed ( message ) ;
74
77
}
75
- } , [ bulkFixStatusData , isBulkFixInProgress , handleBulkFixed ] ) ;
78
+ } , [ error , closeModal , createErrorNotice , bulkFixableThreats . length ] ) ;
76
79
77
80
const handleFixThreats = ( ) => {
78
- setIsBulkFixInProgress ( true ) ;
79
- bulkFixThreats . mutate ( Array . from ( bulkFixableIds ) , {
80
- onError : ( ) => {
81
- closeModal ?.( ) ;
82
- createErrorNotice ( __ ( 'Error fixing threats. Please contact support.' ) , {
83
- type : 'snackbar' ,
84
- } ) ;
85
- } ,
86
- } ) ;
81
+ startFix ( ) ;
87
82
} ;
88
83
89
84
const bulkFixableSection = (
90
85
< >
91
86
< Text variant = "muted" >
92
- { __ ( 'Jetpack will be fixing the selected threats and low risk items:' ) }
87
+ { _n (
88
+ 'Jetpack will be fixing the selected threat and low risk item:' ,
89
+ 'Jetpack will be fixing the selected threats and low risk items:' ,
90
+ bulkFixableThreats . length
91
+ ) }
93
92
</ Text >
94
93
< ThreatsDetailCard threats = { bulkFixableThreats } />
95
94
</ >
@@ -98,8 +97,10 @@ export function BulkFixThreatsModal( { items, closeModal, siteId }: BulkFixThrea
98
97
const remainingThreatsSection = (
99
98
< >
100
99
< Text variant = "muted" >
101
- { __ (
102
- 'These threats cannot be fixed in bulk because individual confirmation is required:'
100
+ { _n (
101
+ 'This threat cannot be fixed in bulk because individual confirmation is required:' ,
102
+ 'These threats cannot be fixed in bulk because individual confirmation is required:' ,
103
+ remainingThreats . length
103
104
) }
104
105
</ Text >
105
106
< ThreatsDetailCard threats = { remainingThreats } />
@@ -119,10 +120,12 @@ export function BulkFixThreatsModal( { items, closeModal, siteId }: BulkFixThrea
119
120
< Button
120
121
variant = "primary"
121
122
onClick = { handleFixThreats }
122
- isBusy = { isBulkFixInProgress }
123
- disabled = { ! canBulkFix || isBulkFixInProgress }
123
+ isBusy = { isFixing }
124
+ disabled = { ! canBulkFix || isFixing }
124
125
>
125
- { isBulkFixInProgress ? __ ( 'Fixing threats…' ) : __ ( 'Fix all threats' ) }
126
+ { isFixing
127
+ ? _n ( 'Fixing threat…' , 'Fixing threats…' , bulkFixableThreats . length )
128
+ : _n ( 'Fix threat' , 'Fix all threats' , bulkFixableThreats . length ) }
126
129
</ Button >
127
130
</ ButtonStack >
128
131
</ VStack >
0 commit comments