/** * WordPress dependencies */ import { Button } from '@safe-wordpress/components'; import { useSelect, useDispatch } from '@safe-wordpress/data'; import { useEffect, useState } from '@safe-wordpress/element'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import clsx from 'clsx'; import { isEqual, some, trim } from 'lodash'; import { SaveButton } from '@nelio-content/components'; import { isDefined } from '@nelio-content/utils'; import type { Maybe, PostStatusSlug } from '@nelio-content/types'; /** * Internal dependencies */ import './style.scss'; import { useAreStatusesDirty, useIsSaving, } from '~/nelio-content-pages/settings/custom-statuses/hooks'; import { store as NC_CUSTOM_STATUSES } from '~/nelio-content-pages/settings/custom-statuses/store'; import { Status } from '../status'; export const StatusesList = (): JSX.Element | null => { const isStatusEditorOpen = useSelect( ( select ) => select( NC_CUSTOM_STATUSES ).isStatusEditorOpen(), [] ); const sts = useSelect( ( select ) => select( NC_CUSTOM_STATUSES ).getStatuses(), [] ); const statuses = sts.filter( ( s ) => ! [ 'trash', 'nelio-content-unscheduled' ].includes( s ) ); const [ currentStatuses, setCurrentStatuses ] = useState( statuses ); const isSaving = useIsSaving(); const areStatusesDirty = useAreStatusesDirty(); const error = useValidationError(); const [ isDragging, setIsDragging ] = useState( false ); const { openStatusEditor, reorderStatuses, saveStatuses } = useDispatch( NC_CUSTOM_STATUSES ); useEffect( () => { if ( ! isEqual( statuses, currentStatuses ) && statuses.length === currentStatuses.length && ! isDragging ) { void reorderStatuses( currentStatuses ); } //end if }, [ statuses, isDragging, isStatusEditorOpen, currentStatuses, reorderStatuses, ] ); useEffect( () => { if ( statuses.length !== currentStatuses.length ) { setCurrentStatuses( statuses ); } //end if }, [ statuses, currentStatuses.length ] ); if ( ! currentStatuses.length ) { return null; } const onMove = ( dragIndex: number, hoverIndex: number ) => { const aux = [ ...currentStatuses ]; aux.splice( dragIndex, 1 ); aux.splice( hoverIndex, 0, currentStatuses[ dragIndex ] as PostStatusSlug ); setCurrentStatuses( aux ); }; return ( <>