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';
|
2021-03-03 00:05:08 +00:00
|
|
|
import { Tooltip } from '@wordpress/components';
|
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';
|
2021-03-03 00:05:08 +00:00
|
|
|
import { Icon, chevronLeft } from '@wordpress/icons';
|
|
|
|
import { getHistory, updateQueryString } from '@woocommerce/navigation';
|
|
|
|
import { ENTER, SPACE } from '@wordpress/keycodes';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
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
|
|
|
|
2021-03-03 00:05:08 +00:00
|
|
|
const renderTaskListBackButton = () => {
|
|
|
|
const currentUrl = new URL( window.location.href );
|
|
|
|
const task = currentUrl.searchParams.get( 'task' );
|
|
|
|
|
|
|
|
if ( task ) {
|
|
|
|
const homeText = __( 'WooCommerce Home', 'woocommerce-admin' );
|
|
|
|
|
|
|
|
const navigateHome = () => {
|
|
|
|
recordEvent( 'topbar_back_button', {
|
|
|
|
page_name: getPageTitle( window.title ),
|
|
|
|
} );
|
|
|
|
updateQueryString( {}, getHistory().location.pathname, {} );
|
|
|
|
};
|
|
|
|
|
|
|
|
// if it's a task list page, render a back button to the homescreen
|
|
|
|
return (
|
|
|
|
<Tooltip text={ homeText }>
|
|
|
|
<div
|
|
|
|
tabIndex="0"
|
|
|
|
role="button"
|
|
|
|
data-testid="header-back-button"
|
|
|
|
className="woocommerce-layout__header-back-button"
|
|
|
|
onKeyDown={ ( { keyCode } ) => {
|
|
|
|
if ( keyCode === ENTER || keyCode === SPACE ) {
|
|
|
|
navigateHome();
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
<Icon icon={ chevronLeft } onClick={ navigateHome } />
|
|
|
|
</div>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getPageTitle = ( defaultTitle ) => {
|
|
|
|
const currentUrl = new URL( window.location.href );
|
|
|
|
const task = currentUrl.searchParams.get( 'task' );
|
|
|
|
|
|
|
|
// If it's the task list then render a title based on which task the user is on.
|
|
|
|
return (
|
|
|
|
{
|
2021-05-21 01:13:40 +00:00
|
|
|
payments: __( 'Set up payments', 'woocommerce-admin' ),
|
2021-03-03 00:05:08 +00:00
|
|
|
tax: __( 'Add tax rates', 'woocommerce-admin' ),
|
|
|
|
appearance: __( 'Personalize your store', 'woocommerce-admin' ),
|
|
|
|
products: __( 'Add products', 'woocommerce-admin' ),
|
|
|
|
shipping: __( 'Set up shipping costs', 'woocommerce-admin' ),
|
|
|
|
}[ task ] || defaultTitle
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2021-03-03 00:05:08 +00:00
|
|
|
const backButton = renderTaskListBackButton();
|
|
|
|
const backButtonClass = backButton ? 'with-back-button' : '';
|
|
|
|
|
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 /> }
|
2021-03-03 00:05:08 +00:00
|
|
|
{ renderTaskListBackButton() }
|
2021-02-12 16:32:26 +00:00
|
|
|
<Text
|
2021-03-03 00:05:08 +00:00
|
|
|
className={ `woocommerce-layout__header-heading ${ backButtonClass }` }
|
2021-02-12 16:32:26 +00:00
|
|
|
as="h1"
|
|
|
|
>
|
2021-03-03 00:05:08 +00:00
|
|
|
{ getPageTitle( decodeEntities( pageTitle ) ) }
|
2021-02-12 16:32:26 +00:00
|
|
|
</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
|
|
|
};
|