Repository navigation
Try adding dynamic page templates to pages section #50630
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
Changes from all commits
bf2ec56
46e813e
02ebcbe
9c0099b
1add381
75e287f
e8c6695
1631b32
a107162
92ee8ab
db06915
bb94a22
2ba722b
a151d40
9c218f6
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 |
|---|---|---|
|
|
@@ -5,64 +5,157 @@ import { | |
| __experimentalItemGroup as ItemGroup, | ||
| __experimentalItem as Item, | ||
| __experimentalTruncate as Truncate, | ||
| __experimentalVStack as VStack, | ||
| } from '@wordpress/components'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useEntityRecords } from '@wordpress/core-data'; | ||
| import { useEntityRecords, store as coreStore } from '@wordpress/core-data'; | ||
| import { decodeEntities } from '@wordpress/html-entities'; | ||
| import { layout, page, home, loop } from '@wordpress/icons'; | ||
| import { useSelect } from '@wordpress/data'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import SidebarNavigationScreen from '../sidebar-navigation-screen'; | ||
| import { useLink } from '../routes/link'; | ||
| import SidebarNavigationItem from '../sidebar-navigation-item'; | ||
| import SidebarNavigationSubtitle from '../sidebar-navigation-subtitle'; | ||
|
|
||
| const PageItem = ( { postId, ...props } ) => { | ||
| const PageItem = ( { postType = 'page', postId, ...props } ) => { | ||
| const linkInfo = useLink( { | ||
| postType: 'page', | ||
| postType, | ||
| postId, | ||
| } ); | ||
| return <SidebarNavigationItem { ...linkInfo } { ...props } />; | ||
| }; | ||
|
|
||
| export default function SidebarNavigationScreenPages() { | ||
| const { records: pages, isResolving: isLoading } = useEntityRecords( | ||
| const { records: pages, isResolving: isLoadingPages } = useEntityRecords( | ||
| 'postType', | ||
| 'page', | ||
| { status: 'any' } | ||
| { | ||
| status: 'any', | ||
| per_page: -1, | ||
| } | ||
| ); | ||
| const { records: templates, isResolving: isLoadingTemplates } = | ||
| useEntityRecords( 'postType', 'wp_template', { | ||
| per_page: -1, | ||
| } ); | ||
|
|
||
| const dynamicPageTemplates = templates?.filter( ( { slug } ) => | ||
| [ '404', 'search' ].includes( slug ) | ||
| ); | ||
|
|
||
| const homeTemplate = | ||
| templates?.find( ( template ) => template.slug === 'front-page' ) || | ||
| templates?.find( ( template ) => template.slug === 'home' ) || | ||
| templates?.find( ( template ) => template.slug === 'index' ); | ||
|
|
||
| const pagesAndTemplates = pages?.concat( dynamicPageTemplates, [ | ||
| homeTemplate, | ||
| ] ); | ||
|
|
||
| const { frontPage, postsPage } = useSelect( ( select ) => { | ||
| const { getEntityRecord } = select( coreStore ); | ||
|
|
||
| const siteSettings = getEntityRecord( 'root', 'site' ); | ||
| return { | ||
| frontPage: siteSettings?.page_on_front, | ||
| postsPage: siteSettings?.page_for_posts, | ||
| }; | ||
| }, [] ); | ||
|
|
||
| const isHomePageBlog = frontPage === postsPage; | ||
|
|
||
| const reorderedPages = pages && [ ...pages ]; | ||
|
|
||
| if ( ! isHomePageBlog && reorderedPages?.length ) { | ||
| const homePageIndex = reorderedPages.findIndex( | ||
| ( item ) => item.id === frontPage | ||
| ); | ||
| const homePage = reorderedPages.splice( homePageIndex, 1 ); | ||
| reorderedPages?.splice( 0, 0, ...homePage ); | ||
|
|
||
| const postsPageIndex = reorderedPages.findIndex( | ||
| ( item ) => item.id === postsPage | ||
| ); | ||
|
|
||
| const blogPage = reorderedPages.splice( postsPageIndex, 1 ); | ||
|
|
||
| reorderedPages.splice( 1, 0, ...blogPage ); | ||
| } | ||
|
|
||
| return ( | ||
| <SidebarNavigationScreen | ||
| title={ __( 'Pages' ) } | ||
| description={ __( 'Browse and edit pages on your site.' ) } | ||
| content={ | ||
| <> | ||
| { isLoading && ( | ||
| { ( isLoadingPages || isLoadingTemplates ) && ( | ||
| <ItemGroup> | ||
| <Item>{ __( 'Loading pages' ) }</Item> | ||
| </ItemGroup> | ||
| ) } | ||
| { ! isLoading && ( | ||
| <> | ||
| <SidebarNavigationSubtitle> | ||
| { __( 'Recent' ) } | ||
| </SidebarNavigationSubtitle> | ||
| <ItemGroup> | ||
| { ! pages?.length && ( | ||
| <Item>{ __( 'No page found' ) }</Item> | ||
| ) } | ||
| { pages?.map( ( page ) => ( | ||
| { ! ( isLoadingPages || isLoadingTemplates ) && ( | ||
| <ItemGroup> | ||
| { ! pagesAndTemplates?.length && ( | ||
| <Item>{ __( 'No page found' ) }</Item> | ||
| ) } | ||
| { isHomePageBlog && homeTemplate && ( | ||
| <PageItem | ||
| postType="wp_template" | ||
| postId={ homeTemplate.id } | ||
| key={ homeTemplate.id } | ||
| icon={ home } | ||
| withChevron | ||
| > | ||
| <Truncate numberOfLines={ 1 }> | ||
| { decodeEntities( | ||
| homeTemplate.title?.rendered | ||
| ) ?? __( '(no title)' ) } | ||
| </Truncate> | ||
| </PageItem> | ||
| ) } | ||
| { reorderedPages?.map( ( item ) => { | ||
|
Member
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. Optional, but perhaps this could be simplified with destructuring, and a function to grab the icon. const getIcon = ( id ) => { // I don't think we need useCallback here since `frontPage` and `postsPage` won't change after defined.
if ( id === frontPage ) {
return home;
}
if ( id === postsPage ) {
return loop;
}
return page;
};
....
{ reorderedPages?.map( ( { id, title } ) => (
<PageItem
postId={ id }
key={ id }
icon={ getIcon( id ) }
withChevron
>
<Truncate numberOfLines={ 1 }>
{ decodeEntities( title?.rendered ) ??
__( '(no title)' ) }
</Truncate>
</PageItem>
) ) } |
||
| let itemIcon; | ||
| switch ( item.id ) { | ||
| case frontPage: | ||
| itemIcon = home; | ||
| break; | ||
| case postsPage: | ||
| itemIcon = loop; | ||
| break; | ||
| default: | ||
| itemIcon = page; | ||
| } | ||
| return ( | ||
| <PageItem | ||
| postId={ page.id } | ||
| key={ page.id } | ||
| postId={ item.id } | ||
| key={ item.id } | ||
| icon={ itemIcon } | ||
| withChevron | ||
| > | ||
| <Truncate numberOfLines={ 1 }> | ||
| { decodeEntities( | ||
| page.title?.rendered | ||
| ) ?? __( 'Untitled' ) } | ||
| item.title?.rendered | ||
| ) ?? __( '(no title)' ) } | ||
|
Member
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. #50767 uses I have no preference either way, but we should make them the same.
Contributor
Author
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. "no title" is what the pages were already using, this PR only adds a couple more instances. Was changing it to "Untitled" a design requirement? I'm happy with either, but would prefer to address that as a follow-up.
Member
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.
Not at all, just asking so that I can update the individual page view to match this copy 👍
Member
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. PR here: #51074 |
||
| </Truncate> | ||
| </PageItem> | ||
| ); | ||
| } ) } | ||
| <VStack className="edit-site-sidebar-navigation-screen__sticky-section"> | ||
| { dynamicPageTemplates?.map( ( item ) => ( | ||
| <PageItem | ||
| postType="wp_template" | ||
| postId={ item.id } | ||
| key={ item.id } | ||
| icon={ layout } | ||
| withChevron | ||
| > | ||
| <Truncate numberOfLines={ 1 }> | ||
| { decodeEntities( | ||
| item.title?.rendered | ||
| ) ?? __( '(no title)' ) } | ||
| </Truncate> | ||
| </PageItem> | ||
| ) ) } | ||
|
|
@@ -76,8 +169,8 @@ export default function SidebarNavigationScreenPages() { | |
| > | ||
| { __( 'Manage all pages' ) } | ||
| </SidebarNavigationItem> | ||
| </ItemGroup> | ||
| </> | ||
| </VStack> | ||
| </ItemGroup> | ||
| ) } | ||
| </> | ||
| } | ||
|
|
||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.