Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix visibility of the template Welcome Guide in the Site Editor #64789

Merged
merged 6 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions packages/edit-site/src/components/welcome-guide/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import { Guide } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as editorStore } from '@wordpress/editor';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { useState } from '@wordpress/element';
import { useDebounce } from '@wordpress/compose';

export default function WelcomeGuideTemplate() {
const { toggle } = useDispatch( preferencesStore );

const isVisible = useSelect( ( select ) => {
const visibility = useSelect( ( select ) => {
const isTemplateActive = !! select( preferencesStore ).get(
'core/edit-site',
'welcomeGuideTemplate'
Expand All @@ -24,16 +21,24 @@ export default function WelcomeGuideTemplate() {
'core/edit-site',
'welcomeGuide'
);
const { isPage } = select( editSiteStore );
const { getCurrentPostType } = select( editorStore );
const { getCurrentPostType, getEditorSettings } = select( editorStore );
const hasBackNavigation =
!! getEditorSettings().onNavigateToPreviousEntityRecord;
return (
isTemplateActive &&
! isEditorActive &&
isPage() &&
getCurrentPostType() === 'wp_template'
getCurrentPostType() === 'wp_template' &&
hasBackNavigation
);
}, [] );

// The visibility conditions change in such a way that it’s tricky to avoid
// an unexpected flash of the component. Using state and debouncing writes
// to it works around the issue.
const [ isVisible, setIsVisible ] = useState();
const debouncedSetIsVisible = useDebounce( setIsVisible, 32 );
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
debouncedSetIsVisible( visibility );

if ( ! isVisible ) {
return null;
}
Expand Down
24 changes: 15 additions & 9 deletions packages/editor/src/components/post-template/block-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { __ } from '@wordpress/i18n';
import { useEntityRecord, store as coreStore } from '@wordpress/core-data';
import { check } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';
import { store as preferencesStore } from '@wordpress/preferences';

/**
* Internal dependencies
Expand Down Expand Up @@ -43,6 +44,8 @@ export default function BlockThemeControl( { id } ) {
};
}, [] );

const { get: getPreference } = useSelect( preferencesStore );

const { editedRecord: template, hasResolved } = useEntityRecord(
'postType',
'wp_template',
Expand Down Expand Up @@ -75,6 +78,17 @@ export default function BlockThemeControl( { id } ) {
},
]
: undefined;

const mayShowTemplateEditNotice = () => {
if ( ! getPreference( 'core/edit-site', 'welcomeGuideTemplate' ) ) {
createSuccessNotice(
__(
'Editing template. Changes made here affect all posts and pages that use the template.'
),
{ type: 'snackbar', actions: notificationAction }
);
}
};
return (
<DropdownMenu
popoverProps={ POPOVER_PROPS }
Expand All @@ -99,15 +113,7 @@ export default function BlockThemeControl( { id } ) {
postType: 'wp_template',
} );
onClose();
createSuccessNotice(
__(
'Editing template. Changes made here affect all posts and pages that use the template.'
),
{
type: 'snackbar',
actions: notificationAction,
}
);
mayShowTemplateEditNotice();
} }
>
{ __( 'Edit template' ) }
Expand Down
Loading