-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[Site Editor]: Re-architect templates addition #42457
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,13 +36,8 @@ import AddCustomTemplateModal from './add-custom-template-modal'; | |
import { | ||
useExistingTemplates, | ||
useDefaultTemplateTypes, | ||
entitiesConfig, | ||
usePostTypes, | ||
usePostTypePage, | ||
useTaxonomies, | ||
useTaxonomyCategory, | ||
useTaxonomyTag, | ||
useExtraTemplates, | ||
useTaxonomiesMenuItems, | ||
usePostTypeMenuItems, | ||
} from './utils'; | ||
import AddCustomGenericTemplateModal from './add-custom-generic-template-modal'; | ||
import { useHistory } from '../routes'; | ||
|
@@ -225,19 +220,11 @@ function useMissingTemplates( | |
setEntityForSuggestions, | ||
setShowCustomTemplateModal | ||
) { | ||
const postTypes = usePostTypes(); | ||
const pagePostType = usePostTypePage(); | ||
const taxonomies = useTaxonomies(); | ||
const categoryTaxonomy = useTaxonomyCategory(); | ||
const tagTaxonomy = useTaxonomyTag(); | ||
|
||
const existingTemplates = useExistingTemplates(); | ||
const defaultTemplateTypes = useDefaultTemplateTypes(); | ||
|
||
const existingTemplateSlugs = ( existingTemplates || [] ).map( | ||
( { slug } ) => slug | ||
); | ||
|
||
const missingDefaultTemplates = ( defaultTemplateTypes || [] ).filter( | ||
( template ) => | ||
DEFAULT_TEMPLATE_SLUGS.includes( template.slug ) && | ||
|
@@ -247,70 +234,46 @@ function useMissingTemplates( | |
setShowCustomTemplateModal( true ); | ||
setEntityForSuggestions( _entityForSuggestions ); | ||
}; | ||
// TODO: find better names for these variables. `useExtraTemplates` returns an array of items. | ||
const categoryMenuItem = useExtraTemplates( | ||
categoryTaxonomy, | ||
entitiesConfig.category, | ||
onClickMenuItem | ||
); | ||
const tagMenuItem = useExtraTemplates( | ||
tagTaxonomy, | ||
entitiesConfig.tag, | ||
onClickMenuItem | ||
); | ||
const pageMenuItem = useExtraTemplates( | ||
pagePostType, | ||
entitiesConfig.page, | ||
onClickMenuItem | ||
); | ||
// We need to replace existing default template types with | ||
// the create specific template functionality. The original | ||
// info (title, description, etc.) is preserved in the | ||
// `useExtraTemplates` hook. | ||
// used hooks. | ||
const enhancedMissingDefaultTemplateTypes = [ ...missingDefaultTemplates ]; | ||
[ categoryMenuItem, tagMenuItem, pageMenuItem ].forEach( ( menuItem ) => { | ||
if ( ! menuItem?.length ) { | ||
return; | ||
} | ||
const matchIndex = enhancedMissingDefaultTemplateTypes.findIndex( | ||
( template ) => template.slug === menuItem[ 0 ].slug | ||
); | ||
// Some default template types might have been filtered above from | ||
// `missingDefaultTemplates` because they only check for the general | ||
// template. So here we either replace or append the item, augmented | ||
// with the check if it has available specific item to create a | ||
// template for. | ||
if ( matchIndex > -1 ) { | ||
enhancedMissingDefaultTemplateTypes.splice( | ||
matchIndex, | ||
1, | ||
menuItem[ 0 ] | ||
const { defaultTaxonomiesMenuItems, taxonomiesMenuItems } = | ||
useTaxonomiesMenuItems( onClickMenuItem ); | ||
const { defaultPostTypesMenuItems, postTypesMenuItems } = | ||
usePostTypeMenuItems( onClickMenuItem ); | ||
[ ...defaultTaxonomiesMenuItems, ...defaultPostTypesMenuItems ].forEach( | ||
( menuItem ) => { | ||
if ( ! menuItem ) { | ||
return; | ||
} | ||
const matchIndex = enhancedMissingDefaultTemplateTypes.findIndex( | ||
( template ) => template.slug === menuItem.slug | ||
); | ||
} else { | ||
enhancedMissingDefaultTemplateTypes.push( menuItem[ 0 ] ); | ||
// Some default template types might have been filtered above from | ||
// `missingDefaultTemplates` because they only check for the general | ||
// template. So here we either replace or append the item, augmented | ||
// with the check if it has available specific item to create a | ||
// template for. | ||
if ( matchIndex > -1 ) { | ||
enhancedMissingDefaultTemplateTypes[ matchIndex ] = menuItem; | ||
} else { | ||
enhancedMissingDefaultTemplateTypes.push( menuItem ); | ||
} | ||
} | ||
} ); | ||
); | ||
// Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order. | ||
enhancedMissingDefaultTemplateTypes?.sort( ( template1, template2 ) => { | ||
return ( | ||
DEFAULT_TEMPLATE_SLUGS.indexOf( template1.slug ) - | ||
DEFAULT_TEMPLATE_SLUGS.indexOf( template2.slug ) | ||
); | ||
} ); | ||
const extraPostTypeTemplates = useExtraTemplates( | ||
postTypes, | ||
entitiesConfig.postType, | ||
onClickMenuItem | ||
); | ||
const extraTaxonomyTemplates = useExtraTemplates( | ||
taxonomies, | ||
entitiesConfig.taxonomy, | ||
onClickMenuItem | ||
); | ||
const missingTemplates = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are returning a new array reference on every rerender. The array computation is also not trivial, e.g.: it has sorting operations. I guess we compute missingTemplates inside a useMemo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you referring to |
||
...enhancedMissingDefaultTemplateTypes, | ||
...extraPostTypeTemplates, | ||
...extraTaxonomyTemplates, | ||
...postTypesMenuItems, | ||
...taxonomiesMenuItems, | ||
]; | ||
return missingTemplates; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we separating between defaults ( defaultPostTypesMenuItems) and non-defaults (postTypesMenuItems) it seems we can just return a single array and remove the separation code. Then all of the items would pass by the next forEach loop, but just the defaults would be found on enhancedMissingDefaultTemplateTypes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way we can control where we want to append the extra menu items besides the default ones. It's also related to the current sorting which might be subject to change(
DEFAULT_TEMPLATE_SLUGS
and the such) - not in this PR 😄