Skip to content

Commit

Permalink
improve code style
Browse files Browse the repository at this point in the history
  • Loading branch information
gigitux committed Dec 3, 2024
1 parent cbb8bda commit f891475
Showing 1 changed file with 11 additions and 39 deletions.
50 changes: 11 additions & 39 deletions packages/fields/src/fields/template/template-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,6 @@ import { getItemTitle } from '../../actions/utils';
import type { BasePost } from '../../types';
import { unlock } from '../../lock-unlock';

export function useAllowSwitchingTemplates( {
postType,
postId,
}: {
postType: string;
postId: number;
} ): boolean {
return useSelect(
( select ) => {
const { getHomePage, getPostsPageId } = unlock(
select( coreStore )
);

const isPostsPage = getPostsPageId() === +postId;
const isFrontPage =
postType === 'page' && getHomePage()?.postId === +postId;

return ! isPostsPage && ! isFrontPage;
},
[ postId, postType ]
);
}

export const TemplateEdit = ( {
data,
field,
Expand All @@ -62,21 +39,6 @@ export const TemplateEdit = ( {
typeof data.id === 'number' ? data.id : parseInt( data.id, 10 );
const slug = data.slug;

const allowSwitchingTemplate = useSelect(
( select ) => {
const { getHomePage, getPostsPageId } = unlock(
select( coreStore )
);

const isPostsPage = getPostsPageId() === +postId;
const isFrontPage =
postType === 'page' && getHomePage()?.postId === +postId;

return ! isPostsPage && ! isFrontPage;
},
[ postId, postType ]
);

const { availableTemplates, templates } = useSelect(
( select ) => {
const allTemplates =
Expand All @@ -89,6 +51,16 @@ export const TemplateEdit = ( {
}
) ?? [];

const { getHomePage, getPostsPageId } = unlock(
select( coreStore )
);

const isPostsPage = getPostsPageId() === +postId;
const isFrontPage =
postType === 'page' && getHomePage()?.postId === +postId;

const allowSwitchingTemplate = ! isPostsPage && ! isFrontPage;

return {
templates: allTemplates,
availableTemplates: allowSwitchingTemplate
Expand All @@ -101,7 +73,7 @@ export const TemplateEdit = ( {
: [],
};
},
[ allowSwitchingTemplate, data.template, postType ]
[ data.template, postId, postType ]
);

const templatesAsPatterns = useMemo(
Expand Down

0 comments on commit f891475

Please sign in to comment.