-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
page-details.js
159 lines (148 loc) · 4.19 KB
/
page-details.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
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
157
158
159
/**
* WordPress dependencies
*/
import { __, _x, sprintf } from '@wordpress/i18n';
import { __experimentalTruncate as Truncate } from '@wordpress/components';
import { count as wordCount } from '@wordpress/wordcount';
import { useSelect } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
import { store as coreStore, useEntityRecord } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import StatusLabel from './status-label';
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import {
SidebarNavigationScreenDetailsPanel,
SidebarNavigationScreenDetailsPanelRow,
SidebarNavigationScreenDetailsPanelLabel,
SidebarNavigationScreenDetailsPanelValue,
} from '../sidebar-navigation-screen-details-panel';
// Taken from packages/editor/src/components/time-to-read/index.js.
const AVERAGE_READING_RATE = 189;
function getPageDetails( page ) {
if ( ! page ) {
return [];
}
const details = [
{
label: __( 'Status' ),
value: (
<StatusLabel
status={ page?.password ? 'protected' : page.status }
date={ page?.date }
/>
),
},
{
label: __( 'Slug' ),
value: <Truncate numberOfLines={ 1 }>{ page.slug }</Truncate>,
},
];
if ( page?.templateTitle ) {
details.push( {
label: __( 'Template' ),
value: decodeEntities( page.templateTitle ),
} );
}
details.push( {
label: __( 'Parent' ),
// `null` indicates no parent.
value:
null === page?.parentTitle
? __( 'Top level' )
: decodeEntities( page?.parentTitle || __( '(no title)' ) ),
} );
/*
* translators: If your word count is based on single characters (e.g. East Asian characters),
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
* Do not translate into your own language.
*/
const wordCountType = _x( 'words', 'Word count type. Do not translate!' );
const wordsCounted = page?.content?.rendered
? wordCount( page.content.rendered, wordCountType )
: 0;
const readingTime = Math.round( wordsCounted / AVERAGE_READING_RATE );
if ( wordsCounted ) {
details.push(
{
label: __( 'Words' ),
value: wordsCounted.toLocaleString() || __( 'Unknown' ),
},
{
label: __( 'Time to read' ),
value:
readingTime > 1
? sprintf(
/* translators: %s: is the number of minutes. */
__( '%s mins' ),
readingTime.toLocaleString()
)
: __( '< 1 min' ),
}
);
}
return details;
}
export default function PageDetails( { id } ) {
const { record } = useEntityRecord( 'postType', 'page', id );
const { parentTitle, templateTitle } = useSelect(
( select ) => {
const { getEditedPostContext } = unlock( select( editSiteStore ) );
const postContext = getEditedPostContext();
const templates = select( coreStore ).getEntityRecords(
'postType',
'wp_template',
{ per_page: -1 }
);
// Template title.
const templateSlug =
// Checks that the post type matches the current theme's post type, otherwise
// the templateSlug returns 'home'.
postContext?.postType === 'page'
? postContext?.templateSlug
: null;
const _templateTitle =
templates && templateSlug
? templates.find(
( template ) => template.slug === templateSlug
)?.title?.rendered
: null;
// Parent page title.
const _parentTitle = record?.parent
? select( coreStore ).getEntityRecord(
'postType',
'page',
record.parent,
{
_fields: [ 'title' ],
}
)?.title?.rendered
: null;
return {
parentTitle: _parentTitle,
templateTitle: _templateTitle,
};
},
[ record?.parent ]
);
return (
<SidebarNavigationScreenDetailsPanel title={ __( 'Details' ) }>
{ getPageDetails( {
parentTitle,
templateTitle,
...record,
} ).map( ( { label, value } ) => (
<SidebarNavigationScreenDetailsPanelRow key={ label }>
<SidebarNavigationScreenDetailsPanelLabel>
{ label }
</SidebarNavigationScreenDetailsPanelLabel>
<SidebarNavigationScreenDetailsPanelValue>
{ value }
</SidebarNavigationScreenDetailsPanelValue>
</SidebarNavigationScreenDetailsPanelRow>
) ) }
</SidebarNavigationScreenDetailsPanel>
);
}