/** * The Quick Access Sidebar. */ import { useLayoutEffect, useState, useEffect } from '@wordpress/element'; import style from '../editor.lazy.scss'; import Blocks from './blocks'; import PopoverModal from './Modal'; import getApiData from '@Controls/getApiData'; import { __ } from '@wordpress/i18n'; import { Popover } from '@wordpress/components'; const Sidebar = () => { const [ defaultAllowedQuickSidebarBlocks, setDefaultAllowedQuickSidebarBlocks, ] = useState( srfm_quick_sidebar_blocks.allowed_blocks ); const [ isPopoverVisible, setPopoverVisible ] = useState( false ); // State to track the sorting status const [ isSorting, setSorting ] = useState( false ); const [ hovering, setHovering ] = useState( null ); // State to track the active tab const [ activeTab, setActiveTab ] = useState( 0 ); const handleMouseOver = ( button ) => setHovering( button ); const handleMouseOut = () => setHovering( false ); // Function to handle outside click useEffect( () => { const handleOutsideClick = ( event ) => { // Check if the click is outside the QAB sidebar and popover if ( document.querySelector( '.srfm-ee-quick-access' ) && ! document .querySelector( '.srfm-ee-quick-access' ) .contains( event.target ) && ! document.querySelector( '.popover' )?.contains( event.target ) ) { setPopoverVisible( false ); setSorting( false ); setActiveTab( 0 ); } }; // Add event listener when component mounts document.body.addEventListener( 'click', handleOutsideClick ); // Remove event listener when component unmounts return () => { document.body.removeEventListener( 'click', handleOutsideClick ); }; }, [] ); // Empty array ensures this effect runs only once when the component mounts // Function to handle tab click const handleTabClick = ( index ) => { setActiveTab( index ); }; useLayoutEffect( () => { style.use(); return () => { style.unuse(); }; }, [] ); // Function to open the popover const openPopover = () => { setPopoverVisible( true ); setSorting( false ); }; // Function to close the popover const closePopover = () => { setPopoverVisible( false ); setSorting( false ); }; // Function to enable re-arrange const enableRearrange = () => { setSorting( true ); }; function updateDefaultAllowedQuickSidebarBlocks( allowedBlocks ) { setDefaultAllowedQuickSidebarBlocks( allowedBlocks ); } // Saving the allowed blocks to the database. const saveOptionToDatabase = ( allowedBlocks ) => { // update allowedBlocks. updateDefaultAllowedQuickSidebarBlocks( allowedBlocks ); // Create an object with the srfm_ajax_nonce and confirmation properties. const data = { security: srfm_quick_sidebar_blocks.srfm_ajax_nonce, defaultAllowedQuickSidebarBlocks: JSON.stringify( allowedBlocks ), }; // Call the getApiData function with the specified parameters. getApiData( { url: srfm_quick_sidebar_blocks.srfm_ajax_url, action: 'srfm_global_update_allowed_block', data, } ); }; return ( <>