@@ -11,8 +11,12 @@ import {
1111} from '@wordpress/element'
1212import { models } from '@wordpress/api'
1313
14+ let isPolling = false
15+ let cimoData = { status : cimo ?. status , action : cimo ?. action }
16+
1417const CimoDownloadNotice = props => {
15- const [ data , setData ] = useState ( { status : cimo ?. status , action : cimo ?. action } )
18+ const { inMediaLibrary = false } = props
19+ const [ data , setData ] = useState ( cimoData )
1620 const pollCountRef = useRef ( 0 )
1721
1822 const onDismiss = ( ) => {
@@ -33,6 +37,12 @@ const CimoDownloadNotice = props => {
3337
3438 // Polls the Cimo plugin status to detect installation or activation state changes
3539 const pollStatus = ( action , link , pollOnce = false ) => {
40+ if ( isPolling ) {
41+ return
42+ }
43+
44+ isPolling = true
45+
3646 fetch ( ajaxUrl , {
3747 method : 'POST' ,
3848 headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
@@ -43,7 +53,9 @@ const CimoDownloadNotice = props => {
4353 nonce : cimo . nonce ,
4454 } ) ,
4555 credentials : 'same-origin' ,
46- } ) . then ( res => res . json ( ) ) . then ( res => {
56+ } ) . then ( res => res . json ( ) ) . then ( res => {
57+ isPolling = false
58+
4759 if ( ! res . success ) {
4860 setData ( { status : 'error' , action : '' } )
4961
@@ -60,35 +72,39 @@ const CimoDownloadNotice = props => {
6072
6173 const _data = res . data
6274
63- if ( data . status !== _data . status ) {
64- setData ( _data )
75+ // Stop polling if it has reached 3 attempts, or plugin status indicates installation/activation is complete
76+ if ( pollOnce || pollCountRef . current >= 3 ||
77+ ( action === 'install' && ( _data . status === 'installed' || _data . status === 'activated' ) ) ||
78+ ( action === 'activate' && _data . status === 'activated' )
79+ ) {
80+ cimoData = _data
81+ setData ( cimoData )
6582
6683 // Update the global stackable.cimo status/action variables
6784 // so new image block selections reflect the latest Cimo installation state
6885 if ( typeof window !== 'undefined' && window . stackable ?. cimo ) {
6986 window . stackable . cimo . status = _data . status
7087 window . stackable . cimo . action = _data . action
7188 }
72- }
73-
74- // Stop polling if it has reached 3 attempts, or plugin status indicates installation/activation is complete
75- if ( pollOnce || pollCountRef . current >= 3 ||
76- ( action === 'install' && ( _data . status === 'installed' || _data . status === 'activated' ) ) ||
77- ( action === 'activate' && _data . status === 'activated' )
78- ) {
7989 return
8090 }
8191
8292 setTimeout ( ( ) => {
8393 pollStatus ( action )
8494 } , 3000 * pollCountRef . current )
85- } ) . catch ( e => {
95+ } ) . catch ( e => {
8696 // eslint-disable-next-line no-console
8797 console . error ( e . message )
88- } )
98+ } ) . finally ( ( ) => {
99+ isPolling = false
100+ } )
89101 }
90102
91103 useEffect ( ( ) => {
104+ if ( inMediaLibrary ) {
105+ return
106+ }
107+
92108 const _media = wp . media
93109 const old = _media . view . MediaFrame . Select
94110
@@ -100,16 +116,15 @@ const CimoDownloadNotice = props => {
100116
101117 this . on ( 'close' , ( ) => {
102118 pollCountRef . current = 0
103- if ( data . status === 'activated' ) {
104- return
119+ const action = ( cimoData . status === 'installing' ) ? 'install'
120+ : ( cimoData . status === 'activating' ) ? 'activate' : false
121+
122+ if ( action ) {
123+ setData ( cimoData )
124+ setTimeout ( ( ) => {
125+ pollStatus ( action , null )
126+ } , 1000 )
105127 }
106-
107- if ( data . status === 'not_installed' ) {
108- pollStatus ( 'install' , null , true )
109- return
110- }
111-
112- pollStatus ( 'activate' , null , true )
113128 } )
114129 } ,
115130 } )
@@ -120,12 +135,14 @@ const CimoDownloadNotice = props => {
120135 pollCountRef . current = 0
121136
122137 if ( data . status === 'not_installed' ) {
123- setData ( { status : 'installing' , action : '' } )
138+ cimoData = { status : 'installing' , action : '' }
139+ setData ( cimoData )
124140 pollStatus ( 'install' , e . currentTarget . href )
125141 return
126142 }
127143
128- setData ( { status : 'activating' , action : '' } )
144+ cimoData = { status : 'activating' , action : '' }
145+ setData ( cimoData )
129146 pollStatus ( 'activate' , e . currentTarget . href )
130147 }
131148
@@ -186,7 +203,7 @@ domReady( () => {
186203 }
187204 }
188205
189- createRoot ( noticeDiv ) . render ( < CimoDownloadNotice onDismiss = { onDismiss } /> )
206+ createRoot ( noticeDiv ) . render ( < CimoDownloadNotice onDismiss = { onDismiss } inMediaLibrary = { true } /> )
190207 details . insertAdjacentElement ( 'afterend' , noticeDiv )
191208 }
192209
0 commit comments