/** * WordPress dependencies */ import { store as CORE } from '@safe-wordpress/core-data'; import { useSelect } from '@safe-wordpress/data'; import { useMemo } from '@safe-wordpress/element'; import { applyFilters } from '@safe-wordpress/hooks'; /** * External dependencies */ import { EMPTY_ARRAY } from '@nelio-content/constants'; import { store as NC_DATA } from '@nelio-content/data'; import type { PostTypeContext, Taxonomy } from '@nelio-content/types'; /** * Internal dependencies */ import { store as NC_POST_EDITOR } from './store'; export const useIsDisabled = (): boolean => { const isDisabled = useIsDisabledUI(); const isSaving = useIsSaving(); const canEdit = useCanEditPost(); return isDisabled || isSaving || ! canEdit; }; export const useIsPublished = (): boolean => useSelect( ( select ) => select( NC_POST_EDITOR ).isPublished(), [] ); export const useIsSubscribed = (): boolean => useSelect( ( select ) => select( NC_DATA ).isSubscribed(), [] ); export const useIsDisabledUI = (): boolean => useSelect( ( select ) => select( NC_POST_EDITOR ).isDisabled(), [] ); export const useIsSaving = (): boolean => useSelect( ( select ) => select( NC_POST_EDITOR ).isSaving(), [] ); export const useIsNew = (): boolean => useSelect( ( select ) => select( NC_POST_EDITOR ).isNewPost(), [] ); export const useCanEditPost = (): boolean => useSelect( ( select ) => { const editor = select( NC_POST_EDITOR ); const data = select( NC_DATA ); const id = editor.getId(); const post = id ? data.getPost( id ) : undefined; return ( editor.isNewPost() || !! ( post && data.canCurrentUserEditPost( post ) ) ); }, [] ); export const useSupportsTaxonomies = (): boolean => !! useSupportedTaxonomies().length; export const useSupportedTaxonomies = (): ReadonlyArray< Taxonomy > => { const postType = useSelect( ( select ) => select( NC_POST_EDITOR ).getPostType(), [] ); const taxonomies = useAllTaxonomies(); return useMemo( () => taxonomies.filter( ( t ) => ! postType || t.types.includes( postType ) ), [ taxonomies, postType ] ); }; export const useIsFeatureEnabled = ( context: PostTypeContext ): boolean => useSelect( ( select ) => { const type = select( NC_POST_EDITOR ).getPostType(); const types = select( NC_DATA ).getPostTypes( context ); return types.some( ( t ) => t.name === type ); }, [ context ] ); export const useMayHaveExtraInfo = (): boolean => [ useSupportsTaxonomies(), useIsFeatureEnabled( 'future-actions' ), useIsFeatureEnabled( 'series' ), useIsFeatureEnabled( 'tasks' ), useIsFeatureEnabled( 'comments' ), useIsFeatureEnabled( 'references' ), ].some( ( x ) => x ); // ======= // HELPERS // ======= const TAX_QUERY = { per_page: -1 } as const; const useAllTaxonomies = (): ReadonlyArray< Taxonomy > => { const records = useSelect( ( select ) => ( select( CORE ).getEntityRecords( 'root', 'taxonomy', TAX_QUERY ) as ReadonlyArray< Taxonomy > | null | undefined ) ?? EMPTY_ARRAY, [] ); const visible = useMemo( () => records.filter( ( t ) => t.visibility.public ), [ records ] ); return applyFilters( 'nelio-content_post-quick-editor_allTaxonomies', visible ) as ReadonlyArray< Taxonomy >; };