/** * WordPress dependencies */ import { useSelect } from '@safe-wordpress/data'; import { useEffect } from '@safe-wordpress/element'; import { _x, sprintf } from '@safe-wordpress/i18n'; /** * External dependencies */ import { find } from 'lodash'; import { store as NC_DATA } from '@nelio-content/data'; import { getRequirementsError } from '@nelio-content/networks'; import { isAutoSelectedUuid } from '@nelio-content/utils'; import type { SocialProfile } from '@nelio-content/types'; /** * Internal dependencies */ import { useActiveSocialNetwork, useDate, useMediaImage, useMediaVideo, useRelatedPostId, useSelectedProfiles, useSelectedTargets, useText, useTextComputed, useTikTokSettings, useTime, useTypeByNetwork, useValidationError, useWebhookSettings, } from '../hooks'; export const ErrorDetector = (): null => { const [ { dateValue } ] = useDate(); const [ { timeValue } ] = useTime(); const [ text ] = useText(); const textComputed = useTextComputed(); const postId = useRelatedPostId(); const network = useActiveSocialNetwork(); const [ profileIds ] = useSelectedProfiles(); const profiles = useSelect( ( select ) => profileIds .map( ( id ) => ( isAutoSelectedUuid( id ) ? id.value : id ) ) .map( ( id ) => select( NC_DATA ).getSocialProfile( id ) ) .filter( ( p ): p is SocialProfile => !! p ), [ profileIds ] ); const targets = useSelectedTargets(); const typeByNetwork = useTypeByNetwork(); const [ tiktokSettings ] = useTikTokSettings(); const allWebhookSettings = useWebhookSettings(); // eslint-disable-next-line @typescript-eslint/no-unused-vars const [ _, setError ] = useValidationError(); const image = useMediaImage(); const video = useMediaVideo(); useEffect( () => { if ( ! profiles.length ) { return void setError( _x( 'Please select a social profile', 'user', 'nelio-content' ) ); } if ( text.includes( '{highlight}' ) ) { return void setError( sprintf( /* translators: %s: Placeholder. */ _x( 'Please remove %s (social template only)', 'user', 'nelio-content' ), '{highlight}' ) ); } if ( text.includes( '{phrase}' ) ) { return void setError( sprintf( /* translators: %s: Placeholder. */ _x( 'Please remove %s, as it can only be used in social templates', 'user', 'nelio-content' ), '{phrase}' ) ); } if ( ! dateValue ) { return void setError( _x( 'Please specify a date', 'user', 'nelio-content' ) ); } if ( ! timeValue ) { return void setError( _x( 'Please specify a time', 'user', 'nelio-content' ) ); } const p = find( profiles, { network } ); const allProfiles = !! p ? [ p, ...profiles ] : profiles; for ( const profile of allProfiles ) { const error = getRequirementsError( { network: profile.network, postId, targetName: targets[ profile.id ]?.[ 0 ], textComputed, type: typeByNetwork[ profile.network ], tiktokSettings, webhookSettings: allWebhookSettings[ profile.id ], image, video, } ); if ( error ) { return void setError( error ); } } const clearErrors = () => setError( '' ); return void clearErrors(); }, [ dateValue, image, network, postId, profileIds, targets, textComputed, timeValue, tiktokSettings, typeByNetwork, video, setError, profiles, text, allWebhookSettings, ] ); return null; };