Skip to content

Commit

Permalink
Add credentials status polling
Browse files Browse the repository at this point in the history
  • Loading branch information
dkmyta committed Dec 19, 2024
1 parent 204bf12 commit 244ff4c
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import {
} from '@automattic/jetpack-components';
import { useConnection } from '@automattic/jetpack-connection';
import { type Threat } from '@automattic/jetpack-scan';
import { useQueryClient } from '@tanstack/react-query';
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 { useCallback, useState, useMemo, useEffect } from 'react';
import AdminSectionHero from '../../components/admin-section-hero';
import ErrorAdminSectionHero from '../../components/error-admin-section-hero';
import OnboardingPopover from '../../components/onboarding-popover';
import { QUERY_CREDENTIALS_KEY } from '../../constants';
import useIgnoreThreatMutation from '../../data/scan/use-ignore-threat-mutation';
import useScanStatusQuery, { isScanInProgress } from '../../data/scan/use-scan-status-query';
import useUnIgnoreThreatMutation from '../../data/scan/use-unignore-threat-mutation';
Expand All @@ -32,6 +34,7 @@ const ScanAdminSectionHero: React.FC = ( { size = 'normal' }: { size?: 'normal'
const [ isSm ] = useBreakpointMatch( 'sm' );
const { data: status } = useScanStatusQuery();
const { isThreatFixInProgress, isThreatFixStale } = useFixers();
const queryClient = useQueryClient();

const getScan = useCallback( () => {
recordEvent( 'jetpack_protect_scan_header_get_scan_link_click' );
Expand Down Expand Up @@ -134,6 +137,22 @@ const ScanAdminSectionHero: React.FC = ( { size = 'normal' }: { size?: 'normal'
setShowModal( ! showModal );
}, [ showModal ] );

/**
* Poll credentials as long as the modal is open.
*/
useEffect( () => {
if ( ! showModal ) {
return;
}
const interval = setInterval( () => {
if ( ! credentials || credentials.length === 0 ) {
queryClient.invalidateQueries( { queryKey: [ QUERY_CREDENTIALS_KEY ] } );
}
}, 5_000 );

return () => clearInterval( interval );
}, [ showModal, queryClient, credentials ] );

if ( scanning ) {
return <ScanningAdminSectionHero size={ size } />;
}
Expand Down

0 comments on commit 244ff4c

Please sign in to comment.