File size: 4,172 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
import { useQuery, keepPreviousData } from '@tanstack/react-query';
import { useI18n } from '@wordpress/react-i18n';
import { addQueryArgs } from '@wordpress/url';
import { useCallback, useMemo } from 'react';
import wpcomRequest from 'wpcom-proxy-request';
import { useSiteSettings } from 'calypso/blocks/plugins-scheduled-updates/hooks/use-site-settings';
import { useDispatch, useSelector } from 'calypso/state';
import { saveSiteSettings } from 'calypso/state/site-settings/actions';
import { getSelectedSite } from 'calypso/state/ui/selectors';
import { isValidURL } from '../utils';
interface SitePage {
id: number;
link: string;
title: { rendered: string };
wpcom_performance_report_url: string;
}
const FIELDS_TO_RETRIEVE = [ 'id', 'link', 'title', 'wpcom_performance_report_url' ];
const getPages = ( siteId: number, query = '' ) => {
return wpcomRequest< SitePage[] >( {
path: addQueryArgs( `/sites/${ siteId }/pages`, {
per_page: 20,
search: query,
page: 1,
status: 'publish',
_fields: FIELDS_TO_RETRIEVE,
} ),
method: 'GET',
apiNamespace: 'wp/v2',
} );
};
const savePageMeta = ( siteId: number, pageId: number, url: string ) => {
return wpcomRequest< SitePage >( {
path: addQueryArgs( `/sites/${ siteId }/pages/${ pageId }`, {
_fields: FIELDS_TO_RETRIEVE,
} ),
method: 'POST',
apiNamespace: 'wp/v2',
body: {
wpcom_performance_report_url: url,
},
} );
};
interface PerformanceReportUrl {
url: string;
hash: string;
}
const toPerformanceReportParts = (
pageUrl: string,
performanceReportUrl: string
): PerformanceReportUrl => {
const [ url, hash ] = performanceReportUrl.split( '&hash=' );
if ( ! url || ! hash ) {
return { url: pageUrl, hash: '' };
}
return {
url,
hash,
};
};
const toPerformanceReportUrl = ( { url, hash }: PerformanceReportUrl ) => {
return `${ url }&hash=${ hash }`;
};
const HOME_PAGE_ID = '0';
export const useSitePerformancePageReports = ( { query = '' } = {} ) => {
const { __ } = useI18n();
const site = useSelector( getSelectedSite );
const siteId = site?.ID;
const {
data,
isLoading: isInitialLoading,
refetch,
} = useQuery( {
queryKey: [ 'useSitePerformancePageReports', siteId, query ],
queryFn: () => getPages( siteId!, query ),
refetchOnWindowFocus: false,
enabled: !! siteId,
placeholderData: keepPreviousData,
select: ( data ) => {
return data.map( ( page ) => {
let path = page.link.replace( site?.URL ?? '', '' );
path = path.length > 1 ? path.replace( /\/$/, '' ) : path;
return {
url: page.link,
path,
label: page.title.rendered || __( 'No Title' ),
value: page.id.toString(),
wpcom_performance_report_url: toPerformanceReportParts(
page.link,
page.wpcom_performance_report_url
),
};
} );
},
meta: {
persist: false,
},
} );
const { getSiteSetting } = useSiteSettings( site?.slug );
const homePagePerformanceUrl: SitePage[ 'wpcom_performance_report_url' ] = getSiteSetting(
'wpcom_performance_report_url'
);
const pages = useMemo( () => {
if ( ! site?.URL ) {
return [];
}
if ( ! query ) {
return [
{
url: site?.URL,
path: '/',
label: __( 'Home' ),
value: HOME_PAGE_ID,
wpcom_performance_report_url: toPerformanceReportParts(
site.URL,
homePagePerformanceUrl
),
},
...( data ?? [] ),
];
}
return data ?? [];
}, [ query, data, site?.URL, __, homePagePerformanceUrl ] );
const dispatch = useDispatch();
const savePerformanceReportUrl = useCallback(
async ( pageId: string, performanceReport: PerformanceReportUrl ) => {
if ( ! siteId ) {
return;
}
const performanceReportUrl = toPerformanceReportUrl( performanceReport );
if ( ! isValidURL( performanceReportUrl ) ) {
return;
}
if ( pageId === HOME_PAGE_ID ) {
return await dispatch(
saveSiteSettings( siteId, { wpcom_performance_report_url: performanceReportUrl } )
);
}
return await savePageMeta( siteId, parseInt( pageId, 10 ), performanceReportUrl );
},
[ siteId, dispatch ]
);
return { pages, isInitialLoading, savePerformanceReportUrl, refetch };
};
|