| 1 |
import { ConfirmationModal } from '@page-creator/components/ConfirmationModal'; |
| 2 |
import { PageGen } from '@page-creator/icons/ai-gen'; |
| 3 |
import { useGlobalsStore } from '@page-creator/state/global'; |
| 4 |
import { useActivityStore } from '@shared/state/activity'; |
| 5 |
import { useSelect } from '@wordpress/data'; |
| 6 |
import { store as editorStore } from '@wordpress/editor'; |
| 7 |
import { useState } from '@wordpress/element'; |
| 8 |
import { __ } from '@wordpress/i18n'; |
| 9 |
import { Icon } from '@wordpress/icons'; |
| 10 |
|
| 11 |
export const MainButton = () => { |
| 12 |
const { setOpen } = useGlobalsStore(); |
| 13 |
const { incrementActivity } = useActivityStore(); |
| 14 |
const [confirmationOpen, setConfirmationOpen] = useState(false); |
| 15 |
// Get post attributes using WordPress's useSelect hook |
| 16 |
const isEmptyPage = useSelect( |
| 17 |
(select) => select(editorStore).isEditedPostEmpty(), |
| 18 |
[], |
| 19 |
); |
| 20 |
|
| 21 |
const handleClick = () => { |
| 22 |
// Minimize HC if its open |
| 23 |
window.dispatchEvent(new CustomEvent('extendify-hc:minimize')); |
| 24 |
if (!isEmptyPage) return setConfirmationOpen(true); |
| 25 |
|
| 26 |
setOpen(true); |
| 27 |
incrementActivity('page-creator-button-click'); |
| 28 |
}; |
| 29 |
|
| 30 |
return ( |
| 31 |
<> |
| 32 |
{confirmationOpen && ( |
| 33 |
<ConfirmationModal |
| 34 |
setConfirmationOpen={setConfirmationOpen} |
| 35 |
setModalOpen={setOpen} |
| 36 |
confirmationOpen={confirmationOpen} |
| 37 |
/> |
| 38 |
)} |
| 39 |
{/* biome-ignore lint: allow button role here */} |
| 40 |
<div |
| 41 |
role="button" |
| 42 |
onClick={handleClick} |
| 43 |
onKeyDown={(e) => { |
| 44 |
if (!(e.key === 'Enter' || e.key === ' ')) return; |
| 45 |
handleClick(); |
| 46 |
}} |
| 47 |
tabIndex={0} |
| 48 |
className="components-button has-icon is-primary ml-3 h-8 min-w-0 cursor-pointer px-2 xs:h-9 sm:ml-2 xl:pr-3" |
| 49 |
> |
| 50 |
<Icon icon={PageGen} size={24} className="fill-none" /> |
| 51 |
<span className="ml-1 hidden xl:inline"> |
| 52 |
{__('AI Page Generator', 'extendify-local')} |
| 53 |
</span> |
| 54 |
</div> |
| 55 |
</> |
| 56 |
); |
| 57 |
}; |
| 58 |
|