1
- import React , { useMemo , useEffect } from 'react' ;
1
+ import React , { useMemo , useEffect , useCallback , useRef } from 'react' ;
2
2
import { connect } from 'react-redux' ;
3
3
import { withPreferences } from 'compass-preferences-model/provider' ;
4
4
import { useWorkspaceTabId } from '@mongodb-js/compass-workspaces/provider' ;
@@ -27,6 +27,7 @@ import {
27
27
unhideIndex ,
28
28
startPollingRegularIndexes ,
29
29
stopPollingRegularIndexes ,
30
+ trackIndexProgress ,
30
31
} from '../../modules/regular-indexes' ;
31
32
32
33
import type {
@@ -45,6 +46,7 @@ type RegularIndexesTableProps = {
45
46
onUnhideIndexClick : ( name : string ) => void ;
46
47
onDeleteIndexClick : ( name : string ) => void ;
47
48
onDeleteFailedIndexClick : ( name : string ) => void ;
49
+ onTrackIndexProgress : ( indexId : string , indexName : string ) => Promise < void > ;
48
50
readOnly ?: boolean ;
49
51
error ?: string | null ;
50
52
onRegularIndexesOpened : ( tabId : string ) => void ;
@@ -286,10 +288,15 @@ function getInProgressIndexInfo(
286
288
index : MappedInProgressIndex ,
287
289
{
288
290
onDeleteFailedIndexClick,
291
+ onDeleteIndexClick,
289
292
} : {
290
293
onDeleteFailedIndexClick : ( indexName : string ) => void ;
294
+ onDeleteIndexClick : ( indexName : string ) => void ;
291
295
}
292
296
) : CommonIndexInfo {
297
+ // Use progress directly from Redux state
298
+ const progressToUse = index . progressPercentage ;
299
+
293
300
return {
294
301
id : index . id ,
295
302
name : index . name ,
@@ -302,8 +309,9 @@ function getInProgressIndexInfo(
302
309
status : < StatusField status = { index . status } error = { index . error } /> ,
303
310
actions : (
304
311
< InProgressIndexActions
305
- index = { index }
312
+ index = { { ... index , progressPercentage : progressToUse } }
306
313
onDeleteFailedIndexClick = { onDeleteFailedIndexClick }
314
+ onDeleteIndexClick = { onDeleteIndexClick }
307
315
> </ InProgressIndexActions >
308
316
) ,
309
317
} ;
@@ -381,12 +389,74 @@ export const RegularIndexesTable: React.FunctionComponent<
381
389
onUnhideIndexClick,
382
390
onDeleteIndexClick,
383
391
onDeleteFailedIndexClick,
392
+ onTrackIndexProgress,
384
393
onRegularIndexesOpened,
385
394
onRegularIndexesClosed,
386
395
error,
387
396
} ) => {
388
397
const tabId = useWorkspaceTabId ( ) ;
389
398
399
+ // Use Redux state for progress tracking instead of local state
400
+ const intervalsRef = useRef < Record < string , NodeJS . Timeout > > ( { } ) ;
401
+
402
+ // Function to start progress tracking for an index
403
+ const startProgressTracking = useCallback (
404
+ ( indexId : string , indexName : string ) => {
405
+ // Clear any existing interval for this index
406
+ if ( intervalsRef . current [ indexId ] ) {
407
+ clearInterval ( intervalsRef . current [ indexId ] ) ;
408
+ }
409
+
410
+ // Start interval to check real progress every 2 seconds
411
+ intervalsRef . current [ indexId ] = setInterval ( ( ) => {
412
+ // Track real progress using the Redux action
413
+ onTrackIndexProgress ( indexId , indexName ) . catch ( ( ) => {
414
+ // If real tracking fails, the error is already handled in the action
415
+ // No fallback needed here since we want to rely on real progress only
416
+ } ) ;
417
+ } , 2000 ) ; // 2 second intervals for real progress checking
418
+ } ,
419
+ [ onTrackIndexProgress ]
420
+ ) ;
421
+
422
+ // Function to stop progress tracking for an index
423
+ const stopProgressTracking = useCallback ( ( indexId : string ) => {
424
+ if ( intervalsRef . current [ indexId ] ) {
425
+ clearInterval ( intervalsRef . current [ indexId ] ) ;
426
+ delete intervalsRef . current [ indexId ] ;
427
+ }
428
+ } , [ ] ) ;
429
+
430
+ // Manage progress tracking based on inProgressIndexes changes
431
+ useEffect ( ( ) => {
432
+ const currentInProgressIds = new Set (
433
+ inProgressIndexes . map ( ( index ) => index . id )
434
+ ) ;
435
+ const trackedIds = new Set ( Object . keys ( intervalsRef . current ) ) ;
436
+
437
+ // Start tracking for new in-progress indexes
438
+ inProgressIndexes . forEach ( ( index ) => {
439
+ if ( index . status === 'inprogress' && ! trackedIds . has ( index . id ) ) {
440
+ startProgressTracking ( index . id , index . name ) ;
441
+ }
442
+ } ) ;
443
+
444
+ // Stop tracking for indexes that are no longer in progress
445
+ trackedIds . forEach ( ( indexId ) => {
446
+ if ( ! currentInProgressIds . has ( indexId ) ) {
447
+ stopProgressTracking ( indexId ) ;
448
+ }
449
+ } ) ;
450
+ } , [ inProgressIndexes , startProgressTracking , stopProgressTracking ] ) ;
451
+
452
+ // Cleanup intervals on unmount
453
+ useEffect ( ( ) => {
454
+ const currentIntervals = intervalsRef . current ;
455
+ return ( ) => {
456
+ Object . values ( currentIntervals ) . forEach ( clearInterval ) ;
457
+ } ;
458
+ } , [ ] ) ;
459
+
390
460
useEffect ( ( ) => {
391
461
onRegularIndexesOpened ( tabId ) ;
392
462
return ( ) => {
@@ -407,6 +477,8 @@ export const RegularIndexesTable: React.FunctionComponent<
407
477
if ( index . compassIndexType === 'in-progress-index' ) {
408
478
indexData = getInProgressIndexInfo ( index , {
409
479
onDeleteFailedIndexClick,
480
+ onDeleteIndexClick,
481
+ // Remove currentProgress since we're using Redux state directly
410
482
} ) ;
411
483
} else if ( index . compassIndexType === 'rolling-index' ) {
412
484
indexData = getRollingIndexInfo ( index ) ;
@@ -479,6 +551,7 @@ const mapDispatch = {
479
551
onUnhideIndexClick : unhideIndex ,
480
552
onRegularIndexesOpened : startPollingRegularIndexes ,
481
553
onRegularIndexesClosed : stopPollingRegularIndexes ,
554
+ onTrackIndexProgress : trackIndexProgress ,
482
555
} ;
483
556
484
557
export default connect (
0 commit comments