| 1 |
import { Spinner } from '@wordpress/components'; |
| 2 |
import { __, isRTL } from '@wordpress/i18n'; |
| 3 |
import { arrowRight, Icon, arrowLeft } from '@wordpress/icons'; |
| 4 |
import classnames from 'classnames'; |
| 5 |
import { DynamicTextarea } from '@draft/components/DynamicTextarea'; |
| 6 |
import { useSelectedText } from '@draft/hooks/useSelectedText'; |
| 7 |
import { magic } from '@draft/svg'; |
| 8 |
|
| 9 |
export const Input = ({ |
| 10 |
inputText, |
| 11 |
setInputText, |
| 12 |
ready, |
| 13 |
setReady, |
| 14 |
setPrompt, |
| 15 |
loading, |
| 16 |
}) => { |
| 17 |
const { selectedText } = useSelectedText(); |
| 18 |
|
| 19 |
const submit = (event) => { |
| 20 |
event.preventDefault(); |
| 21 |
|
| 22 |
if (!ready || loading) return; |
| 23 |
|
| 24 |
setInputText(''); |
| 25 |
setReady(false); |
| 26 |
|
| 27 |
setPrompt({ |
| 28 |
text: selectedText ? selectedText : inputText, |
| 29 |
promptType: selectedText ? 'custom-requests' : 'create', |
| 30 |
systemMessageKey: selectedText ? 'edit' : 'generate', |
| 31 |
// The prompt as a followup to the user's input |
| 32 |
details: { followup: selectedText ? inputText : undefined }, |
| 33 |
}); |
| 34 |
}; |
| 35 |
|
| 36 |
return ( |
| 37 |
<form className="relative flex items-start" onSubmit={submit}> |
| 38 |
<Icon |
| 39 |
icon={magic} |
| 40 |
className="absolute left-2 top-3.5 h-5 w-5 fill-current text-wp-theme-main rtl:left-auto rtl:right-2" |
| 41 |
/> |
| 42 |
<DynamicTextarea |
| 43 |
disabled={loading} |
| 44 |
placeholder={ |
| 45 |
loading |
| 46 |
? __('AI is writing...', 'extendify-local') |
| 47 |
: selectedText |
| 48 |
? __('Ask AI to edit', 'extendify-local') |
| 49 |
: __('Ask AI to generate text', 'extendify-local') |
| 50 |
} |
| 51 |
value={inputText} |
| 52 |
className="h-full w-full resize-none overflow-hidden rounded-none border-transparent bg-transparent px-10 py-3 outline-none focus:ring-1 focus:ring-wp-theme-main" |
| 53 |
onChange={(event) => { |
| 54 |
setInputText(event.target.value); |
| 55 |
setReady(event.target.value.length > 0); |
| 56 |
}} |
| 57 |
onKeyDown={(event) => { |
| 58 |
if (event.key === 'Enter' && !event.shiftKey) { |
| 59 |
event.preventDefault(); |
| 60 |
submit(event); |
| 61 |
} |
| 62 |
}} |
| 63 |
/> |
| 64 |
{loading && ( |
| 65 |
<div className="absolute right-4 top-3.5 h-4 w-4 p-1 text-gray-700 rtl:left-4 rtl:right-auto"> |
| 66 |
<Spinner style={{ margin: '0' }} /> |
| 67 |
</div> |
| 68 |
)} |
| 69 |
{!loading && ( |
| 70 |
<button |
| 71 |
type="submit" |
| 72 |
disabled={!ready} |
| 73 |
aria-label={__('Submit', 'extendify-local')} |
| 74 |
className={classnames( |
| 75 |
'absolute right-2 top-3.5 border-none bg-transparent p-0 rtl:left-2 rtl:right-auto', |
| 76 |
{ |
| 77 |
'text-gray-700 hover:text-design-main': ready, |
| 78 |
'text-gray-500': !ready, |
| 79 |
}, |
| 80 |
)}> |
| 81 |
<Icon |
| 82 |
icon={isRTL() ? arrowLeft : arrowRight} |
| 83 |
onClick={submit} |
| 84 |
className="h-6 w-6 fill-current" |
| 85 |
/> |
| 86 |
</button> |
| 87 |
)} |
| 88 |
</form> |
| 89 |
); |
| 90 |
}; |
| 91 |
|