11/**
22 * WordPress dependencies
33 */
4- import { Component } from '@wordpress/element' ;
5- import { withSelect } from '@wordpress/data' ;
4+ import { useEffect , useState } from '@wordpress/element' ;
5+ import { useSelect } from '@wordpress/data' ;
66import { addQueryArgs } from '@wordpress/url' ;
77import { store as editorStore } from '@wordpress/editor' ;
88
@@ -17,65 +17,35 @@ export function getPostEditURL( postId ) {
1717 return addQueryArgs ( 'post.php' , { post : postId , action : 'edit' } ) ;
1818}
1919
20- export class BrowserURL extends Component {
21- constructor ( ) {
22- super ( ...arguments ) ;
20+ export default function BrowserURL ( ) {
21+ const [ historyId , setHistoryId ] = useState ( null ) ;
22+ const { postId, postStatus } = useSelect ( ( select ) => {
23+ const { getCurrentPost } = select ( editorStore ) ;
24+ const post = getCurrentPost ( ) ;
25+ let { id, status, type } = post ;
26+ const isTemplate = [ 'wp_template' , 'wp_template_part' ] . includes (
27+ type
28+ ) ;
29+ if ( isTemplate ) {
30+ id = post . wp_id ;
31+ }
2332
24- this . state = {
25- historyId : null ,
33+ return {
34+ postId : id ,
35+ postStatus : status ,
2636 } ;
27- }
28-
29- componentDidUpdate ( prevProps ) {
30- const { postId, postStatus } = this . props ;
31- const { historyId } = this . state ;
32-
33- if (
34- ( postId !== prevProps . postId || postId !== historyId ) &&
35- postStatus !== 'auto-draft' &&
36- postId
37- ) {
38- this . setBrowserURL ( postId ) ;
37+ } , [ ] ) ;
38+
39+ useEffect ( ( ) => {
40+ if ( postId && postId !== historyId && postStatus !== 'auto-draft' ) {
41+ window . history . replaceState (
42+ { id : postId } ,
43+ 'Post ' + postId ,
44+ getPostEditURL ( postId )
45+ ) ;
46+ setHistoryId ( postId ) ;
3947 }
40- }
48+ } , [ postId , postStatus , historyId ] ) ;
4149
42- /**
43- * Replaces the browser URL with a post editor link for the given post ID.
44- *
45- * Note it is important that, since this function may be called when the
46- * editor first loads, the result generated `getPostEditURL` matches that
47- * produced by the server. Otherwise, the URL will change unexpectedly.
48- *
49- * @param {number } postId Post ID for which to generate post editor URL.
50- */
51- setBrowserURL ( postId ) {
52- window . history . replaceState (
53- { id : postId } ,
54- 'Post ' + postId ,
55- getPostEditURL ( postId )
56- ) ;
57-
58- this . setState ( ( ) => ( {
59- historyId : postId ,
60- } ) ) ;
61- }
62-
63- render ( ) {
64- return null ;
65- }
50+ return null ;
6651}
67-
68- export default withSelect ( ( select ) => {
69- const { getCurrentPost } = select ( editorStore ) ;
70- const post = getCurrentPost ( ) ;
71- let { id, status, type } = post ;
72- const isTemplate = [ 'wp_template' , 'wp_template_part' ] . includes ( type ) ;
73- if ( isTemplate ) {
74- id = post . wp_id ;
75- }
76-
77- return {
78- postId : id ,
79- postStatus : status ,
80- } ;
81- } ) ( BrowserURL ) ;
0 commit comments