/**
* External dependencies
*/
import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { createInterpolateElement, createPortal, render, useEffect, useState, useRef, forwardRef } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import { ifCondition, compose, pure } from '@wordpress/compose';
import { Popover, Button } from '@wordpress/components';
import { Icon, info } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';
import { logInteractionEvent } from './instrumentation';
const EID_PREFIX = 'wp.editor';
/**
* Local dependencies
*/
import { HIRE_EXPERT_SETTINGS_ID } from './settings';
import './index.scss';
function clickSettingsButton() {
document.querySelector('.interface-more-menu-dropdown > button')?.click?.();
}
/**
* This functional component renders null and instead uses a 'hacky' approach
* to modify the editor toolbar to include Expert Link.
*
* @param {Object} props Contains the props such as 'editorToolbar'
* @returns null
*/
function ExpertBanner(props) {
const { editorToolbar } = props;
const [ target, setTarget ] = useState(null);
const [ tooltip, setTooltip ] = useState(false);
const [ settingsPanelOpen, setSettingsPanel ] = useState(false);
const [ settingsAnchor, setSettingsAnchor ] = useState(null);
const iconRef = useRef(null);
const settingsBtnId = 'wpex-eb-open-settings';
const popoverId = 'wpex-eb-tooltip-popover';
useEffect(() => {
const div = document.createElement('div');
div.id = 'wpex-eb-container';
editorToolbar.parentNode.insertBefore(div, editorToolbar);
setTarget(div);
}, [editorToolbar]);
useEffect(function settingsPanel() {
if (!settingsPanelOpen) {
return;
}
clickSettingsButton();
setTimeout(() => {
const settingButton = document.getElementById(HIRE_EXPERT_SETTINGS_ID);
if (!settingButton) {
return;
}
settingButton.focus();
const settingButtonContainer = settingButton.getBoundingClientRect();
setSettingsAnchor(new DOMRect(
settingButtonContainer.x + 20,
settingButtonContainer.y,
settingButtonContainer.width,
settingButtonContainer.height
));
}, 200);
setSettingsPanel(false);
}, [settingsPanelOpen, setSettingsAnchor]);
const handleClickOutside = ( e ) => {
e.stopPropagation();
e.preventDefault();
};
useEffect( () => {
if (!settingsAnchor){
return;
}
document.addEventListener( 'mousedown', handleClickOutside );
document.addEventListener( 'click', handleClickOutside );
return () => {
document.removeEventListener( 'mousedown', handleClickOutside );
document.removeEventListener( 'click', handleClickOutside );
};
}, [settingsAnchor] );
if (!target) {
return null;
}
const iconRect = iconRef.current?.getBoundingClientRect();
const targetRect = new DOMRect(
iconRect?.x,
iconRect?.y + 10,
iconRect?.width,
iconRect?.height
)
return createPortal(
<>
{
settingsAnchor && (
{ __( 'Just so you know…', 'gd-system-plugin' ) }
{ __( 'Here you can enable or disable "Hire an expert"', 'gd-system-plugin' ) }