| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { |
| 6 |
Button, |
| 7 |
PanelBody, |
| 8 |
TextareaControl, |
| 9 |
TextControl, |
| 10 |
} from '@wordpress/components'; |
| 11 |
import { compose, useCopyToClipboard } from '@wordpress/compose'; |
| 12 |
import { useDispatch, withDispatch, withSelect } from '@wordpress/data'; |
| 13 |
import { useEffect, useState } from '@wordpress/element'; |
| 14 |
import { store as noticesStore } from '@wordpress/notices'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Internal dependencies |
| 18 |
*/ |
| 19 |
import Stack from '../stack'; |
| 20 |
import { getTextToCopy, hasBeyondwordsData } from './helpers'; |
| 21 |
|
| 22 |
export function PostInspectPanel( { |
| 23 |
beyondwordsDeleteContent, |
| 24 |
beyondwordsGenerateAudio, |
| 25 |
beyondwordsIntegrationMethod, |
| 26 |
beyondwordsContentId, |
| 27 |
beyondwordsPreviewToken, |
| 28 |
beyondwordsLanguageCode, |
| 29 |
beyondwordsLanguageId, |
| 30 |
beyondwordsBodyVoiceId, |
| 31 |
beyondwordsProjectId, |
| 32 |
beyondwordsErrorMessage, |
| 33 |
beyondwordsSource, |
| 34 |
beyondwordsOutput, |
| 35 |
beyondwordsScriptTemplateId, |
| 36 |
beyondwordsVideoTemplateId, |
| 37 |
beyondwordsVideoSize, |
| 38 |
beyondwordsEmbed, |
| 39 |
pluginVersion, |
| 40 |
wpVersion, |
| 41 |
wpPostId, |
| 42 |
// Live post meta + key lists (from PHP) that drive the Copy/Remove controls. |
| 43 |
meta, |
| 44 |
currentMetaKeys, |
| 45 |
deprecatedMetaKeys, |
| 46 |
createWarningNotice, |
| 47 |
removeWarningNotice, |
| 48 |
setDeleteContent, |
| 49 |
didPostSaveRequestSucceed, |
| 50 |
isSavingPost, |
| 51 |
isAutosavingPost, |
| 52 |
} ) { |
| 53 |
const [ removed, setRemoved ] = useState( false ); |
| 54 |
const { createNotice } = useDispatch( noticesStore ); |
| 55 |
|
| 56 |
useEffect( () => { |
| 57 |
if ( isSavingPost && ! isAutosavingPost && didPostSaveRequestSucceed ) { |
| 58 |
removeWarningNotice(); |
| 59 |
} |
| 60 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 61 |
}, [ didPostSaveRequestSucceed, isAutosavingPost, isSavingPost ] ); |
| 62 |
|
| 63 |
useEffect( () => { |
| 64 |
if ( |
| 65 |
isSavingPost && |
| 66 |
! isAutosavingPost && |
| 67 |
didPostSaveRequestSucceed && |
| 68 |
removed |
| 69 |
) { |
| 70 |
setRemoved( false ); |
| 71 |
} |
| 72 |
}, [ didPostSaveRequestSucceed, isAutosavingPost, isSavingPost, removed ] ); |
| 73 |
|
| 74 |
// Copy and Remove derive from the same live meta + PHP-supplied key lists, so |
| 75 |
// they can never disagree and Remove tracks edits made after mount. |
| 76 |
const dataKeys = [ |
| 77 |
...( currentMetaKeys ?? [] ), |
| 78 |
...( deprecatedMetaKeys ?? [] ), |
| 79 |
]; |
| 80 |
const hasData = hasBeyondwordsData( meta, dataKeys ); |
| 81 |
|
| 82 |
// System diagnostics aren't post meta, so add them for the copied payload. |
| 83 |
const copyMeta = { |
| 84 |
...meta, |
| 85 |
plugin_version: pluginVersion, |
| 86 |
wp_version: wpVersion, |
| 87 |
wp_post_id: wpPostId, |
| 88 |
}; |
| 89 |
|
| 90 |
const handleRemoveButtonClick = ( e ) => { |
| 91 |
e.stopPropagation(); |
| 92 |
|
| 93 |
if ( removed ) { |
| 94 |
setRemoved( false ); |
| 95 |
setDeleteContent( false ); |
| 96 |
removeWarningNotice(); |
| 97 |
} else { |
| 98 |
setRemoved( true ); |
| 99 |
setDeleteContent( true ); |
| 100 |
createWarningNotice(); |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
const copyToClipboardRef = useCopyToClipboard( |
| 105 |
getTextToCopy( copyMeta, currentMetaKeys, deprecatedMetaKeys ), |
| 106 |
() => { |
| 107 |
createNotice( |
| 108 |
'info', |
| 109 |
__( 'Copied data to clipboard.', 'speechkit' ), |
| 110 |
{ |
| 111 |
isDismissible: true, |
| 112 |
type: 'snackbar', |
| 113 |
} |
| 114 |
); |
| 115 |
} |
| 116 |
); |
| 117 |
|
| 118 |
return ( |
| 119 |
<PanelBody |
| 120 |
title={ __( 'Inspect', 'speechkit' ) } |
| 121 |
initialOpen={ false } |
| 122 |
className={ 'beyondwords beyondwords-sidebar__inspect' } |
| 123 |
> |
| 124 |
<Stack> |
| 125 |
<TextControl |
| 126 |
label="beyondwords_generate_audio" |
| 127 |
readOnly |
| 128 |
value={ beyondwordsGenerateAudio } |
| 129 |
__next40pxDefaultSize |
| 130 |
/> |
| 131 |
|
| 132 |
<TextControl |
| 133 |
label="beyondwords_integration_method" |
| 134 |
readOnly |
| 135 |
value={ beyondwordsIntegrationMethod } |
| 136 |
__next40pxDefaultSize |
| 137 |
/> |
| 138 |
|
| 139 |
<TextControl |
| 140 |
label="beyondwords_project_id" |
| 141 |
readOnly |
| 142 |
value={ beyondwordsProjectId } |
| 143 |
__next40pxDefaultSize |
| 144 |
/> |
| 145 |
|
| 146 |
<TextControl |
| 147 |
label="beyondwords_preview_token" |
| 148 |
readOnly |
| 149 |
value={ beyondwordsPreviewToken } |
| 150 |
__next40pxDefaultSize |
| 151 |
/> |
| 152 |
|
| 153 |
<TextControl |
| 154 |
label="beyondwords_content_id" |
| 155 |
readOnly |
| 156 |
value={ beyondwordsContentId } |
| 157 |
__next40pxDefaultSize |
| 158 |
/> |
| 159 |
|
| 160 |
<TextControl |
| 161 |
label="beyondwords_language_code" |
| 162 |
readOnly |
| 163 |
value={ beyondwordsLanguageCode } |
| 164 |
__next40pxDefaultSize |
| 165 |
/> |
| 166 |
|
| 167 |
<TextControl |
| 168 |
label="beyondwords_language_id" |
| 169 |
readOnly |
| 170 |
value={ beyondwordsLanguageId } |
| 171 |
__next40pxDefaultSize |
| 172 |
/> |
| 173 |
|
| 174 |
<TextControl |
| 175 |
label="beyondwords_body_voice_id" |
| 176 |
readOnly |
| 177 |
value={ beyondwordsBodyVoiceId } |
| 178 |
__next40pxDefaultSize |
| 179 |
/> |
| 180 |
|
| 181 |
<TextareaControl |
| 182 |
label="beyondwords_error_message" |
| 183 |
readOnly |
| 184 |
rows="3" |
| 185 |
value={ beyondwordsErrorMessage } |
| 186 |
__next40pxDefaultSize |
| 187 |
/> |
| 188 |
|
| 189 |
<TextControl |
| 190 |
label="beyondwords_delete_content" |
| 191 |
readOnly |
| 192 |
value={ beyondwordsDeleteContent } |
| 193 |
__next40pxDefaultSize |
| 194 |
/> |
| 195 |
|
| 196 |
<TextControl |
| 197 |
label="beyondwords_source" |
| 198 |
readOnly |
| 199 |
value={ beyondwordsSource } |
| 200 |
__next40pxDefaultSize |
| 201 |
/> |
| 202 |
|
| 203 |
<TextControl |
| 204 |
label="beyondwords_output" |
| 205 |
readOnly |
| 206 |
value={ beyondwordsOutput } |
| 207 |
__next40pxDefaultSize |
| 208 |
/> |
| 209 |
|
| 210 |
<TextControl |
| 211 |
label="beyondwords_script_template_id" |
| 212 |
readOnly |
| 213 |
value={ beyondwordsScriptTemplateId } |
| 214 |
__next40pxDefaultSize |
| 215 |
/> |
| 216 |
|
| 217 |
<TextControl |
| 218 |
label="beyondwords_video_template_id" |
| 219 |
readOnly |
| 220 |
value={ beyondwordsVideoTemplateId } |
| 221 |
__next40pxDefaultSize |
| 222 |
/> |
| 223 |
|
| 224 |
<TextControl |
| 225 |
label="beyondwords_video_size" |
| 226 |
readOnly |
| 227 |
value={ beyondwordsVideoSize } |
| 228 |
__next40pxDefaultSize |
| 229 |
/> |
| 230 |
|
| 231 |
<TextControl |
| 232 |
label="beyondwords_embed" |
| 233 |
readOnly |
| 234 |
value={ beyondwordsEmbed } |
| 235 |
__next40pxDefaultSize |
| 236 |
/> |
| 237 |
</Stack> |
| 238 |
|
| 239 |
<hr /> |
| 240 |
|
| 241 |
<Button |
| 242 |
id="beyondwords-inspect-copy" |
| 243 |
variant="primary" |
| 244 |
ref={ copyToClipboardRef } |
| 245 |
disabled={ removed } |
| 246 |
> |
| 247 |
{ __( 'Copy', 'speechkit' ) } |
| 248 |
</Button> |
| 249 |
|
| 250 |
<Button |
| 251 |
isDestructive |
| 252 |
style={ { float: 'right' } } |
| 253 |
id="beyondwords-inspect-remove" |
| 254 |
onClick={ handleRemoveButtonClick } |
| 255 |
disabled={ ! hasData } |
| 256 |
> |
| 257 |
{ removed |
| 258 |
? __( 'Restore', 'speechkit' ) |
| 259 |
: __( 'Remove', 'speechkit' ) } |
| 260 |
</Button> |
| 261 |
</PanelBody> |
| 262 |
); |
| 263 |
} |
| 264 |
|
| 265 |
export default compose( [ |
| 266 |
withSelect( ( select ) => { |
| 267 |
const { |
| 268 |
didPostSaveRequestSucceed, |
| 269 |
getCurrentPostId, |
| 270 |
getCurrentPostType, |
| 271 |
getEditedPostAttribute, |
| 272 |
isSavingPost, |
| 273 |
isAutosavingPost, |
| 274 |
} = select( 'core/editor' ); |
| 275 |
|
| 276 |
const { getSettings } = select( 'beyondwords/settings' ); |
| 277 |
|
| 278 |
const { pluginVersion, wpVersion, inspectMetaKeys } = getSettings(); |
| 279 |
|
| 280 |
return { |
| 281 |
beyondwordsDeleteContent: |
| 282 |
getEditedPostAttribute( 'meta' ).beyondwords_delete_content, |
| 283 |
beyondwordsGenerateAudio: |
| 284 |
getEditedPostAttribute( 'meta' ).beyondwords_generate_audio, |
| 285 |
beyondwordsIntegrationMethod: |
| 286 |
getEditedPostAttribute( 'meta' ).beyondwords_integration_method, |
| 287 |
beyondwordsContentId: |
| 288 |
getEditedPostAttribute( 'meta' ).beyondwords_content_id, |
| 289 |
beyondwordsPreviewToken: |
| 290 |
getEditedPostAttribute( 'meta' ).beyondwords_preview_token, |
| 291 |
beyondwordsLanguageCode: |
| 292 |
getEditedPostAttribute( 'meta' ).beyondwords_language_code, |
| 293 |
beyondwordsLanguageId: |
| 294 |
getEditedPostAttribute( 'meta' ).beyondwords_language_id, |
| 295 |
beyondwordsBodyVoiceId: |
| 296 |
getEditedPostAttribute( 'meta' ).beyondwords_body_voice_id, |
| 297 |
beyondwordsProjectId: |
| 298 |
getEditedPostAttribute( 'meta' ).beyondwords_project_id, |
| 299 |
beyondwordsErrorMessage: |
| 300 |
getEditedPostAttribute( 'meta' ).beyondwords_error_message, |
| 301 |
beyondwordsSource: |
| 302 |
getEditedPostAttribute( 'meta' ).beyondwords_source, |
| 303 |
beyondwordsOutput: |
| 304 |
getEditedPostAttribute( 'meta' ).beyondwords_output, |
| 305 |
beyondwordsScriptTemplateId: |
| 306 |
getEditedPostAttribute( 'meta' ).beyondwords_script_template_id, |
| 307 |
beyondwordsVideoTemplateId: |
| 308 |
getEditedPostAttribute( 'meta' ).beyondwords_video_template_id, |
| 309 |
beyondwordsVideoSize: |
| 310 |
getEditedPostAttribute( 'meta' ).beyondwords_video_size, |
| 311 |
beyondwordsEmbed: |
| 312 |
getEditedPostAttribute( 'meta' ).beyondwords_embed, |
| 313 |
// Live post meta + key lists (from PHP) that drive the Copy/Remove controls. |
| 314 |
meta: getEditedPostAttribute( 'meta' ), |
| 315 |
currentMetaKeys: inspectMetaKeys?.current, |
| 316 |
deprecatedMetaKeys: inspectMetaKeys?.deprecated, |
| 317 |
pluginVersion, |
| 318 |
wpVersion, |
| 319 |
wpPostId: getCurrentPostId(), |
| 320 |
currentPostType: getCurrentPostType(), |
| 321 |
didPostSaveRequestSucceed: didPostSaveRequestSucceed(), |
| 322 |
isSavingPost: isSavingPost(), |
| 323 |
isAutosavingPost: isAutosavingPost(), |
| 324 |
}; |
| 325 |
} ), |
| 326 |
withDispatch( ( dispatch ) => { |
| 327 |
const { editPost } = dispatch( 'core/editor' ); |
| 328 |
const { createNotice, removeNotice } = dispatch( 'core/notices' ); |
| 329 |
|
| 330 |
return { |
| 331 |
createWarningNotice: () => |
| 332 |
createNotice( |
| 333 |
'warning', |
| 334 |
__( |
| 335 |
'The BeyondWords data for this post will be removed when the post is saved.', |
| 336 |
'speechkit' |
| 337 |
), |
| 338 |
{ |
| 339 |
id: 'beyondwords-remove-post-data--warning', |
| 340 |
isDismissible: false, |
| 341 |
speak: true, |
| 342 |
} |
| 343 |
), |
| 344 |
removeWarningNotice: () => |
| 345 |
removeNotice( 'beyondwords-remove-post-data--warning' ), |
| 346 |
setDeleteContent: ( deleteContent ) => { |
| 347 |
editPost( { |
| 348 |
meta: { |
| 349 |
beyondwords_delete_content: deleteContent ? '1' : '', |
| 350 |
}, |
| 351 |
} ); |
| 352 |
}, |
| 353 |
}; |
| 354 |
} ), |
| 355 |
] )( PostInspectPanel ); |
| 356 |
|