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

Add the sticky notice when previewing a theme #82629

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions apps/wpcom-block-editor/src/wpcom/editor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { registerPlugin } from '@wordpress/plugins';
import './features/live-preview';
import './features/deprecate-coblocks-buttons';
import './features/fix-block-invalidation-errors';
import './features/fix-coblocks-fonts';
Expand Down
103 changes: 103 additions & 0 deletions apps/wpcom-block-editor/src/wpcom/features/live-preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { useDispatch, useSelect } from '@wordpress/data';
import domReady from '@wordpress/dom-ready';
import { __, sprintf } from '@wordpress/i18n';
import { registerPlugin } from '@wordpress/plugins';
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';
import { getQueryArg } from '@wordpress/url';
import { useEffect } from 'react';

/**
* Return true if the user is currently previewing a theme.
* FIXME: This is copied from Gutenberg; we should be creating a selector for the `core/edit-site` store.
* @see https://github.com/WordPress/gutenberg/blob/053c8f733c85d80c891fa308b071b9a18e5194e9/packages/edit-site/src/utils/is-previewing-theme.js#L6
* @returns {boolean} isPreviewingTheme
*/
function isPreviewingTheme() {
return getQueryArg( window.location.href, 'wp_theme_preview' ) !== undefined;
}

/**
* Return the theme slug if the user is currently previewing a theme.
* FIXME: This is copied from Gutenberg; we should be creating a selector for the `core/edit-site` store.
* @see https://github.com/WordPress/gutenberg/blob/053c8f733c85d80c891fa308b071b9a18e5194e9/packages/edit-site/src/utils/is-previewing-theme.js#L6
* @returns {string|null} currentlyPreviewingTheme
*/
function currentlyPreviewingTheme() {
if ( isPreviewingTheme() ) {
return getQueryArg( window.location.href, 'wp_theme_preview' );
}
return null;
}

/**
* Sometimes Gutenberg doesn't allow you to re-register the module and throws an error.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let unlock: ( object: any ) => any | undefined;
try {
unlock = __dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'@wordpress/edit-site'
).unlock;
} catch ( error ) {
// eslint-disable-next-line no-console
console.error( 'Error: Unable to get the unlock api. Reason: %s', error );
}

const NOTICE_ID = 'wpcom-live-preview/notice';

/**
* This is an interim solution to clarify to users that they are currently live previewing a theme.
* And this should be moved to jetpack-mu-wpcom.
* @see https://github.com/Automattic/wp-calypso/issues/82218
*/
const LivePreviewNotice = () => {
const { createWarningNotice } = useDispatch( 'core/notices' );
const { dashboardLink, previewingTheme } = useSelect( ( select ) => {
const { getSettings } = unlock( select( 'core/edit-site' ) );
const themeSlug = currentlyPreviewingTheme();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const theme = ( select( 'core' ) as any ).getTheme( themeSlug );
return {
previewingTheme: theme?.name?.rendered || themeSlug,
dashboardLink: getSettings().__experimentalDashboardLink,
};
}, [] );
useEffect( () => {
if ( ! isPreviewingTheme() ) {
return;
}
createWarningNotice(
sprintf(
// translators: %s: theme name
__(
'You are previewing the %s theme. You can try out your own style customizations, which will only be saved if you activate this theme.',
'wpcom-live-preview'
),
previewingTheme
),
{
id: NOTICE_ID,
isDismissible: false,
actions: [
{
label: __( 'Back to themes', 'wpcom-live-preview' ),
url: dashboardLink,
variant: 'secondary',
},
],
}
);
}, [ dashboardLink, createWarningNotice, previewingTheme ] );
return null;
};

const registerLivePreviewPlugin = () => {
registerPlugin( 'wpcom-live-preview', {
render: () => <LivePreviewNotice />,
} );
};

domReady( () => {
registerLivePreviewPlugin();
} );