File size: 832 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
import { useQuery } from '@tanstack/react-query';
import wp from 'calypso/lib/wp';
import { UrlPerformanceMetricsQueryResponse } from './types';

function mapResult( response: UrlPerformanceMetricsQueryResponse ) {
	return response.webtestpage_org.report;
}

export const useUrlPerformanceMetricsQuery = ( url?: string, hash?: string ) => {
	return useQuery( {
		queryKey: [ 'url', 'performance', url, hash ],
		queryFn: () =>
			wp.req.get(
				{
					path: '/site-profiler/metrics/advanced/performance',
					apiNamespace: 'wpcom/v2',
				},
				{ url, hash }
			),
		meta: {
			persist: false,
		},
		select: mapResult,
		enabled: !! url && !! hash,
		retry: false,
		refetchOnWindowFocus: false,
		refetchInterval: ( query ) =>
			query.state.data?.webtestpage_org?.status === 'completed' ? false : 5000, // 5 second	;
	} );
};