Skip to content
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

Update: Show template sources on templates Dataviews sidebar. #58124

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
VisuallyHidden,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useState, useMemo, useCallback } from '@wordpress/element';
import { useState, useMemo, useCallback, useEffect } from '@wordpress/element';
import { useEntityRecords } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
import { parse } from '@wordpress/blocks';
Expand Down Expand Up @@ -162,16 +162,42 @@ function Preview( { content, viewType } ) {

export default function PageTemplatesTemplateParts( { postType } ) {
const { params } = useLocation();
const { layout } = params;
const { activeView = 'all', layout } = params;
const defaultView = useMemo( () => {
return {
...DEFAULT_VIEW,
type: window?.__experimentalAdminViews
? layout ?? DEFAULT_VIEW.type
: DEFAULT_VIEW.type,
filters:
activeView !== 'all'
? [
{
field: 'author',
operator: 'in',
value: activeView,
},
]
: [],
};
}, [ layout ] );
}, [ layout, activeView ] );
const [ view, setView ] = useState( defaultView );
useEffect( () => {
setView( ( currentView ) => ( {
...currentView,
filters:
activeView !== 'all'
? [
{
field: 'author',
operator: 'in',
value: activeView,
},
]
: [],
} ) );
}, [ activeView ] );

const { records, isResolving: isLoadingData } = useEntityRecords(
'postType',
postType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ export default function DataViewItem( {
suffix,
} ) {
const {
params: { path },
params: { path, layout },
} = useLocation();

const iconToUse =
icon || VIEW_LAYOUTS.find( ( v ) => v.type === type ).icon;

const linkInfo = useLink( {
path,
layout,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this change? In pages list it certainly creates problems(at least in pages list) when changing views. For example if we change a view from the sidebar which has a grid view set, I'd expect this to be respected without taking into account the current layout param set.

In general the logic is hard to follow with side effects, when handling the views in sidebar and it would be great if we can simplify and also have more consistency between the different lists(pages, templates).

activeView: isCustom === 'true' ? customViewId : slug,
isCustom,
} );
Expand Down
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>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';

import { __experimentalUseNavigator as useNavigator } from '@wordpress/components';
import { privateApis as routerPrivateApis } from '@wordpress/router';

Expand All @@ -16,6 +17,7 @@ import {
TEMPLATE_PART_POST_TYPE,
} from '../../utils/constants';
import { unlock } from '../../lock-unlock';
import DataviewsTemplatesSidebarContent from './content';

const config = {
[ TEMPLATE_POST_TYPE ]: {
Expand All @@ -40,7 +42,7 @@ export default function SidebarNavigationScreenTemplatesBrowse() {
params: { postType },
} = useNavigator();
const {
params: { didAccessPatternsPage },
params: { didAccessPatternsPage, activeView = 'all' },
} = useLocation();

const isTemplatePartsMode = useSelect( ( select ) => {
Expand All @@ -56,6 +58,13 @@ export default function SidebarNavigationScreenTemplatesBrowse() {
title={ config[ postType ].title }
description={ config[ postType ].description }
backPath={ config[ postType ].backPath }
content={
<DataviewsTemplatesSidebarContent
activeView={ activeView }
postType={ postType }
config={ config }
/>
}
/>
);
}