Skip to content

Commit

Permalink
Add <ViewLink> component (#50260)
Browse files Browse the repository at this point in the history
Displays a button with an external icon to open
a new window with the published post type
  • Loading branch information
mtias authored May 3, 2023
1 parent dc026f1 commit 9046f21
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/edit-post/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import HeaderToolbar from './header-toolbar';
import MoreMenu from './more-menu';
import PostPublishButtonOrToggle from './post-publish-button-or-toggle';
import { default as DevicePreview } from '../device-preview';
import ViewLink from '../view-link';
import MainDashboardButton from './main-dashboard-button';
import { store as editPostStore } from '../../store';
import TemplateTitle from './template-title';
Expand Down Expand Up @@ -88,6 +89,7 @@ function Header( { setEntitiesSavedStatesCallback } ) {
showIconLabels={ showIconLabels }
/>
) }
<ViewLink />
<DevicePreview />
<PostPreviewButton
forceIsAutosaveable={ hasActiveMetaboxes }
Expand Down
37 changes: 37 additions & 0 deletions packages/edit-post/src/components/view-link/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { external } from '@wordpress/icons';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

export default function ViewLink() {
const { permalink, isPublished, label } = useSelect( ( select ) => {
// Grab post type to retrieve the view_item label.
const postTypeSlug = select( editorStore ).getCurrentPostType();
const postType = select( coreStore ).getPostType( postTypeSlug );

return {
permalink: select( editorStore ).getPermalink(),
isPublished: select( editorStore ).isCurrentPostPublished(),
label: postType?.labels.view_item,
};
}, [] );

// Only render the view button if the post is published and has a permalink.
if ( ! isPublished || ! permalink ) {
return null;
}

return (
<Button
icon={ external }
label={ label || __( 'View post' ) }
href={ permalink }
target="_blank"
/>
);
}

1 comment on commit 9046f21

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 9046f21.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4873681679
📝 Reported issues:

Please sign in to comment.