-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
content.js
79 lines (72 loc) · 2.06 KB
/
content.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
/**
* WordPress dependencies
*/
import { useEntityRecords } from '@wordpress/core-data';
import { useMemo } from '@wordpress/element';
import { __experimentalItemGroup as ItemGroup } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { addQueryArgs } from '@wordpress/url';
/**
* Internal dependencies
*/
import SidebarNavigationItem from '../sidebar-navigation-item';
import { useAddedBy } from '../page-templates/hooks';
import { layout } from '@wordpress/icons';
import { TEMPLATE_POST_TYPE } from '../../utils/constants';
import { unlock } from '../../lock-unlock';
const { useLocation } = unlock( routerPrivateApis );
const EMPTY_ARRAY = [];
function TemplateDataviewItem( { template, isActive } ) {
const { text, icon } = useAddedBy( template.type, template.id );
return (
<SidebarNavigationItem
to={ addQueryArgs( '/template', { activeView: text } ) }
icon={ icon }
aria-current={ isActive }
>
{ text }
</SidebarNavigationItem>
);
}
export default function DataviewsTemplatesSidebarContent() {
const {
query: { activeView = 'all' },
} = useLocation();
const { records } = useEntityRecords( 'postType', TEMPLATE_POST_TYPE, {
per_page: -1,
} );
const firstItemPerAuthorText = useMemo( () => {
const firstItemPerAuthor = records?.reduce( ( acc, template ) => {
const author = template.author_text;
if ( author && ! acc[ author ] ) {
acc[ author ] = template;
}
return acc;
}, {} );
return (
( firstItemPerAuthor && Object.values( firstItemPerAuthor ) ) ??
EMPTY_ARRAY
);
}, [ records ] );
return (
<ItemGroup>
<SidebarNavigationItem
to="/template"
icon={ layout }
aria-current={ activeView === 'all' }
>
{ __( 'All templates' ) }
</SidebarNavigationItem>
{ firstItemPerAuthorText.map( ( template ) => {
return (
<TemplateDataviewItem
key={ template.author_text }
template={ template }
isActive={ activeView === template.author_text }
/>
);
} ) }
</ItemGroup>
);
}