| 1 |
import { BaseControl, Panel, PanelBody } from '@wordpress/components' |
| 2 |
import { useSelect } from '@wordpress/data' |
| 3 |
import { useEffect, useState } from '@wordpress/element' |
| 4 |
import { __ } from '@wordpress/i18n' |
| 5 |
import { Completion } from '@draft/components/Completion' |
| 6 |
import { DraftMenu } from '@draft/components/DraftMenu' |
| 7 |
import { EditMenu } from '@draft/components/EditMenu' |
| 8 |
import { Input } from '@draft/components/Input' |
| 9 |
import { InsertMenu } from '@draft/components/InsertMenu' |
| 10 |
import { useCompletion } from '@draft/hooks/useCompletion' |
| 11 |
|
| 12 |
export const Draft = () => { |
| 13 |
const [inputText, setInputText] = useState('') |
| 14 |
const [ready, setReady] = useState(false) |
| 15 |
const [prompt, setPrompt] = useState({ |
| 16 |
text: '', |
| 17 |
promptType: '', |
| 18 |
systemMessageKey: '', |
| 19 |
}) |
| 20 |
const { completion, loading, error } = useCompletion( |
| 21 |
prompt.text, |
| 22 |
prompt.promptType, |
| 23 |
prompt.systemMessageKey, |
| 24 |
) |
| 25 |
const selectedBlockClientIds = useSelect( |
| 26 |
(select) => select('core/block-editor').getSelectedBlockClientIds(), |
| 27 |
[], |
| 28 |
) |
| 29 |
const { getBlock } = useSelect((select) => select('core/block-editor'), []) |
| 30 |
|
| 31 |
// Reset input text when an error occurs |
| 32 |
useEffect(() => { |
| 33 |
if (!error) return |
| 34 |
setInputText(prompt.text) |
| 35 |
}, [error, prompt.text]) |
| 36 |
|
| 37 |
const canEditContent = () => { |
| 38 |
if (selectedBlockClientIds.length === 0) { |
| 39 |
return false |
| 40 |
} |
| 41 |
const targetBlock = getBlock(selectedBlockClientIds[0]) |
| 42 |
if (!targetBlock) { |
| 43 |
return false |
| 44 |
} |
| 45 |
return ( |
| 46 |
typeof targetBlock?.attributes?.content !== 'undefined' && |
| 47 |
targetBlock?.attributes?.content !== '' |
| 48 |
) |
| 49 |
} |
| 50 |
|
| 51 |
return ( |
| 52 |
<> |
| 53 |
<Panel> |
| 54 |
<PanelBody> |
| 55 |
<div className="rounded-sm border-none bg-gray-100 overflow-hidden mb-4"> |
| 56 |
<Input |
| 57 |
inputText={inputText} |
| 58 |
setInputText={setInputText} |
| 59 |
ready={ready} |
| 60 |
setReady={setReady} |
| 61 |
setPrompt={setPrompt} |
| 62 |
loading={loading} |
| 63 |
/> |
| 64 |
{completion && ( |
| 65 |
<> |
| 66 |
<hr className="mx-5 my-0 border-gray-300" /> |
| 67 |
<Completion completion={completion} /> |
| 68 |
</> |
| 69 |
)} |
| 70 |
{error && ( |
| 71 |
<div className="px-4 mb-4 mt-2"> |
| 72 |
<p className="m-0 text-xs font-semibold text-red-500"> |
| 73 |
{error.message} |
| 74 |
</p> |
| 75 |
</div> |
| 76 |
)} |
| 77 |
</div> |
| 78 |
{(completion || loading) && !error && ( |
| 79 |
<InsertMenu |
| 80 |
prompt={prompt} |
| 81 |
completion={completion} |
| 82 |
setPrompt={setPrompt} |
| 83 |
setInputText={setInputText} |
| 84 |
loading={loading} |
| 85 |
/> |
| 86 |
)} |
| 87 |
{!loading && !completion && canEditContent() && ( |
| 88 |
<BaseControl label={__('Edit or review', 'extendify')}> |
| 89 |
<EditMenu |
| 90 |
completion={completion} |
| 91 |
disabled={loading} |
| 92 |
setInputText={setInputText} |
| 93 |
setPrompt={setPrompt} |
| 94 |
/> |
| 95 |
</BaseControl> |
| 96 |
)} |
| 97 |
{!loading && !completion && !canEditContent() && ( |
| 98 |
<BaseControl label={__('Draft with AI', 'extendify')}> |
| 99 |
<DraftMenu |
| 100 |
disabled={loading} |
| 101 |
setInputText={setInputText} |
| 102 |
setReady={setReady} |
| 103 |
/> |
| 104 |
</BaseControl> |
| 105 |
)} |
| 106 |
</PanelBody> |
| 107 |
</Panel> |
| 108 |
{window.extendifyData?.devbuild && ( |
| 109 |
<Panel> |
| 110 |
<PanelBody title="Debug" initialOpen={false}> |
| 111 |
<label>prompt text:</label> |
| 112 |
<pre className="whitespace-pre-wrap">{prompt.text}</pre> |
| 113 |
<label>prompt system message:</label> |
| 114 |
<pre className="whitespace-pre-wrap"> |
| 115 |
{prompt.systemMessageKey} |
| 116 |
</pre> |
| 117 |
<label>completion:</label> |
| 118 |
<pre className="whitespace-pre-wrap">{completion}</pre> |
| 119 |
<label>error:</label> |
| 120 |
<pre className="whitespace-pre-wrap"> |
| 121 |
{error?.message ?? ''} |
| 122 |
</pre> |
| 123 |
<label> |
| 124 |
loading:{' '} |
| 125 |
{loading ? <span>true</span> : <span>false</span>} |
| 126 |
</label> |
| 127 |
</PanelBody> |
| 128 |
</Panel> |
| 129 |
)} |
| 130 |
</> |
| 131 |
) |
| 132 |
} |
| 133 |
|