2018-05-10 18:35:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-05-24 16:03:03 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2020-08-23 22:46:18 +00:00
|
|
|
import { useEffect, useRef, useState } 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';
|
2018-11-05 21:02:04 +00:00
|
|
|
import { Link } from '@woocommerce/components';
|
2020-08-27 01:46:53 +00:00
|
|
|
import { useUserPreferences } from '@woocommerce/data';
|
|
|
|
import { getNewPath } from '@woocommerce/navigation';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2020-08-27 01:46:53 +00:00
|
|
|
import { getAdminLink, getSetting } from '@woocommerce/wc-admin-settings';
|
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';
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
const trackLinkClick = ( event ) => {
|
|
|
|
const target = event.target.closest( 'a' );
|
|
|
|
const href = target.getAttribute( 'href' );
|
2019-07-09 19:10:49 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
if ( href ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
recordEvent( 'navbar_breadcrumb_click', {
|
|
|
|
href,
|
2020-08-23 22:46:18 +00:00
|
|
|
text: target.innerText,
|
2020-02-14 02:23:21 +00:00
|
|
|
} );
|
2019-07-09 19:10:49 +00:00
|
|
|
}
|
2020-08-23 22:46:18 +00:00
|
|
|
};
|
2019-07-09 19:10:49 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
export const Header = ( { sections, isEmbedded = false, query } ) => {
|
|
|
|
const headerElement = useRef( null );
|
|
|
|
const rafHandle = useRef( null );
|
|
|
|
const threshold = useRef( null );
|
|
|
|
const siteTitle = getSetting( 'siteTitle', '' );
|
|
|
|
const [ isScrolled, setIsScrolled ] = useState( false );
|
2020-08-27 01:46:53 +00:00
|
|
|
const { updateUserPreferences, ...userData } = useUserPreferences();
|
|
|
|
|
|
|
|
const isModalDismissed = userData.android_app_banner_dismissed === 'yes';
|
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
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
useEffect( () => {
|
|
|
|
threshold.current = headerElement.current.offsetTop;
|
2018-07-09 15:46:31 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
const updateIsScrolled = () => {
|
|
|
|
setIsScrolled( window.pageYOffset > threshold.current - 20 );
|
|
|
|
};
|
2018-07-09 15:46:31 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
const scrollListener = () => {
|
|
|
|
rafHandle.current = window.requestAnimationFrame(
|
|
|
|
updateIsScrolled
|
|
|
|
);
|
|
|
|
};
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
window.addEventListener( 'scroll', scrollListener );
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
return () => {
|
|
|
|
window.removeEventListener( 'scroll', scrollListener );
|
|
|
|
window.cancelAnimationFrame( rafHandle.current );
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
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-08-23 22:46:18 +00:00
|
|
|
<h1 className="woocommerce-layout__header-breadcrumbs">
|
2020-08-27 01:46:53 +00:00
|
|
|
{ sections.map( ( section, i ) => {
|
2020-08-23 22:46:18 +00:00
|
|
|
const sectionPiece = Array.isArray( section ) ? (
|
|
|
|
<Link
|
|
|
|
href={
|
|
|
|
isEmbedded
|
|
|
|
? getAdminLink( section[ 0 ] )
|
|
|
|
: getNewPath( {}, section[ 0 ], {} )
|
|
|
|
}
|
|
|
|
type={ isEmbedded ? 'wp-admin' : 'wc-admin' }
|
|
|
|
onClick={ trackLinkClick }
|
|
|
|
>
|
|
|
|
{ section[ 1 ] }
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
section
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<span key={ i }>
|
|
|
|
{ decodeEntities( sectionPiece ) }
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</h1>
|
|
|
|
{ window.wcAdminFeatures[ 'activity-panels' ] && (
|
|
|
|
<ActivityPanel isEmbedded={ isEmbedded } query={ query } />
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
2018-05-22 15:19:56 +00:00
|
|
|
};
|