2024-04-02 12:12:20 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { store as editorStore } from '@wordpress/editor';
|
|
|
|
|
|
|
|
enum Locations {
|
|
|
|
SINGLE_PRODUCT = 'single-product',
|
|
|
|
PRODUCT_CATALOG = 'product-catalog',
|
|
|
|
PRODUCT_ARCHIVE = 'product-archive',
|
|
|
|
ORDER_CONFIRMATION = 'order-confirmation',
|
|
|
|
CART = 'cart',
|
|
|
|
CHECKOUT = 'checkout',
|
|
|
|
POST = 'post',
|
|
|
|
PAGE = 'page',
|
|
|
|
OTHER = 'other',
|
|
|
|
}
|
|
|
|
const templateSlugToTemplateMap: {
|
|
|
|
[ key: string ]: Locations | undefined;
|
|
|
|
} = {
|
|
|
|
'single-product': Locations.SINGLE_PRODUCT,
|
|
|
|
'archive-product': Locations.PRODUCT_CATALOG,
|
|
|
|
'taxonomy-product_cat': Locations.PRODUCT_ARCHIVE,
|
|
|
|
'taxonomy-product_tag': Locations.PRODUCT_ARCHIVE,
|
|
|
|
'taxonomy-product_attribute': Locations.PRODUCT_ARCHIVE,
|
|
|
|
'product-search-results': Locations.PRODUCT_ARCHIVE,
|
|
|
|
'order-confirmation': Locations.ORDER_CONFIRMATION,
|
|
|
|
'page-cart': Locations.CART,
|
|
|
|
'page-checkout': Locations.CHECKOUT,
|
|
|
|
};
|
|
|
|
|
2024-05-16 05:47:52 +00:00
|
|
|
export const useTracksLocation = ( templateSlug: string | undefined ) => {
|
2024-04-02 12:12:20 +00:00
|
|
|
const postType = useSelect( ( select ) => {
|
|
|
|
// @ts-expect-error Type definitions are missing
|
|
|
|
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/wordpress__blocks/store/selectors.d.ts
|
|
|
|
return select( editorStore ).getCurrentPostType();
|
|
|
|
}, [] );
|
|
|
|
|
2024-05-14 07:45:33 +00:00
|
|
|
if ( postType === Locations.PAGE || postType === Locations.POST ) {
|
|
|
|
return postType;
|
|
|
|
}
|
2024-04-02 12:12:20 +00:00
|
|
|
|
2024-05-16 05:47:52 +00:00
|
|
|
if ( ! templateSlug ) {
|
|
|
|
return Locations.OTHER;
|
|
|
|
}
|
|
|
|
|
2024-05-14 07:45:33 +00:00
|
|
|
const template = templateSlugToTemplateMap[ templateSlug ];
|
2024-04-02 12:12:20 +00:00
|
|
|
|
2024-05-14 07:45:33 +00:00
|
|
|
if ( template ) {
|
|
|
|
return template;
|
|
|
|
}
|
2024-04-02 12:12:20 +00:00
|
|
|
|
2024-05-14 07:45:33 +00:00
|
|
|
if ( templateSlug.includes( 'single-product' ) ) {
|
|
|
|
return Locations.SINGLE_PRODUCT;
|
2024-04-02 12:12:20 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 07:45:33 +00:00
|
|
|
if (
|
|
|
|
templateSlug.includes( 'taxonomy-product_cat' ) ||
|
|
|
|
templateSlug.includes( 'taxonomy-product_tag' )
|
|
|
|
) {
|
|
|
|
return Locations.PRODUCT_ARCHIVE;
|
2024-04-02 12:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Locations.OTHER;
|
|
|
|
};
|