| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { Spinner } from '@wordpress/components'; |
| 5 |
import { compose } from '@wordpress/compose'; |
| 6 |
import { withSelect } from '@wordpress/data'; |
| 7 |
import { Fragment, useState } from '@wordpress/element'; |
| 8 |
import { __ } from '@wordpress/i18n'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Internal dependencies |
| 12 |
*/ |
| 13 |
import PlayAudioCheck from './check'; |
| 14 |
import { useBeyondWordsPlayer } from './hooks'; |
| 15 |
|
| 16 |
/** |
| 17 |
* The message to show when the player did not (yet) embed. |
| 18 |
* |
| 19 |
* @param {string|undefined} status Last-seen content status. |
| 20 |
* @param {boolean} timedOut Whether polling gave up. |
| 21 |
* |
| 22 |
* @return {string|null} The message, or null when there is nothing to say. |
| 23 |
*/ |
| 24 |
function terminalMessage( status, timedOut ) { |
| 25 |
if ( timedOut ) { |
| 26 |
return __( |
| 27 |
'Generation is taking longer than expected. Refresh to check again.', |
| 28 |
'speechkit' |
| 29 |
); |
| 30 |
} |
| 31 |
if ( status === 'error' ) { |
| 32 |
return __( 'Generation failed.', 'speechkit' ); |
| 33 |
} |
| 34 |
if ( status === 'skipped' ) { |
| 35 |
return __( 'No content was generated.', 'speechkit' ); |
| 36 |
} |
| 37 |
return null; |
| 38 |
} |
| 39 |
|
| 40 |
function PlayAudio( { |
| 41 |
contentId, |
| 42 |
previewToken, |
| 43 |
projectId, |
| 44 |
sourceId, |
| 45 |
wrapper = Fragment, |
| 46 |
} ) { |
| 47 |
const Wrapper = wrapper; |
| 48 |
|
| 49 |
const [ target, setTarget ] = useState( null ); |
| 50 |
|
| 51 |
const { player, status, isPolling, timedOut } = useBeyondWordsPlayer( { |
| 52 |
target, |
| 53 |
projectId, |
| 54 |
sourceId, |
| 55 |
contentId, |
| 56 |
previewToken, |
| 57 |
} ); |
| 58 |
|
| 59 |
// Only speak up once polling has settled without a player (error/skipped/ |
| 60 |
// timeout). While polling, the spinner covers it. |
| 61 |
const message = |
| 62 |
! isPolling && ! player ? terminalMessage( status, timedOut ) : null; |
| 63 |
|
| 64 |
return ( |
| 65 |
<PlayAudioCheck> |
| 66 |
<Wrapper> |
| 67 |
<div className="beyondwords-player-box-wrapper"> |
| 68 |
{ /* Live region scoped to the status text only, so the |
| 69 |
player's own DOM isn't announced when it embeds. */ } |
| 70 |
{ ( isPolling || message ) && ( |
| 71 |
<div role="status" aria-live="polite"> |
| 72 |
{ isPolling && ( |
| 73 |
<p className="beyondwords-player-loading"> |
| 74 |
<Spinner /> |
| 75 |
{ __( 'Generating…', 'speechkit' ) } |
| 76 |
</p> |
| 77 |
) } |
| 78 |
{ message && ( |
| 79 |
<p className="beyondwords-player-message"> |
| 80 |
{ message } |
| 81 |
</p> |
| 82 |
) } |
| 83 |
</div> |
| 84 |
) } |
| 85 |
{ /* Always mounted so the player has a stable target. */ } |
| 86 |
<div ref={ setTarget }></div> |
| 87 |
</div> |
| 88 |
</Wrapper> |
| 89 |
</PlayAudioCheck> |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
export default compose( [ |
| 94 |
withSelect( ( select ) => { |
| 95 |
const { getCurrentPostId, getEditedPostAttribute } = |
| 96 |
select( 'core/editor' ); |
| 97 |
|
| 98 |
const beyondwordsProjectId = |
| 99 |
getEditedPostAttribute( 'meta' ).beyondwords_project_id; |
| 100 |
const speechkitProjectId = |
| 101 |
getEditedPostAttribute( 'meta' ).speechkit_project_id; |
| 102 |
|
| 103 |
const beyondwordsContentId = |
| 104 |
getEditedPostAttribute( 'meta' ).beyondwords_content_id; |
| 105 |
const beyondwordsPodcastId = |
| 106 |
getEditedPostAttribute( 'meta' ).beyondwords_podcast_id; |
| 107 |
const speechkitPodcastId = |
| 108 |
getEditedPostAttribute( 'meta' ).speechkit_podcast_id; |
| 109 |
|
| 110 |
const beyondwordsPreviewToken = |
| 111 |
getEditedPostAttribute( 'meta' ).beyondwords_preview_token; |
| 112 |
|
| 113 |
return { |
| 114 |
contentId: |
| 115 |
beyondwordsContentId || |
| 116 |
beyondwordsPodcastId || |
| 117 |
speechkitPodcastId, |
| 118 |
previewToken: beyondwordsPreviewToken, |
| 119 |
projectId: beyondwordsProjectId || speechkitProjectId, |
| 120 |
sourceId: getCurrentPostId(), |
| 121 |
}; |
| 122 |
} ), |
| 123 |
] )( PlayAudio ); |
| 124 |
|