/** * WordPress dependencies */ import { Button, ExternalLink } from '@safe-wordpress/components'; import { useDispatch } from '@safe-wordpress/data'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import { map } from 'lodash'; import { PostSearcher } from '@nelio-content/components'; import { usePostStatuses, usePostTypes } from '@nelio-content/data'; /** * Internal dependencies */ import './style.scss'; import { store as NC_TASK_EDITOR } from '../../store'; import { useRelatedPost, useRelatedPostId, useIsRelatedPostHidden, useIsRelatedPostReadOnly, useRelatedPostStatus, } from '../../hooks'; export type MaybeRelatedPostProps = { readonly disabled?: boolean; }; export const MaybeRelatedPost = ( { disabled, }: MaybeRelatedPostProps ): JSX.Element | null => { const className = 'nelio-content-task-editor__related-post-controls'; const [ _, setPost ] = useRelatedPost(); const postId = useRelatedPostId(); const isHidden = useIsRelatedPostHidden(); const isReadOnly = useIsRelatedPostReadOnly(); const relatedPostStatus = useRelatedPostStatus(); const { setRelatedPostStatus } = useDispatch( NC_TASK_EDITOR ); const postTypes = map( usePostTypes( 'tasks' ), 'name' ); const postStatuses = usePostStatuses(); if ( isReadOnly ) { return ; } if ( isHidden || ! postTypes.length ) { return
; } if ( 'none' === relatedPostStatus ) { return (
); } if ( 'searcher' === relatedPostStatus ) { return (
void ( p && setPost( p ) ) } postTypes={ postTypes } postStatuses={ postStatuses } />
); } return
; }; const ReadOnlyRelatedPost = ( { className }: { className: string } ) => { const [ post ] = useRelatedPost(); const relatedPostStatus = useRelatedPostStatus(); if ( 'none' === relatedPostStatus ) { return
; } if ( 'loading' === relatedPostStatus ) { return
Loading...
; } if ( 'error' === relatedPostStatus || ! post ) { return (
{ _x( 'Related Post:', 'text', 'nelio-content' ) + ' ' } { _x( 'Not Found', 'text (post)', 'nelio-content' ) }
); } return (
{ _x( 'Related Post:', 'text', 'nelio-content' ) + ' ' } { post.title }
); };