This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
/
navigation.js
97 lines (90 loc) · 2.23 KB
/
navigation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* External dependencies
*/
import { registerPlugin } from '@wordpress/plugins';
import {
WooNavigationItem,
getNewPath,
pathIsExcluded,
} from '@woocommerce/navigation';
import { Link } from '@woocommerce/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { NAVIGATION_STORE_NAME } from '@woocommerce/data';
/**
* Internal dependencies
*/
import getReports from '../analytics/report/get-reports';
import { getPages } from './controller';
import { isWCAdmin } from '../dashboard/utils';
const NavigationPlugin = () => {
const { persistedQuery } = useSelect( ( select ) => {
return {
persistedQuery: select( NAVIGATION_STORE_NAME ).getPersistedQuery(),
};
} );
/**
* 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.
*/
if ( ! isWCAdmin( window.location.href ) ) {
return null;
}
const reports = getReports().filter( ( item ) => item.navArgs );
const pages = getPages()
.filter( ( page ) => page.navArgs )
.map( ( page ) => {
if ( page.path === '/analytics/settings' ) {
return {
...page,
breadcrumbs: [ __( 'Analytics', 'woocommerce-admin' ) ],
};
}
return page;
} );
return (
<>
{ pages.map( ( page ) => (
<WooNavigationItem
item={ page.navArgs.id }
key={ page.navArgs.id }
>
<Link
className="components-button"
href={ getNewPath(
pathIsExcluded( page.path ) ? {} : persistedQuery,
page.path,
{}
) }
type="wc-admin"
>
{ page.breadcrumbs[ page.breadcrumbs.length - 1 ] }
</Link>
</WooNavigationItem>
) ) }
{ reports.map( ( item ) => (
<WooNavigationItem
item={ item.navArgs.id }
key={ item.navArgs.id }
>
<Link
className="components-button"
href={ getNewPath(
pathIsExcluded( item.report ) ? {} : persistedQuery,
`/analytics/${ item.report }`,
{}
) }
type="wc-admin"
>
{ item.title }
</Link>
</WooNavigationItem>
) ) }
</>
);
};
registerPlugin( 'wc-admin-navigation', {
render: NavigationPlugin,
scope: 'woocommerce-navigation',
} );