From 02dabf9c01e11d39ff9cc07e86c60c2fac303091 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Mon, 2 Dec 2024 15:56:02 +0100 Subject: [PATCH] Add a custom hook to determine whether we are in email or template mode There are a couple of places where we determine the mode, but we use different approaches. This commit adds a mechanism for determining the editor mode which can be easily reusable in the whole app. [MAILPOET-6336] --- packages/js/email-editor/src/hooks/index.ts | 1 + .../js/email-editor/src/hooks/use-editor-mode.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 packages/js/email-editor/src/hooks/use-editor-mode.ts diff --git a/packages/js/email-editor/src/hooks/index.ts b/packages/js/email-editor/src/hooks/index.ts index bae76b3559..4621268f06 100644 --- a/packages/js/email-editor/src/hooks/index.ts +++ b/packages/js/email-editor/src/hooks/index.ts @@ -4,3 +4,4 @@ export * from './use-validation-notices'; export * from './use-email-styles'; export * from './use-email-css'; export * from './use-preview-templates'; +export * from './use-editor-mode'; diff --git a/packages/js/email-editor/src/hooks/use-editor-mode.ts b/packages/js/email-editor/src/hooks/use-editor-mode.ts new file mode 100644 index 0000000000..c1bc7724ff --- /dev/null +++ b/packages/js/email-editor/src/hooks/use-editor-mode.ts @@ -0,0 +1,16 @@ +/** + * WordPress dependencies + */ +import { useSelect } from '@wordpress/data'; +import { store as editorStore } from '@wordpress/editor'; + +export function useEditorMode() { + const { isEditingTemplate } = useSelect( + ( select ) => ( { + isEditingTemplate: + select( editorStore ).getCurrentPostType() === 'wp_template', + } ), + [] + ); + return [ isEditingTemplate ? 'template' : 'email' ]; +}