File "use-post-permalink.js"

Full Path: /home/fresvfqn/waterdamagerestorationandrepairsmithtown.com/wp-content/plugins/surerank/src/global/hooks/use-post-permalink.js
File size: 829 bytes
MIME-type: text/x-java
Charset: utf-8

import { useState } from '@wordpress/element';

const usePostPermalink = () => {
	const getCurrentPermalink = () => {
		if ( ! wp?.data?.select( 'core/editor' ) ) {
			return '';
		}

		const { getPermalink, isCurrentPostPublished } =
			wp.data.select( 'core/editor' );
		const permalink = getPermalink();
		const isPublished = isCurrentPostPublished();

		if (
			( isPublished && permalink ) ||
			( permalink && ! permalink?.includes( 'auto-draft' ) )
		) {
			return permalink;
		}

		return '';
	};
	const [ postPermalink, setPostPermalink ] = useState(
		getCurrentPermalink()
	);

	wp.data.subscribe( () => {
		const permalink = getCurrentPermalink();
		if ( ! permalink || permalink === postPermalink ) {
			return;
		}
		setPostPermalink( permalink );
	} );

	return postPermalink;
};

export default usePostPermalink;