diff --git a/packages/editor/src/components/post-actions/actions.js b/packages/editor/src/components/post-actions/actions.js index 831a4f5349869b..e1c0ed1558193d 100644 --- a/packages/editor/src/components/post-actions/actions.js +++ b/packages/editor/src/components/post-actions/actions.js @@ -1,10 +1,7 @@ /** * WordPress dependencies */ -import { external } from '@wordpress/icons'; import { useDispatch, useSelect } from '@wordpress/data'; -import { store as coreStore } from '@wordpress/core-data'; -import { __ } from '@wordpress/i18n'; import { useMemo, useEffect } from '@wordpress/element'; /** @@ -13,30 +10,11 @@ import { useMemo, useEffect } from '@wordpress/element'; import { store as editorStore } from '../../store'; import { unlock } from '../../lock-unlock'; -const viewPostAction = { - id: 'view-post', - label: __( 'View' ), - isPrimary: true, - icon: external, - isEligible( post ) { - return post.status !== 'trash'; - }, - callback( posts, { onActionPerformed } ) { - const post = posts[ 0 ]; - window.open( post.link, '_blank' ); - if ( onActionPerformed ) { - onActionPerformed( posts ); - } - }, -}; - export function usePostActions( { postType, onActionPerformed, context } ) { - const { defaultActions, postTypeObject } = useSelect( + const { defaultActions } = useSelect( ( select ) => { - const { getPostType } = select( coreStore ); const { getEntityActions } = unlock( select( editorStore ) ); return { - postTypeObject: getPostType( postType ), defaultActions: getEntityActions( 'postType', postType ), }; }, @@ -48,23 +26,14 @@ export function usePostActions( { postType, onActionPerformed, context } ) { registerPostTypeActions( postType ); }, [ registerPostTypeActions, postType ] ); - const isLoaded = !! postTypeObject; return useMemo( () => { - if ( ! isLoaded ) { - return []; - } - - let actions = [ - postTypeObject?.viewable && viewPostAction, - ...defaultActions, - ].filter( Boolean ); // Filter actions based on provided context. If not provided // all actions are returned. We'll have a single entry for getting the actions // and the consumer should provide the context to filter the actions, if needed. // Actions should also provide the `context` they support, if it's specific, to // compare with the provided context to get all the actions. // Right now the only supported context is `list`. - actions = actions.filter( ( action ) => { + const actions = defaultActions.filter( ( action ) => { if ( ! action.context ) { return true; } @@ -119,11 +88,5 @@ export function usePostActions( { postType, onActionPerformed, context } ) { } return actions; - }, [ - defaultActions, - postTypeObject?.viewable, - onActionPerformed, - isLoaded, - context, - ] ); + }, [ defaultActions, onActionPerformed, context ] ); } diff --git a/packages/editor/src/dataviews/actions/view-post.tsx b/packages/editor/src/dataviews/actions/view-post.tsx new file mode 100644 index 00000000000000..47eb1a66d019ad --- /dev/null +++ b/packages/editor/src/dataviews/actions/view-post.tsx @@ -0,0 +1,30 @@ +/** + * WordPress dependencies + */ +import { external } from '@wordpress/icons'; +import { __ } from '@wordpress/i18n'; +import type { Action } from '@wordpress/dataviews'; + +/** + * Internal dependencies + */ +import type { BasePost } from '../types'; + +const viewPost: Action< BasePost > = { + id: 'view-post', + label: __( 'View' ), + isPrimary: true, + icon: external, + isEligible( post ) { + return post.status !== 'trash'; + }, + callback( posts, { onActionPerformed } ) { + const post = posts[ 0 ]; + window.open( post?.link, '_blank' ); + if ( onActionPerformed ) { + onActionPerformed( posts ); + } + }, +}; + +export default viewPost; diff --git a/packages/editor/src/dataviews/store/private-actions.ts b/packages/editor/src/dataviews/store/private-actions.ts index 3ac121aea7393b..a9101e57dd08b5 100644 --- a/packages/editor/src/dataviews/store/private-actions.ts +++ b/packages/editor/src/dataviews/store/private-actions.ts @@ -23,6 +23,7 @@ import { store as editorStore } from '../../store'; import { unlock } from '../../lock-unlock'; import duplicatePost from '../actions/duplicate-post'; import viewPostRevisions from '../actions/view-post-revisions'; +import viewPost from '../actions/view-post'; export function registerEntityAction< Item >( kind: string, @@ -89,6 +90,7 @@ export const registerPostTypeActions = .getCurrentTheme(); const actions = [ + postTypeConfig.viewable ? viewPost : undefined, !! postTypeConfig?.supports?.revisions ? viewPostRevisions : undefined, @@ -101,9 +103,10 @@ export const registerPostTypeActions = duplicatePost : undefined, postTypeConfig.slug === 'wp_template_part' && - canCreate && - currentTheme?.is_block_theme && - duplicateTemplatePart, + canCreate && + currentTheme?.is_block_theme + ? duplicateTemplatePart + : undefined, canCreate && postTypeConfig.slug === 'wp_block' ? duplicatePattern : undefined, @@ -121,7 +124,7 @@ export const registerPostTypeActions = registry.batch( () => { actions.forEach( ( action ) => { - if ( action === undefined ) { + if ( ! action ) { return; } unlock( registry.dispatch( editorStore ) ).registerEntityAction( diff --git a/packages/editor/src/dataviews/types.ts b/packages/editor/src/dataviews/types.ts index e5886792fb9c33..664c2dd417201c 100644 --- a/packages/editor/src/dataviews/types.ts +++ b/packages/editor/src/dataviews/types.ts @@ -34,6 +34,7 @@ export interface BasePost extends CommonPost { featured_media?: number; menu_order?: number; ping_status?: 'open' | 'closed'; + link?: string; } export interface Template extends CommonPost { @@ -72,6 +73,7 @@ export type PostWithPermissions = Post & { export interface PostType { slug: string; + viewable: boolean; supports?: { 'page-attributes'?: boolean; title?: boolean;