Skip to content

Commit

Permalink
Performance: Fetch permissions for visible patterns only (#64606)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
  • Loading branch information
4 people authored Aug 20, 2024
1 parent 3ed1812 commit 07c2a8e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
6 changes: 4 additions & 2 deletions packages/edit-site/src/components/page-patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '../../utils/constants';
import usePatternSettings from './use-pattern-settings';
import { unlock } from '../../lock-unlock';
import usePatterns from './use-patterns';
import usePatterns, { useAugmentPatternsWithPermissions } from './use-patterns';
import PatternsHeader from './header';
import { useEditPostAction } from '../dataviews-actions';
import {
Expand Down Expand Up @@ -136,6 +136,8 @@ export default function DataviewsPatterns() {
return filterSortAndPaginate( patterns, viewWithoutFilters, fields );
}, [ patterns, view, fields, type ] );

const dataWithPermissions = useAugmentPatternsWithPermissions( data );

const templatePartActions = usePostActions( {
postType: TEMPLATE_PART_POST_TYPE,
context: 'list',
Expand Down Expand Up @@ -175,7 +177,7 @@ export default function DataviewsPatterns() {
paginationInfo={ paginationInfo }
fields={ fields }
actions={ actions }
data={ data || EMPTY_ARRAY }
data={ dataWithPermissions || EMPTY_ARRAY }
getItemId={ ( item ) => item.name ?? item.id }
isLoading={ isResolving }
view={ view }
Expand Down
63 changes: 33 additions & 30 deletions packages/edit-site/src/components/page-patterns/use-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,44 @@ const selectUserPatterns = createSelector(
]
);

export function useAugmentPatternsWithPermissions( patterns ) {
const idsAndTypes = useMemo(
() =>
patterns
?.filter( ( record ) => record.type !== PATTERN_TYPES.theme )
.map( ( record ) => [ record.type, record.id ] ) ?? [],
[ patterns ]
);

const permissions = useSelect(
( select ) => {
const { getEntityRecordPermissions } = unlock(
select( coreStore )
);
return idsAndTypes.reduce( ( acc, [ type, id ] ) => {
acc[ id ] = getEntityRecordPermissions( 'postType', type, id );
return acc;
}, {} );
},
[ idsAndTypes ]
);

return useMemo(
() =>
patterns?.map( ( record ) => ( {
...record,
permissions: permissions?.[ record.id ] ?? {},
} ) ) ?? [],
[ patterns, permissions ]
);
}

export const usePatterns = (
postType,
categoryId,
{ search = '', syncStatus } = {}
) => {
const { patterns, ...rest } = useSelect(
return useSelect(
( select ) => {
if ( postType === TEMPLATE_PART_POST_TYPE ) {
return selectTemplateParts( select, categoryId, search );
Expand All @@ -284,35 +316,6 @@ export const usePatterns = (
},
[ categoryId, postType, search, syncStatus ]
);

const ids = useMemo(
() => patterns?.map( ( record ) => record.id ) ?? [],
[ patterns ]
);

const permissions = useSelect(
( select ) => {
const { getEntityRecordsPermissions } = unlock(
select( coreStore )
);
return getEntityRecordsPermissions( 'postType', postType, ids );
},
[ ids, postType ]
);

const patternsWithPermissions = useMemo(
() =>
patterns?.map( ( record, index ) => ( {
...record,
permissions: permissions[ index ],
} ) ) ?? [],
[ patterns, permissions ]
);

return {
...rest,
patterns: patternsWithPermissions,
};
};

export default usePatterns;

0 comments on commit 07c2a8e

Please sign in to comment.