2022-11-15 19:19:58 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import {
|
2022-12-22 17:23:44 +00:00
|
|
|
EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME,
|
2022-11-15 19:19:58 +00:00
|
|
|
Product,
|
|
|
|
PRODUCTS_STORE_NAME,
|
|
|
|
WCDataSelector,
|
|
|
|
} from '@woocommerce/data';
|
2022-12-22 17:23:44 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
|
|
|
import { getNewPath } from '@woocommerce/navigation';
|
2023-02-23 17:48:49 +00:00
|
|
|
import {
|
|
|
|
getProductTitle,
|
|
|
|
getProductVariationTitle,
|
|
|
|
getTruncatedProductVariationTitle,
|
|
|
|
} from '@woocommerce/product-editor';
|
2022-11-15 19:19:58 +00:00
|
|
|
import { useFormContext } from '@woocommerce/components';
|
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-11-18 17:51:01 +00:00
|
|
|
import { ProductBreadcrumbs } from './product-breadcrumbs';
|
2022-11-16 16:03:39 +00:00
|
|
|
import { ProductStatusBadge } from './product-status-badge';
|
2022-11-15 19:19:58 +00:00
|
|
|
import { WooHeaderPageTitle } from '~/header/utils';
|
2022-11-24 14:00:20 +00:00
|
|
|
import './product-title.scss';
|
2022-11-15 19:19:58 +00:00
|
|
|
|
|
|
|
export const ProductTitle: React.FC = () => {
|
|
|
|
const { values } = useFormContext< Product >();
|
2022-12-22 17:23:44 +00:00
|
|
|
const { productId, variationId } = useParams();
|
|
|
|
const { isLoading, persistedName, productVariation } = useSelect(
|
|
|
|
( select: WCDataSelector ) => {
|
|
|
|
const {
|
|
|
|
getProduct,
|
|
|
|
hasFinishedResolution: hasProductFinishedResolution,
|
|
|
|
} = select( PRODUCTS_STORE_NAME );
|
|
|
|
const {
|
|
|
|
getProductVariation,
|
|
|
|
hasFinishedResolution: hasProductVariationFinishedResolution,
|
|
|
|
} = select( EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME );
|
|
|
|
const product = productId
|
|
|
|
? getProduct( parseInt( productId, 10 ) )
|
|
|
|
: null;
|
|
|
|
const variation =
|
|
|
|
variationId && productId
|
|
|
|
? getProductVariation( {
|
|
|
|
id: parseInt( variationId, 10 ),
|
|
|
|
product_id: parseInt( productId, 10 ),
|
|
|
|
} )
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const isProductLoading =
|
|
|
|
productId &&
|
|
|
|
! hasProductFinishedResolution( 'getProduct', [
|
2022-11-15 19:19:58 +00:00
|
|
|
parseInt( productId, 10 ),
|
2022-12-22 17:23:44 +00:00
|
|
|
] );
|
2022-11-15 19:19:58 +00:00
|
|
|
|
2022-12-22 17:23:44 +00:00
|
|
|
const isVariationLoading =
|
|
|
|
variationId &&
|
|
|
|
productId &&
|
|
|
|
! hasProductVariationFinishedResolution(
|
|
|
|
'getProductVariation',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
id: parseInt( variationId, 10 ),
|
|
|
|
product_id: parseInt( productId, 10 ),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
persistedName: product?.name,
|
|
|
|
productVariation: variation,
|
|
|
|
isLoading: isProductLoading || isVariationLoading,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const productTitle = getProductTitle(
|
|
|
|
values.name,
|
|
|
|
values.type,
|
|
|
|
persistedName
|
|
|
|
);
|
|
|
|
const productVariationTitle =
|
|
|
|
productVariation && getProductVariationTitle( productVariation );
|
2022-11-15 19:19:58 +00:00
|
|
|
|
2022-12-22 17:23:44 +00:00
|
|
|
const pageHierarchy = [
|
2022-11-18 17:51:01 +00:00
|
|
|
{
|
|
|
|
href: getAdminLink( 'edit.php?post_type=product' ),
|
|
|
|
title: __( 'Products', 'woocommerce' ),
|
|
|
|
},
|
2022-12-22 17:23:44 +00:00
|
|
|
{
|
|
|
|
href: getNewPath( {}, '/product/' + productId ),
|
|
|
|
type: 'wc-admin',
|
|
|
|
title: (
|
|
|
|
<>
|
|
|
|
{ productTitle }
|
|
|
|
<ProductStatusBadge />
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
productVariationTitle && {
|
|
|
|
title: (
|
|
|
|
<span title={ productVariationTitle }>
|
|
|
|
{ getTruncatedProductVariationTitle( productVariation ) }
|
|
|
|
</span>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
].filter( ( page ) => !! page ) as {
|
|
|
|
href: string;
|
|
|
|
title: string | JSX.Element;
|
|
|
|
}[];
|
|
|
|
|
|
|
|
const current = pageHierarchy.pop();
|
|
|
|
|
|
|
|
if ( isLoading ) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-11-15 19:19:58 +00:00
|
|
|
|
2022-11-16 16:03:39 +00:00
|
|
|
return (
|
|
|
|
<WooHeaderPageTitle>
|
2022-11-24 14:00:20 +00:00
|
|
|
<span className="woocommerce-product-title">
|
2022-12-22 17:23:44 +00:00
|
|
|
<ProductBreadcrumbs breadcrumbs={ pageHierarchy } />
|
2022-11-24 14:00:20 +00:00
|
|
|
<span className="woocommerce-product-title__wrapper">
|
2022-12-22 17:23:44 +00:00
|
|
|
{ current?.title }
|
2022-11-24 14:00:20 +00:00
|
|
|
</span>
|
|
|
|
</span>
|
2022-11-16 16:03:39 +00:00
|
|
|
</WooHeaderPageTitle>
|
|
|
|
);
|
2022-11-15 19:19:58 +00:00
|
|
|
};
|