-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathSummarizationBlockControls.tsx
More file actions
84 lines (73 loc) · 1.84 KB
/
SummarizationBlockControls.tsx
File metadata and controls
84 lines (73 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* External dependencies
*/
import React from 'react';
/**
* WordPress dependencies
*/
import { BlockControls } from '@wordpress/block-editor';
import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
import { createHigherOrderComponent } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { update } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { useSummaryGeneration } from '../functions/useSummaryGeneration';
const { aiSummarizationData } = window as any;
/**
* Block controls component.
*/
const Controls = () => {
const { isSummarizing, hasSummary, handleSummarize } =
useSummaryGeneration();
let buttonLabel: string = __( 'Generate Summary', 'ai' );
if ( isSummarizing ) {
buttonLabel = __( 'Generating…', 'ai' );
} else if ( hasSummary ) {
buttonLabel = __( 'Re-generate Summary', 'ai' );
}
// Don't render if disabled.
if ( ! aiSummarizationData?.enabled ) {
return null;
}
return (
<BlockControls>
<ToolbarGroup>
<ToolbarButton
label={ buttonLabel }
icon={ update }
className="ai-summarization-block-controls-button"
onClick={ handleSummarize }
disabled={ isSummarizing }
isBusy={ isSummarizing }
/>
</ToolbarGroup>
</BlockControls>
);
};
/**
* Add custom block controls to the summarization block.
*/
const SummarizationBlockControls = createHigherOrderComponent(
( BlockEdit: React.ComponentType< any > ) => {
return ( props: any ) => {
const {
name,
isSelected,
attributes: { aiGeneratedSummary = false },
} = props;
if ( name !== 'core/paragraph' || ! aiGeneratedSummary ) {
return <BlockEdit { ...props } />;
}
return (
<>
{ isSelected && <Controls { ...props } /> }
<BlockEdit { ...props } />
</>
);
};
},
'addBlockControls'
);
export default SummarizationBlockControls;