File size: 1,223 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 |
import { recordTracksEvent } from '@automattic/calypso-analytics';
import { localizeUrl } from '@automattic/i18n-utils';
import { useQuery } from '@tanstack/react-query';
import apiFetch from '@wordpress/api-fetch';
import wpcomRequest, { canAccessWpcomApis } from 'wpcom-proxy-request';
import { useHelpCenterContext } from '../contexts/HelpCenterContext';
import type { PostObject } from '../types';
export function usePostByUrl( url: string ) {
const { sectionName } = useHelpCenterContext();
const postUrl = encodeURIComponent( localizeUrl( url ) );
return useQuery< PostObject >( {
queryKey: [ 'support-status', url ],
queryFn: () =>
canAccessWpcomApis()
? wpcomRequest( {
path: `/help/article?post_url=${ postUrl }`,
apiNamespace: 'wpcom/v2',
} )
: apiFetch( {
path: `/help-center/fetch-post?post_url=${ postUrl }`,
} ),
enabled: !! url,
refetchOnWindowFocus: false,
staleTime: 12 * 3600, // 12 hours
throwOnError: () => {
const tracksData = {
force_site_id: true,
location: 'help-center',
section: sectionName,
post_url: url,
};
recordTracksEvent( 'calypso_helpcenter_post_by_url_error', tracksData );
return false;
},
} );
}
|