-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathcontent.js
More file actions
89 lines (82 loc) · 2.56 KB
/
content.js
File metadata and controls
89 lines (82 loc) · 2.56 KB
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
/**
* 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 { 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 = 'active' },
} = useLocation();
const { records } = useEntityRecords( 'root', 'registeredTemplate' );
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 className="edit-site-sidebar-navigation-screen-templates-browse">
<SidebarNavigationItem
to="/template"
icon={ layout }
aria-current={ activeView === 'active' }
>
{ __( 'Active templates' ) }
</SidebarNavigationItem>
<SidebarNavigationItem
to={ addQueryArgs( '/template', { activeView: 'user' } ) }
icon={ layout }
aria-current={ activeView === 'user' }
>
{
// Let's avoid calling them "custom templates" to avoid
// confusion. "Created" is closest to meaning database
// templates, created by users.
// https://developer.wordpress.org/themes/classic-themes/templates/page-template-files/#creating-custom-page-templates-for-global-use
__( 'Created templates' )
}
</SidebarNavigationItem>
{ firstItemPerAuthorText.map( ( template ) => {
return (
<TemplateDataviewItem
key={ template.author_text }
template={ template }
isActive={ activeView === template.author_text }
/>
);
} ) }
</ItemGroup>
);
}