-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update: Show template sources on templates Dataviews sidebar. (#58124)
* Update: Show template sources on templates Dataviews sidebar. * Fix rebase * Remove leftover suffix * Make sidebar work for templates & parts * Fix view name * Update component * Do not reset layout --------- Co-authored-by: André Maneiro <[email protected]>
- Loading branch information
1 parent
e8dbce0
commit 46446b8
Showing
4 changed files
with
114 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/edit-site/src/components/sidebar-navigation-screen-templates-browse/content.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEntityRecords } from '@wordpress/core-data'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { __experimentalItemGroup as ItemGroup } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import DataViewItem from '../sidebar-dataviews/dataview-item'; | ||
import { useAddedBy } from '../list/added-by'; | ||
import { layout } from '@wordpress/icons'; | ||
|
||
const EMPTY_ARRAY = []; | ||
|
||
function TemplateDataviewItem( { template, isActive } ) { | ||
const { text, icon } = useAddedBy( template.type, template.id ); | ||
return ( | ||
<DataViewItem | ||
key={ text } | ||
slug={ text } | ||
title={ text } | ||
icon={ icon } | ||
isActive={ isActive } | ||
isCustom="false" | ||
/> | ||
); | ||
} | ||
|
||
export default function DataviewsTemplatesSidebarContent( { | ||
activeView, | ||
postType, | ||
config, | ||
} ) { | ||
const { records } = useEntityRecords( 'postType', postType, { | ||
per_page: -1, | ||
} ); | ||
const firstItemPerAuthorText = useMemo( () => { | ||
const firstItemPerAuthor = records?.reduce( ( acc, template ) => { | ||
const author = template.author_text; | ||
if ( author && ! acc[ author ] ) { | ||
acc[ author ] = template; | ||
} | ||
return acc; | ||
}, {} ); | ||
return ( | ||
( firstItemPerAuthor && Object.values( firstItemPerAuthor ) ) ?? | ||
EMPTY_ARRAY | ||
); | ||
}, [ records ] ); | ||
|
||
return ( | ||
<ItemGroup> | ||
<DataViewItem | ||
slug={ 'all' } | ||
title={ config[ postType ].title } | ||
icon={ layout } | ||
isActive={ activeView === 'all' } | ||
isCustom="false" | ||
/> | ||
{ firstItemPerAuthorText.map( ( template ) => { | ||
return ( | ||
<TemplateDataviewItem | ||
key={ template.author_text } | ||
template={ template } | ||
isActive={ activeView === template.author_text } | ||
/> | ||
); | ||
} ) } | ||
</ItemGroup> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters