diff --git a/packages/editor/src/components/post-publish-panel/maybe-post-format-panel.js b/packages/editor/src/components/post-publish-panel/maybe-post-format-panel.js index 5d173e4f3c680..270d11fe74952 100644 --- a/packages/editor/src/components/post-publish-panel/maybe-post-format-panel.js +++ b/packages/editor/src/components/post-publish-panel/maybe-post-format-panel.js @@ -6,16 +6,22 @@ import { find, get, includes } from 'lodash'; /** * WordPress dependencies */ -import { __, sprintf } from '@wordpress/i18n'; -import { ifCondition, compose } from '@wordpress/compose'; -import { withDispatch, withSelect } from '@wordpress/data'; import { Button, PanelBody } from '@wordpress/components'; +import { useDispatch, useSelect } from '@wordpress/data'; +import { __, sprintf } from '@wordpress/i18n'; /** * Internal dependencies */ import { POST_FORMATS } from '../post-format'; +const getSuggestion = ( supportedFormats, suggestedPostFormat ) => { + const formats = POST_FORMATS.filter( ( format ) => + includes( supportedFormats, format.id ) + ); + return find( formats, ( format ) => format.id === suggestedPostFormat ); +}; + const PostFormatSuggestion = ( { suggestedPostFormat, suggestionText, @@ -26,7 +32,29 @@ const PostFormatSuggestion = ( { ); -const PostFormatPanel = ( { suggestion, onUpdatePostFormat } ) => { +export default function PostFormatPanel() { + const { currentPostFormat, suggestion } = useSelect( ( select ) => { + const { getEditedPostAttribute, getSuggestedPostFormat } = select( + 'core/editor' + ); + const supportedFormats = get( + select( 'core' ).getThemeSupports(), + [ 'formats' ], + [] + ); + return { + currentPostFormat: getEditedPostAttribute( 'format' ), + suggestion: getSuggestion( + supportedFormats, + getSuggestedPostFormat() + ), + }; + }, [] ); + + const { editPost } = useDispatch( 'core/editor' ); + + const onUpdatePostFormat = ( format ) => editPost( { format } ); + const panelBodyTitle = [ __( 'Suggestion:' ), @@ -34,6 +62,10 @@ const PostFormatPanel = ( { suggestion, onUpdatePostFormat } ) => { , ]; + if ( ! suggestion || suggestion.id === currentPostFormat ) { + return null; + } + return (

@@ -54,40 +86,4 @@ const PostFormatPanel = ( { suggestion, onUpdatePostFormat } ) => {

); -}; - -const getSuggestion = ( supportedFormats, suggestedPostFormat ) => { - const formats = POST_FORMATS.filter( ( format ) => - includes( supportedFormats, format.id ) - ); - return find( formats, ( format ) => format.id === suggestedPostFormat ); -}; - -export default compose( - withSelect( ( select ) => { - const { getEditedPostAttribute, getSuggestedPostFormat } = select( - 'core/editor' - ); - const supportedFormats = get( - select( 'core' ).getThemeSupports(), - [ 'formats' ], - [] - ); - return { - currentPostFormat: getEditedPostAttribute( 'format' ), - suggestion: getSuggestion( - supportedFormats, - getSuggestedPostFormat() - ), - }; - } ), - withDispatch( ( dispatch ) => ( { - onUpdatePostFormat( postFormat ) { - dispatch( 'core/editor' ).editPost( { format: postFormat } ); - }, - } ) ), - ifCondition( - ( { suggestion, currentPostFormat } ) => - suggestion && suggestion.id !== currentPostFormat - ) -)( PostFormatPanel ); +}