| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { PanelBody, SelectControl } from '@wordpress/components'; |
| 6 |
import { useEntityProp } from '@wordpress/core-data'; |
| 7 |
import { useSelect } from '@wordpress/data'; |
| 8 |
import { useEffect } from '@wordpress/element'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Internal dependencies |
| 12 |
*/ |
| 13 |
import { |
| 14 |
getDefaultEmbed, |
| 15 |
getEmbedOptions, |
| 16 |
isEmbedValid, |
| 17 |
normalizeSource, |
| 18 |
OUTPUT_AUDIO, |
| 19 |
} from './helpers'; |
| 20 |
import Stack from '../stack'; |
| 21 |
|
| 22 |
export function PlayerSection( { withPanel = true } ) { |
| 23 |
const postType = useSelect( |
| 24 |
( select ) => select( 'core/editor' ).getCurrentPostType(), |
| 25 |
[] |
| 26 |
); |
| 27 |
|
| 28 |
// `useEntityProp` yields undefined meta until the post entity record is hydrated. |
| 29 |
const [ rawMeta, setMeta ] = useEntityProp( 'postType', postType, 'meta' ); |
| 30 |
const meta = rawMeta ?? {}; |
| 31 |
|
| 32 |
const source = normalizeSource( meta.beyondwords_source ); |
| 33 |
const output = meta.beyondwords_output || OUTPUT_AUDIO; |
| 34 |
|
| 35 |
const stored = meta.beyondwords_embed; |
| 36 |
// Unset → default to the first asset so the player shows ("None" is the |
| 37 |
// opt-out; the v7.0.0 migration converts legacy `beyondwords_disabled` posts). |
| 38 |
const embed = stored || getDefaultEmbed( source, output ); |
| 39 |
|
| 40 |
const embedOptions = getEmbedOptions( source, output ); |
| 41 |
|
| 42 |
const setEmbed = ( value ) => { |
| 43 |
setMeta( { ...meta, beyondwords_embed: value } ); |
| 44 |
}; |
| 45 |
|
| 46 |
// Never write meta on mount: an unset value already means "show", and a |
| 47 |
// mount-time write races the other panels' preselect writes (e.g. Generate audio). |
| 48 |
// Only a stale value lands here — None is valid for every combination. |
| 49 |
useEffect( () => { |
| 50 |
if ( stored && ! isEmbedValid( stored, source, output ) ) { |
| 51 |
setEmbed( getDefaultEmbed( source, output ) ); |
| 52 |
} |
| 53 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 54 |
}, [ source, output ] ); |
| 55 |
|
| 56 |
const field = ( |
| 57 |
<SelectControl |
| 58 |
className="beyondwords--embed" |
| 59 |
label={ __( 'Embed', 'speechkit' ) } |
| 60 |
help={ __( |
| 61 |
'Pick which generated asset is shown on this post. All other generated assets stay available in BeyondWords.', |
| 62 |
'speechkit' |
| 63 |
) } |
| 64 |
options={ embedOptions } |
| 65 |
value={ embed } |
| 66 |
onChange={ setEmbed } |
| 67 |
__nextHasNoMarginBottom |
| 68 |
__next40pxDefaultSize |
| 69 |
/> |
| 70 |
); |
| 71 |
|
| 72 |
// Document/pre-publish panels render the field without nesting another panel. |
| 73 |
if ( ! withPanel ) { |
| 74 |
return <Stack>{ field }</Stack>; |
| 75 |
} |
| 76 |
|
| 77 |
return ( |
| 78 |
<PanelBody title={ __( 'Player', 'speechkit' ) } initialOpen={ true }> |
| 79 |
<Stack>{ field }</Stack> |
| 80 |
</PanelBody> |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
export default PlayerSection; |
| 85 |
|