/** * WordPress dependencies */ import { Button, DropdownMenu, MenuGroup, MenuItem, } from '@safe-wordpress/components'; import { useDispatch } from '@safe-wordpress/data'; import { _x, sprintf } from '@safe-wordpress/i18n'; import { MediaUpload } from '@safe-wordpress/media-utils'; /** * External dependencies */ import clsx from 'clsx'; import { isArray } from 'lodash'; import { store as NC_DATA } from '@nelio-content/data'; import { getNetworkLabel, getSupportedImageTypes, } from '@nelio-content/networks'; import type { MediaUploadItem } from '@nelio-content/types'; /** * Internal dependencies */ import { store as NC_SOCIAL_EDITOR } from '../../store'; import { useActiveNetworkType, useActiveSocialNetwork, useDoesActiveNetworkSupport, useHasExplicitImage, } from '../../hooks'; // NOTE. This is a workaround because, for some reason, clicking outside the dropdown doesn’t work if the click is still inside the modal window. let doCloseMenu: () => void; const closeMenuOnBlur = () => { if ( 'function' === typeof doCloseMenu ) { doCloseMenu(); } }; export type ImageToggleProps = { readonly disabled?: boolean; }; export const ImageToggle = ( { disabled, }: ImageToggleProps ): JSX.Element | null => { const supportsImage = useDoesActiveNetworkSupport( 'image' ); const supportsMultiImage = useDoesActiveNetworkSupport( 'multi-image' ); const type = useActiveNetworkType(); const activeNetwork = useActiveSocialNetwork(); const hasExplicitImage = useHasExplicitImage(); const { setImages } = useDispatch( NC_SOCIAL_EDITOR ); const { receiveMediaUploadItem } = useDispatch( NC_DATA ); if ( ! supportsImage ) { return null; } if ( [ 'image', 'multi-media' ].includes( type ) ) { return ; } if ( hasExplicitImage ) { return ; } return ( ) => { image = isArray( image ) ? image : [ image ]; image.forEach( ( img ) => { void receiveMediaUploadItem( img ); } ); void setImages( activeNetwork, image.map( ( img ) => img.id ), image.map( ( img ) => img.url ), image.map( ( img ) => img.alt ) ); } } render={ ( { open }: { open: () => void } ) => ( ) } /> ); }; // ============ // HELPER VIEWS // ============ const RemoveImage = ( { disabled }: { disabled?: boolean } ): JSX.Element => { const { removeImages } = useDispatch( NC_SOCIAL_EDITOR ); return (