-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathindex.js
More file actions
156 lines (152 loc) · 3.9 KB
/
index.js
File metadata and controls
156 lines (152 loc) · 3.9 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* WordPress dependencies
*/
import {
__experimentalItemGroup as ItemGroup,
__experimentalItem as Item,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEntityRecords } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
import { useViewportMatch } from '@wordpress/compose';
/**
* Internal dependencies
*/
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import { useLink } from '../routes/link';
import SidebarNavigationItem from '../sidebar-navigation-item';
import AddNewTemplate from '../add-new-template';
import SidebarButton from '../sidebar-button';
import { TEMPLATE_POST_TYPE } from '../../utils/constants';
const TemplateItem = ( { postType, postId, ...props } ) => {
const linkInfo = useLink( {
postType,
postId,
} );
return <SidebarNavigationItem { ...linkInfo } { ...props } />;
};
export default function SidebarNavigationScreenTemplates() {
const isMobileViewport = useViewportMatch( 'medium', '<' );
const { records: templates, isResolving: isLoading } = useEntityRecords(
'postType',
TEMPLATE_POST_TYPE,
{ per_page: -1 }
);
const browseAllLink = useLink( { path: '/wp_template/all' } );
const canCreate = ! isMobileViewport;
return (
<SidebarNavigationScreen
title={ __( 'Templates' ) }
description={ __(
'Express the layout of your site with templates.'
) }
actions={
canCreate && (
<AddNewTemplate
templateType={ TEMPLATE_POST_TYPE }
toggleProps={ {
as: SidebarButton,
} }
/>
)
}
content={
<>
{ isLoading && __( 'Loading templates…' ) }
{ ! isLoading && (
<SidebarTemplatesList templates={ templates } />
) }
</>
}
footer={
! isMobileViewport && (
<SidebarNavigationItem withChevron { ...browseAllLink }>
{ __( 'Manage all templates' ) }
</SidebarNavigationItem>
)
}
/>
);
}
function TemplatesGroup( { title, templates } ) {
return (
<ItemGroup>
{ !! title && (
<Item className="edit-site-sidebar-navigation-screen-templates__templates-group-title">
{ title }
</Item>
) }
{ templates.map( ( template ) => (
<TemplateItem
postType={ TEMPLATE_POST_TYPE }
postId={ template.id }
key={ template.id }
withChevron
>
{ decodeEntities(
template.title?.rendered || template.slug
) }
</TemplateItem>
) ) }
</ItemGroup>
);
}
function SidebarTemplatesList( { templates } ) {
if ( ! templates?.length ) {
return (
<ItemGroup>
<Item>{ __( 'No templates found' ) }</Item>
</ItemGroup>
);
}
const sortedTemplates = templates ? [ ...templates ] : [];
sortedTemplates.sort( ( a, b ) =>
a.title.rendered.localeCompare( b.title.rendered )
);
const { hierarchyTemplates, customTemplates, ...plugins } =
sortedTemplates.reduce(
( accumulator, template ) => {
const {
original_source: originalSource,
author_text: authorText,
} = template;
if ( originalSource === 'plugin' ) {
if ( ! accumulator[ authorText ] ) {
accumulator[ authorText ] = [];
}
accumulator[ authorText ].push( template );
} else if ( template.is_custom ) {
accumulator.customTemplates.push( template );
} else {
accumulator.hierarchyTemplates.push( template );
}
return accumulator;
},
{ hierarchyTemplates: [], customTemplates: [] }
);
return (
<VStack spacing={ 3 }>
{ !! hierarchyTemplates.length && (
<TemplatesGroup templates={ hierarchyTemplates } />
) }
{ !! customTemplates.length && (
<TemplatesGroup
title={ __( 'Custom' ) }
templates={ customTemplates }
/>
) }
{ Object.entries( plugins ).map(
( [ plugin, pluginTemplates ] ) => {
return (
<TemplatesGroup
key={ plugin }
title={ plugin }
templates={ pluginTemplates }
/>
);
}
) }
</VStack>
);
}