2020-10-22 03:01:25 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { registerPlugin } from '@wordpress/plugins';
|
|
|
|
import {
|
|
|
|
WooNavigationItem,
|
|
|
|
getNewPath,
|
2021-04-02 21:35:31 +00:00
|
|
|
pathIsExcluded,
|
2022-06-01 17:10:20 +00:00
|
|
|
isWCAdmin,
|
2020-10-22 03:01:25 +00:00
|
|
|
} from '@woocommerce/navigation';
|
|
|
|
import { Link } from '@woocommerce/components';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-04-02 21:35:31 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { NAVIGATION_STORE_NAME } from '@woocommerce/data';
|
2020-10-22 03:01:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import getReports from '../analytics/report/get-reports';
|
|
|
|
import { getPages } from './controller';
|
2021-12-14 16:56:42 +00:00
|
|
|
import Navigation from '~/navigation';
|
|
|
|
import { WooHeaderNavigationItem } from '~/header/utils';
|
2020-10-22 03:01:25 +00:00
|
|
|
|
|
|
|
const NavigationPlugin = () => {
|
2021-04-02 21:35:31 +00:00
|
|
|
const { persistedQuery } = useSelect( ( select ) => {
|
|
|
|
return {
|
|
|
|
persistedQuery: select( NAVIGATION_STORE_NAME ).getPersistedQuery(),
|
|
|
|
};
|
|
|
|
} );
|
2021-01-07 16:53:31 +00:00
|
|
|
|
2021-12-14 16:56:42 +00:00
|
|
|
if ( ! window.wcAdminFeatures.navigation ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-22 03:01:25 +00:00
|
|
|
/**
|
|
|
|
* If the current page is embedded, stay with the default urls
|
|
|
|
* provided by Navigation because the router isn't present to
|
|
|
|
* respond to <Link /> component's manipulation of the url.
|
|
|
|
*/
|
2022-06-01 17:10:20 +00:00
|
|
|
if ( ! isWCAdmin() ) {
|
2021-12-14 16:56:42 +00:00
|
|
|
return (
|
|
|
|
<WooHeaderNavigationItem order={ -100 }>
|
|
|
|
<Navigation />
|
|
|
|
</WooHeaderNavigationItem>
|
|
|
|
);
|
2020-10-22 03:01:25 +00:00
|
|
|
}
|
2021-01-07 16:53:31 +00:00
|
|
|
|
2020-12-02 22:56:24 +00:00
|
|
|
const reports = getReports().filter( ( item ) => item.navArgs );
|
2021-01-07 16:53:31 +00:00
|
|
|
|
2020-11-18 01:02:46 +00:00
|
|
|
const pages = getPages()
|
2020-12-02 22:56:24 +00:00
|
|
|
.filter( ( page ) => page.navArgs )
|
2020-11-18 01:02:46 +00:00
|
|
|
.map( ( page ) => {
|
2020-12-02 22:56:24 +00:00
|
|
|
if ( page.path === '/analytics/settings' ) {
|
2020-11-18 01:02:46 +00:00
|
|
|
return {
|
2020-12-02 22:56:24 +00:00
|
|
|
...page,
|
2022-03-30 09:00:04 +00:00
|
|
|
breadcrumbs: [ __( 'Analytics', 'woocommerce' ) ],
|
2020-11-18 01:02:46 +00:00
|
|
|
};
|
|
|
|
}
|
2020-12-02 22:56:24 +00:00
|
|
|
return page;
|
2020-11-18 01:02:46 +00:00
|
|
|
} );
|
2021-01-07 16:53:31 +00:00
|
|
|
|
2020-10-22 03:01:25 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-12-14 16:56:42 +00:00
|
|
|
<WooHeaderNavigationItem order={ -100 }>
|
|
|
|
<Navigation />
|
|
|
|
</WooHeaderNavigationItem>
|
2020-10-22 03:01:25 +00:00
|
|
|
{ pages.map( ( page ) => (
|
2020-12-02 22:56:24 +00:00
|
|
|
<WooNavigationItem
|
|
|
|
item={ page.navArgs.id }
|
|
|
|
key={ page.navArgs.id }
|
|
|
|
>
|
2020-10-22 03:01:25 +00:00
|
|
|
<Link
|
|
|
|
className="components-button"
|
2021-01-07 16:53:31 +00:00
|
|
|
href={ getNewPath(
|
|
|
|
pathIsExcluded( page.path ) ? {} : persistedQuery,
|
|
|
|
page.path,
|
|
|
|
{}
|
|
|
|
) }
|
2020-10-22 03:01:25 +00:00
|
|
|
type="wc-admin"
|
|
|
|
>
|
|
|
|
{ page.breadcrumbs[ page.breadcrumbs.length - 1 ] }
|
|
|
|
</Link>
|
|
|
|
</WooNavigationItem>
|
|
|
|
) ) }
|
2020-12-02 22:56:24 +00:00
|
|
|
{ reports.map( ( item ) => (
|
|
|
|
<WooNavigationItem
|
|
|
|
item={ item.navArgs.id }
|
|
|
|
key={ item.navArgs.id }
|
|
|
|
>
|
|
|
|
<Link
|
|
|
|
className="components-button"
|
|
|
|
href={ getNewPath(
|
2021-01-07 16:53:31 +00:00
|
|
|
pathIsExcluded( item.report ) ? {} : persistedQuery,
|
2020-12-02 22:56:24 +00:00
|
|
|
`/analytics/${ item.report }`,
|
|
|
|
{}
|
|
|
|
) }
|
|
|
|
type="wc-admin"
|
|
|
|
>
|
|
|
|
{ item.title }
|
|
|
|
</Link>
|
|
|
|
</WooNavigationItem>
|
|
|
|
) ) }
|
2020-10-22 03:01:25 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-01-07 16:53:31 +00:00
|
|
|
registerPlugin( 'wc-admin-navigation', {
|
|
|
|
render: NavigationPlugin,
|
2021-09-15 16:42:54 +00:00
|
|
|
scope: 'woocommerce-navigation',
|
2021-01-07 16:53:31 +00:00
|
|
|
} );
|