2018-05-10 18:35:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-05-24 16:03:03 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2021-02-12 16:32:26 +00:00
|
|
|
import { useEffect, useLayoutEffect, useRef } from '@wordpress/element';
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2018-07-23 20:14:40 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2023-03-10 14:58:02 +00:00
|
|
|
import {
|
|
|
|
WC_HEADER_SLOT_NAME,
|
|
|
|
WC_HEADER_PAGE_TITLE_SLOT_NAME,
|
|
|
|
WooHeaderNavigationItem,
|
|
|
|
WooHeaderItem,
|
|
|
|
WooHeaderPageTitle,
|
|
|
|
} from '@woocommerce/admin-layout';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2021-12-14 16:56:42 +00:00
|
|
|
import { Text, useSlot } from '@woocommerce/experimental';
|
2024-03-20 03:26:17 +00:00
|
|
|
import { getScreenFromPath, isWCAdmin } from '@woocommerce/navigation';
|
2018-11-05 21:02:04 +00:00
|
|
|
|
2018-05-10 18:35:55 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2020-11-03 00:57:49 +00:00
|
|
|
import useIsScrolled from '../hooks/useIsScrolled';
|
2023-05-17 01:54:31 +00:00
|
|
|
import { TasksReminderBar, useActiveSetupTasklist } from '../task-lists';
|
2024-03-11 23:50:22 +00:00
|
|
|
import {
|
|
|
|
LaunchYourStoreStatus,
|
|
|
|
useLaunchYourStore,
|
|
|
|
} from '../launch-your-store';
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2021-12-14 16:56:42 +00:00
|
|
|
export const PAGE_TITLE_FILTER = 'woocommerce_admin_header_page_title';
|
2021-03-03 00:05:08 +00:00
|
|
|
|
2024-04-15 02:46:09 +00:00
|
|
|
export const getPageTitle = ( sections ) => {
|
|
|
|
let pageTitle;
|
|
|
|
const pagesWithTabs = [ 'Settings', 'Reports', 'Status' ];
|
|
|
|
|
|
|
|
if (
|
|
|
|
sections.length > 2 &&
|
|
|
|
Array.isArray( sections[ 1 ] ) &&
|
|
|
|
pagesWithTabs.includes( sections[ 1 ][ 1 ] )
|
|
|
|
) {
|
|
|
|
pageTitle = sections[ 1 ][ 1 ];
|
|
|
|
} else {
|
|
|
|
pageTitle = sections[ sections.length - 1 ];
|
|
|
|
}
|
|
|
|
return pageTitle;
|
|
|
|
};
|
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
export const Header = ( { sections, isEmbedded = false, query } ) => {
|
|
|
|
const headerElement = useRef( null );
|
2022-06-01 17:10:20 +00:00
|
|
|
const activeSetupList = useActiveSetupTasklist();
|
2020-08-23 22:46:18 +00:00
|
|
|
const siteTitle = getSetting( 'siteTitle', '' );
|
2024-04-15 02:46:09 +00:00
|
|
|
const pageTitle = getPageTitle( sections );
|
2023-10-12 14:56:48 +00:00
|
|
|
const { isScrolled } = useIsScrolled();
|
2021-02-12 16:32:26 +00:00
|
|
|
let debounceTimer = null;
|
2024-05-31 03:49:36 +00:00
|
|
|
const className = clsx( 'woocommerce-layout__header', {
|
2020-08-23 22:46:18 +00:00
|
|
|
'is-scrolled': isScrolled,
|
|
|
|
} );
|
2020-01-20 17:34:24 +00:00
|
|
|
|
2023-03-10 14:58:02 +00:00
|
|
|
const pageTitleSlot = useSlot( WC_HEADER_PAGE_TITLE_SLOT_NAME );
|
2021-12-14 16:56:42 +00:00
|
|
|
const hasPageTitleFills = Boolean( pageTitleSlot?.fills?.length );
|
2023-03-10 14:58:02 +00:00
|
|
|
const headerItemSlot = useSlot( WC_HEADER_SLOT_NAME );
|
2021-12-14 16:56:42 +00:00
|
|
|
const headerItemSlotFills = headerItemSlot?.fills;
|
|
|
|
|
2023-03-10 14:58:02 +00:00
|
|
|
const updateBodyMargin = () => {
|
|
|
|
clearTimeout( debounceTimer );
|
|
|
|
debounceTimer = setTimeout( function () {
|
|
|
|
const wpBody = document.querySelector( '#wpbody' );
|
|
|
|
|
|
|
|
if ( ! wpBody || ! headerElement.current ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-21 16:43:50 +00:00
|
|
|
wpBody.style.marginTop = `${ headerElement.current.clientHeight }px`;
|
2023-03-10 14:58:02 +00:00
|
|
|
}, 200 );
|
|
|
|
};
|
|
|
|
|
2021-02-12 16:32:26 +00:00
|
|
|
useLayoutEffect( () => {
|
|
|
|
updateBodyMargin();
|
|
|
|
window.addEventListener( 'resize', updateBodyMargin );
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener( 'resize', updateBodyMargin );
|
|
|
|
const wpBody = document.querySelector( '#wpbody' );
|
|
|
|
|
|
|
|
if ( ! wpBody ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wpBody.style.marginTop = null;
|
|
|
|
};
|
2021-12-14 16:56:42 +00:00
|
|
|
}, [ headerItemSlotFills ] );
|
2021-02-12 16:32:26 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( ! isEmbedded ) {
|
2020-08-27 01:46:53 +00:00
|
|
|
const documentTitle = sections
|
2020-08-23 22:46:18 +00:00
|
|
|
.map( ( section ) => {
|
|
|
|
return Array.isArray( section ) ? section[ 1 ] : section;
|
|
|
|
} )
|
|
|
|
.reverse()
|
|
|
|
.join( ' ‹ ' );
|
|
|
|
|
|
|
|
const decodedTitle = decodeEntities(
|
|
|
|
sprintf(
|
|
|
|
/* translators: 1: document title. 2: page title */
|
|
|
|
__(
|
|
|
|
'%1$s ‹ %2$s — WooCommerce',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-08-23 22:46:18 +00:00
|
|
|
),
|
|
|
|
documentTitle,
|
|
|
|
siteTitle
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( document.title !== decodedTitle ) {
|
|
|
|
document.title = decodedTitle;
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 01:46:53 +00:00
|
|
|
}, [ isEmbedded, sections, siteTitle ] );
|
|
|
|
|
2024-04-09 14:34:37 +00:00
|
|
|
const isHomescreen =
|
|
|
|
isWCAdmin() && getScreenFromPath() === 'homescreen' && ! query.task;
|
2024-03-18 09:43:02 +00:00
|
|
|
const { isLoading, launchYourStoreEnabled, comingSoon, storePagesOnly } =
|
2024-06-11 05:28:05 +00:00
|
|
|
useLaunchYourStore( {
|
|
|
|
enabled: isHomescreen,
|
|
|
|
} );
|
2024-03-20 03:26:17 +00:00
|
|
|
const showLaunchYourStoreStatus =
|
|
|
|
isHomescreen && launchYourStoreEnabled && ! isLoading;
|
2024-03-11 23:50:22 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
return (
|
|
|
|
<div className={ className } ref={ headerElement }>
|
2022-06-01 17:10:20 +00:00
|
|
|
{ activeSetupList && (
|
2022-03-22 15:28:58 +00:00
|
|
|
<TasksReminderBar
|
|
|
|
updateBodyMargin={ updateBodyMargin }
|
2022-06-01 17:10:20 +00:00
|
|
|
taskListId={ activeSetupList }
|
2022-03-22 15:28:58 +00:00
|
|
|
/>
|
|
|
|
) }
|
2021-12-14 16:56:42 +00:00
|
|
|
<div className="woocommerce-layout__header-wrapper">
|
|
|
|
<WooHeaderNavigationItem.Slot
|
|
|
|
fillProps={ { isEmbedded, query } }
|
2020-08-27 01:46:53 +00:00
|
|
|
/>
|
2020-10-06 20:42:46 +00:00
|
|
|
|
2021-02-12 16:32:26 +00:00
|
|
|
<Text
|
2024-03-11 23:50:22 +00:00
|
|
|
className={ `woocommerce-layout__header-heading ${
|
|
|
|
showLaunchYourStoreStatus
|
|
|
|
? ''
|
|
|
|
: 'woocommerce-layout__header-left-align'
|
|
|
|
}` }
|
2021-02-12 16:32:26 +00:00
|
|
|
as="h1"
|
|
|
|
>
|
2021-12-14 16:56:42 +00:00
|
|
|
{ decodeEntities(
|
|
|
|
hasPageTitleFills ? (
|
|
|
|
<WooHeaderPageTitle.Slot
|
|
|
|
fillProps={ { isEmbedded, query } }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
pageTitle
|
|
|
|
)
|
|
|
|
) }
|
2021-02-12 16:32:26 +00:00
|
|
|
</Text>
|
|
|
|
|
2024-03-11 23:50:22 +00:00
|
|
|
{ showLaunchYourStoreStatus && (
|
2024-03-18 09:43:02 +00:00
|
|
|
<LaunchYourStoreStatus
|
|
|
|
comingSoon={ comingSoon }
|
|
|
|
storePagesOnly={ storePagesOnly }
|
|
|
|
/>
|
2024-03-11 23:50:22 +00:00
|
|
|
) }
|
|
|
|
|
2021-12-14 16:56:42 +00:00
|
|
|
<WooHeaderItem.Slot fillProps={ { isEmbedded, query } } />
|
2021-02-12 16:32:26 +00:00
|
|
|
</div>
|
2020-08-23 22:46:18 +00:00
|
|
|
</div>
|
|
|
|
);
|
2018-05-22 15:19:56 +00:00
|
|
|
};
|