Skip to content

Commit

Permalink
return early instead if module is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
dhasilva committed Nov 27, 2024
1 parent de065f9 commit 9475352
Showing 1 changed file with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type CoreEditorSelect = { getCurrentPostId: () => number };

// HOC to populate the block's edit component with the AI Assistant control inpuit and toolbar button.
const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
return props => {
function ExtendedBlock( props ) {
// Block props. isSelectionEnabled is used to determine if the block is in the editor or in the preview.
const { clientId, isSelected, name: blockName, isSelectionEnabled } = props;
// Ref to the control wrapper, its height and its ResizeObserver, for positioning adjustments.
Expand All @@ -91,7 +91,6 @@ const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
// Ref to the requesting state to use it in the hideOnBlockFocus effect.
const requestingStateRef = useRef< RequestingStateProp | null >( null );

const isRequiredModulePresent = useBlockModuleStatus( blockName );
// Data and functions from the editor.
const { undo } = useDispatch( 'core/editor' ) as CoreEditorDispatch;
const { postId } = useSelect( select => {
Expand Down Expand Up @@ -501,7 +500,7 @@ const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
<>
<BlockEdit { ...props } />

{ showAiControl && isRequiredModulePresent && (
{ showAiControl && (
<AiAssistantInput
customPlaceholder={ customPlaceholder ? customPlaceholder : null }
className={ className }
Expand All @@ -521,14 +520,12 @@ const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
) }

<BlockControls { ...blockControlsProps }>
{ isRequiredModulePresent && (
<AiAssistantExtensionToolbarDropdown
blockType={ blockName }
onAskAiAssistant={ handleAskAiAssistant }
onRequestSuggestion={ handleRequestSuggestion }
behavior={ behavior }
/>
) }
<AiAssistantExtensionToolbarDropdown
blockType={ blockName }
onAskAiAssistant={ handleAskAiAssistant }
onRequestSuggestion={ handleRequestSuggestion }
behavior={ behavior }
/>
</BlockControls>
</>
);
Expand All @@ -546,6 +543,17 @@ const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
{ aiInlineExtensionContent }
</InlineExtensionsContext.Provider>
);
}

return props => {
const isRequiredModulePresent = useBlockModuleStatus( props.name );

// If the required module is not enabled, return the original block edit component early.
if ( ! isRequiredModulePresent ) {
return <BlockEdit { ...props } />;
}

return <ExtendedBlock { ...props } />;
};
}, 'blockEditWithAiComponents' );

Expand Down

0 comments on commit 9475352

Please sign in to comment.