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';
|
2018-07-09 15:46:31 +00:00
|
|
|
import classnames from 'classnames';
|
2018-07-23 20:14:40 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2020-08-27 01:46:53 +00:00
|
|
|
import { useUserPreferences } from '@woocommerce/data';
|
2020-10-06 20:42:46 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2021-01-07 23:57:09 +00:00
|
|
|
import { Text } from '@woocommerce/experimental';
|
2018-11-05 21:02:04 +00:00
|
|
|
|
2018-05-10 18:35:55 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2018-09-21 15:19:05 +00:00
|
|
|
import ActivityPanel from './activity-panel';
|
2020-08-27 01:46:53 +00:00
|
|
|
import { MobileAppBanner } from '../mobile-banner';
|
2020-11-03 00:57:49 +00:00
|
|
|
import useIsScrolled from '../hooks/useIsScrolled';
|
2021-02-12 16:32:26 +00:00
|
|
|
import Navigation from '../navigation';
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
export const Header = ( { sections, isEmbedded = false, query } ) => {
|
|
|
|
const headerElement = useRef( null );
|
|
|
|
const siteTitle = getSetting( 'siteTitle', '' );
|
2020-10-06 20:42:46 +00:00
|
|
|
const pageTitle = sections.slice( -1 )[ 0 ];
|
2020-11-03 00:57:49 +00:00
|
|
|
const isScrolled = useIsScrolled();
|
2020-08-27 01:46:53 +00:00
|
|
|
const { updateUserPreferences, ...userData } = useUserPreferences();
|
|
|
|
const isModalDismissed = userData.android_app_banner_dismissed === 'yes';
|
2021-02-12 16:32:26 +00:00
|
|
|
let debounceTimer = null;
|
2020-01-20 17:34:24 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
const className = classnames( 'woocommerce-layout__header', {
|
|
|
|
'is-scrolled': isScrolled,
|
|
|
|
} );
|
2020-01-20 17:34:24 +00:00
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
}, [ isModalDismissed ] );
|
|
|
|
|
|
|
|
const updateBodyMargin = () => {
|
|
|
|
clearTimeout( debounceTimer );
|
|
|
|
debounceTimer = setTimeout( function () {
|
|
|
|
const wpBody = document.querySelector( '#wpbody' );
|
|
|
|
|
2021-02-12 21:42:04 +00:00
|
|
|
if ( ! wpBody || ! headerElement.current ) {
|
2021-02-12 16:32:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wpBody.style.marginTop = `${ headerElement.current.offsetHeight }px`;
|
|
|
|
}, 200 );
|
|
|
|
};
|
|
|
|
|
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',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
documentTitle,
|
|
|
|
siteTitle
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( document.title !== decodedTitle ) {
|
|
|
|
document.title = decodedTitle;
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 01:46:53 +00:00
|
|
|
}, [ isEmbedded, sections, siteTitle ] );
|
|
|
|
|
|
|
|
const dismissHandler = () => {
|
|
|
|
updateUserPreferences( {
|
|
|
|
android_app_banner_dismissed: 'yes',
|
|
|
|
} );
|
|
|
|
};
|
2020-08-23 22:46:18 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={ className } ref={ headerElement }>
|
2020-08-27 01:46:53 +00:00
|
|
|
{ ! isModalDismissed && (
|
|
|
|
<MobileAppBanner
|
|
|
|
onDismiss={ dismissHandler }
|
|
|
|
onInstall={ dismissHandler }
|
|
|
|
/>
|
|
|
|
) }
|
2020-10-06 20:42:46 +00:00
|
|
|
|
2021-02-12 16:32:26 +00:00
|
|
|
<div className="woocommerce-layout__header-wrapper">
|
|
|
|
{ window.wcAdminFeatures.navigation && <Navigation /> }
|
|
|
|
|
|
|
|
<Text
|
|
|
|
className="woocommerce-layout__header-heading"
|
|
|
|
as="h1"
|
|
|
|
variant="subtitle.small"
|
|
|
|
>
|
|
|
|
{ decodeEntities( pageTitle ) }
|
|
|
|
</Text>
|
|
|
|
|
|
|
|
{ window.wcAdminFeatures[ 'activity-panels' ] && (
|
|
|
|
<ActivityPanel
|
|
|
|
isEmbedded={ isEmbedded }
|
|
|
|
query={ query }
|
|
|
|
userPreferencesData={ {
|
|
|
|
...userData,
|
|
|
|
updateUserPreferences,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</div>
|
2020-08-23 22:46:18 +00:00
|
|
|
</div>
|
|
|
|
);
|
2018-05-22 15:19:56 +00:00
|
|
|
};
|