File size: 887 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp';

interface ApiResponse {
	migration_key: string;
}

const getMigrationKey = async ( siteId: number ): Promise< ApiResponse > => {
	return wpcom.req.get(
		`/sites/${ siteId }/atomic-migration-status/wpcom-migration-key?http_envelope=1`,
		{
			apiNamespace: 'wpcom/v2',
		}
	);
};

type Options = {
	enabled?: UseQueryOptions[ 'enabled' ];
	retry?: UseQueryOptions[ 'retry' ];
};

export const useSiteMigrationKey = ( siteId?: number, options?: Options ) => {
	return useQuery( {
		queryKey: [ 'site-migration-key', siteId ],
		queryFn: () => getMigrationKey( siteId! ),
		retry: options?.retry ?? false,
		retryDelay: 5000,
		enabled: !! siteId && ( options?.enabled ?? true ),
		select: ( data ) => ( { migrationKey: data?.migration_key } ),
		refetchOnWindowFocus: false,
	} );
};