diff --git a/packages/ra-core/src/auth/useCanAccessResources.ts b/packages/ra-core/src/auth/useCanAccessResources.ts index 94e57f6fff..30d394d854 100644 --- a/packages/ra-core/src/auth/useCanAccessResources.ts +++ b/packages/ra-core/src/auth/useCanAccessResources.ts @@ -1,9 +1,5 @@ import { useMemo } from 'react'; -import { - useQueries, - UseQueryOptions, - UseQueryResult, -} from '@tanstack/react-query'; +import { useQuery, UseQueryOptions } from '@tanstack/react-query'; import useAuthProvider from './useAuthProvider'; import { HintedString } from '../types'; import { useRecordContext } from '../controller'; @@ -50,18 +46,18 @@ export const useCanAccessResources = < RecordType extends Record = Record, ErrorType extends Error = Error, >( - params: UseCanAccessResourcesOptions + params: UseCanAccessResourcesOptions ): UseCanAccessResourcesResult => { const authProvider = useAuthProvider(); const record = useRecordContext(params); - const { action, resources } = params; + const { action, resources, ...options } = params; - const queryResult = useQueries({ - queries: resources.map(resource => { - return { - queryKey: ['auth', 'canAccess', resource, action, record], - queryFn: async ({ signal }) => { + const queryResult = useQuery({ + queryKey: ['auth', 'canAccess', resources, action, record], + queryFn: async ({ signal }) => { + const queries = await Promise.all( + resources.map(async resource => { if (!authProvider || !authProvider.canAccess) { return { canAccess: true, resource }; } @@ -75,10 +71,20 @@ export const useCanAccessResources = < }); return { canAccess, resource }; + }) + ); + + const result = queries.reduce( + (acc, { resource, canAccess }) => { + acc[resource] = canAccess; + return acc; }, - }; - }), - combine: combineSourceAccessResults, + {} as Record + ); + + return result; + }, + ...options, }); const result = useMemo(() => { @@ -111,7 +117,10 @@ export const useCanAccessResources = < export interface UseCanAccessResourcesOptions< RecordType extends Record = Record, ErrorType extends Error = Error, -> extends Omit, 'queryKey' | 'queryFn'> { +> extends Omit< + UseQueryOptions, ErrorType>, + 'queryKey' | 'queryFn' + > { resources: string[]; action: HintedString<'list' | 'create' | 'edit' | 'show' | 'delete'>; record?: RecordType; @@ -143,39 +152,3 @@ export interface UseCanAccessResourcesSuccessResult { error: null; isPending: false; } - -const combineSourceAccessResults = ( - results: UseQueryResult< - { - canAccess: boolean; - resource: string; - }, - ErrorType - >[] -): { - data?: Record; - isPending: boolean; - isError: boolean; - error?: ErrorType; -} => { - return { - data: results - ? results.reduce( - (acc, { data }) => { - if (!data) { - return acc; - } - const { resource, canAccess } = data; - return { - ...acc, - [resource]: canAccess, - }; - }, - {} as Record - ) - : undefined, - isPending: results.some(result => result.isPending), - isError: results.some(result => result.isError), - error: results.find(result => result.error)?.error || undefined, - }; -};