| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { PanelBody, PanelRow } from '@wordpress/components'; |
| 6 |
import { useSelect } from '@wordpress/data'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Internal dependencies |
| 10 |
*/ |
| 11 |
import ErrorNotice from '../error-notice'; |
| 12 |
import PlayAudio from '../play-audio'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Whether the post has audio/video ready to preview. |
| 16 |
* |
| 17 |
* Matches PlayAudioCheck so the panel hides when PlayAudio would render nothing; |
| 18 |
* legacy `podcast_id` keys are recognised for upgraded posts. |
| 19 |
* |
| 20 |
* @param {Function} select Redux-style select() from `@wordpress/data`. |
| 21 |
* |
| 22 |
* @return {boolean} True when a content/podcast id is set on the post. |
| 23 |
*/ |
| 24 |
function hasGeneratedContent( select ) { |
| 25 |
const meta = select( 'core/editor' ).getEditedPostAttribute( 'meta' ); |
| 26 |
return Boolean( |
| 27 |
meta?.beyondwords_content_id || |
| 28 |
meta?.beyondwords_podcast_id || |
| 29 |
meta?.speechkit_podcast_id |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Whether the post has a BeyondWords error message to surface. |
| 35 |
* |
| 36 |
* @param {Function} select Redux-style select() from `@wordpress/data`. |
| 37 |
* |
| 38 |
* @return {boolean} True when an error message is set on the post. |
| 39 |
*/ |
| 40 |
function hasError( select ) { |
| 41 |
const meta = select( 'core/editor' ).getEditedPostAttribute( 'meta' ); |
| 42 |
return Boolean( |
| 43 |
meta?.beyondwords_error_message || meta?.speechkit_error_message |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
export function PreviewPanel() { |
| 48 |
// An error alone also shows the panel, mirroring the document-settings display. |
| 49 |
const showPanel = useSelect( |
| 50 |
( select ) => hasGeneratedContent( select ) || hasError( select ), |
| 51 |
[] |
| 52 |
); |
| 53 |
|
| 54 |
if ( ! showPanel ) { |
| 55 |
return null; |
| 56 |
} |
| 57 |
|
| 58 |
return ( |
| 59 |
<PanelBody |
| 60 |
title={ __( 'Preview', 'speechkit' ) } |
| 61 |
initialOpen={ true } |
| 62 |
className="beyondwords beyondwords-sidebar__preview" |
| 63 |
> |
| 64 |
<ErrorNotice wrapper={ PanelRow } /> |
| 65 |
<PlayAudio wrapper={ PanelRow } /> |
| 66 |
</PanelBody> |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
export default PreviewPanel; |
| 71 |
|