File size: 953 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import { useQuery } from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp';
import type { Mailbox } from './types';
import type { UseQueryOptions } from '@tanstack/react-query';
type UseGetMailboxesQueryData = Mailbox[];
export const getCacheKey = ( siteId: number | null ) => [ 'sites', siteId, 'emails', 'mailboxes' ];
/**
* Get the associated mailboxes for a given site
* @param siteId Site identifier
* @param queryOptions Query options
* @returns Returns the result of the `useQuery` call
*/
export const useGetMailboxes = (
siteId: number,
queryOptions?: Omit< UseQueryOptions< any, unknown, UseGetMailboxesQueryData >, 'queryKey' >
) => {
return useQuery< any, unknown, UseGetMailboxesQueryData >( {
queryKey: getCacheKey( siteId ),
queryFn: () =>
wpcom.req.get( {
path: `/sites/${ siteId }/emails/mailboxes`,
apiNamespace: 'wpcom/v2',
} ),
select: ( data ) => data.mailboxes,
...queryOptions,
} );
};
|