-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathindex.js
126 lines (116 loc) · 3.42 KB
/
index.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
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
/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import { pencil } from '@wordpress/icons';
import {
__experimentalUseNavigator as useNavigator,
Icon,
} from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import useEditedEntityRecord from '../use-edited-entity-record';
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import SidebarButton from '../sidebar-button';
import { useAddedBy } from '../list/added-by';
import TemplateActions from '../template-actions';
import HomeTemplateDetails from './home-template-details';
import SidebarNavigationScreenDetailsFooter from '../sidebar-navigation-screen-details-footer';
function useTemplateDetails( postType, postId ) {
const { getDescription, getTitle, record } = useEditedEntityRecord(
postType,
postId
);
const currentTheme = useSelect(
( select ) => select( coreStore ).getCurrentTheme(),
[]
);
const addedBy = useAddedBy( postType, postId );
const isAddedByActiveTheme =
addedBy.type === 'theme' && record.theme === currentTheme?.stylesheet;
const title = getTitle();
let descriptionText = getDescription();
if ( ! descriptionText && addedBy.text ) {
descriptionText = __(
'This is a custom template that can be applied manually to any Post or Page.'
);
}
const content =
record?.slug === 'home' || record?.slug === 'index' ? (
<HomeTemplateDetails />
) : null;
const footer = record?.modified ? (
<SidebarNavigationScreenDetailsFooter record={ record } />
) : null;
const description = (
<>
{ descriptionText }
{ addedBy.text && ! isAddedByActiveTheme && (
<span className="edit-site-sidebar-navigation-screen-template__added-by-description">
<span className="edit-site-sidebar-navigation-screen-template__added-by-description-author">
<span className="edit-site-sidebar-navigation-screen-template__added-by-description-author-icon">
{ addedBy.imageUrl ? (
<img
src={ addedBy.imageUrl }
alt=""
width="24"
height="24"
/>
) : (
<Icon icon={ addedBy.icon } />
) }
</span>
{ addedBy.text }
</span>
{ addedBy.isCustomized && (
<span className="edit-site-sidebar-navigation-screen-template__added-by-description-customized">
{ _x( '(Customized)', 'template' ) }
</span>
) }
</span>
) }
</>
);
return { title, description, content, footer };
}
export default function SidebarNavigationScreenTemplate() {
const navigator = useNavigator();
const {
params: { postType, postId },
} = navigator;
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );
const { title, content, description, footer } = useTemplateDetails(
postType,
postId
);
return (
<SidebarNavigationScreen
title={ title }
actions={
<>
<TemplateActions
postType={ postType }
postId={ postId }
toggleProps={ { as: SidebarButton } }
onRemove={ () => {
navigator.goTo( `/${ postType }/all` );
} }
/>
<SidebarButton
onClick={ () => setCanvasMode( 'edit' ) }
label={ __( 'Edit' ) }
icon={ pencil }
/>
</>
}
description={ description }
content={ content }
footer={ footer }
/>
);
}