Skip to content

Commit

Permalink
Use a private selector
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jul 23, 2024
1 parent 6629299 commit 0b55895
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
19 changes: 5 additions & 14 deletions packages/core-data/src/hooks/use-entity-records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useQuerySelect from './use-query-select';
import { store as coreStore } from '../';
import type { Options } from './use-entity-record';
import type { Status } from './constants';
import { unlock } from '../lock-unlock';

interface EntityRecordsResolution< RecordType > {
/** The requested entity record */
Expand Down Expand Up @@ -179,22 +180,12 @@ export function useEntityRecordsWithPermissions< RecordType >(
[ data, entityConfig?.key ]
);

// Todo: this selector is not memoized properly.
const permissions = useSelect(
( select ) => {
const { canUser } = select( coreStore );
return ids.map( ( id ) => ( {
delete: canUser( 'delete', {
kind,
name,
id,
} ),
update: canUser( 'update', {
kind,
name,
id,
} ),
} ) );
const { getEntityRecordsPermissions } = unlock(
select( coreStore )
);
return getEntityRecordsPermissions( kind, name, ids );
},
[ ids, kind, name ]
);
Expand Down
1 change: 1 addition & 0 deletions packages/core-data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ export * from './entity-provider';
export * from './entity-types';
export * from './fetch';
export * from './hooks';
export * from './private-apis';
22 changes: 22 additions & 0 deletions packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createSelector, createRegistrySelector } from '@wordpress/data';
/**
* Internal dependencies
*/
import { canUser } from './selectors';
import type { State } from './selectors';
import { STORE_NAME } from './name';

Expand Down Expand Up @@ -50,3 +51,24 @@ export const getBlockPatternsForPostType = createRegistrySelector(
() => [ select( STORE_NAME ).getBlockPatterns() ]
)
);

/**
* Returns the entity records permissions for the given entity record ids.
*/
export const getEntityRecordsPermissions = createSelector(
( state: State, kind: string, name: string, ids: string[] ) => {
return ids.map( ( id ) => ( {
delete: canUser( state, 'delete', {
kind,
name,
id,
} ),
update: canUser( state, 'update', {
kind,
name,
id,
} ),
} ) );
},
( state ) => [ state.userPermissions ]
);

0 comments on commit 0b55895

Please sign in to comment.