| 1 |
import { magic, magicAnimated } from '@agent/icons'; |
| 2 |
import { useGlobalStore } from '@agent/state/global'; |
| 3 |
import { Button } from '@wordpress/components'; |
| 4 |
import { useEffect, useRef, useState } from '@wordpress/element'; |
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
import classNames from 'classnames'; |
| 7 |
|
| 8 |
export const PostEditor = () => { |
| 9 |
const { toggleOpen, open, isMobile } = useGlobalStore(); |
| 10 |
const [animate, setAnimate] = useState(false); |
| 11 |
const [animateIcon, setAnimateIcon] = useState(false); |
| 12 |
const pageLoaded = useRef(false); |
| 13 |
|
| 14 |
useEffect(() => { |
| 15 |
// Don't run this on the first page load |
| 16 |
if (!pageLoaded.current) { |
| 17 |
pageLoaded.current = true; |
| 18 |
return; |
| 19 |
} |
| 20 |
if (open || isMobile) return; |
| 21 |
setAnimate(true); |
| 22 |
setAnimateIcon(true); |
| 23 |
const id = setTimeout(() => { |
| 24 |
setAnimate(false); |
| 25 |
}, 1500); |
| 26 |
const iconId = setTimeout(() => { |
| 27 |
setAnimateIcon(false); |
| 28 |
}, 5000); |
| 29 |
return () => { |
| 30 |
clearTimeout(id); |
| 31 |
clearTimeout(iconId); |
| 32 |
}; |
| 33 |
}, [open, isMobile]); |
| 34 |
|
| 35 |
if (isMobile) return null; |
| 36 |
|
| 37 |
return ( |
| 38 |
<Button |
| 39 |
variant="primary" |
| 40 |
icon={animateIcon ? magicAnimated : magic} |
| 41 |
iconPosition="left" |
| 42 |
className="is-compact has-text relative z-10" |
| 43 |
onClick={() => { |
| 44 |
if (open) setAnimate(true); |
| 45 |
toggleOpen(); |
| 46 |
}} |
| 47 |
aria-label={__('Open Agent', 'extendify-local')} |
| 48 |
> |
| 49 |
<span |
| 50 |
className={classNames('px-1 leading-none', { |
| 51 |
'extendify-gradient-animation': animate, |
| 52 |
})} |
| 53 |
> |
| 54 |
{__('AI Agent', 'extendify-local')} |
| 55 |
</span> |
| 56 |
</Button> |
| 57 |
); |
| 58 |
}; |
| 59 |
|