|
|
import { useQuery } from '@tanstack/react-query'; |
|
|
import wpcom from 'calypso/lib/wp'; |
|
|
import type { UseQueryOptions } from '@tanstack/react-query'; |
|
|
|
|
|
export const getCacheKey = ( domain: string, mailboxName: string ) => [ |
|
|
'emails', |
|
|
'titan', |
|
|
domain, |
|
|
'check-mailbox-availability', |
|
|
mailboxName, |
|
|
]; |
|
|
|
|
|
|
|
|
const finalErrorStatuses = [ 400, 401, 403, 409 ]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useGetTitanMailboxAvailability = ( |
|
|
domainName: string, |
|
|
mailboxName: string, |
|
|
queryOptions: Omit< UseQueryOptions< any, any >, 'queryKey' > = {} |
|
|
) => { |
|
|
return useQuery< any, any >( { |
|
|
queryKey: getCacheKey( domainName, mailboxName ), |
|
|
queryFn: () => |
|
|
wpcom.req.get( { |
|
|
path: `/emails/titan/${ encodeURIComponent( |
|
|
domainName |
|
|
) }/check-mailbox-availability/${ encodeURIComponent( mailboxName ) }`, |
|
|
apiNamespace: 'wpcom/v2', |
|
|
} ), |
|
|
...queryOptions, |
|
|
retry: ( count, { message: status } ) => finalErrorStatuses.includes( status ), |
|
|
} ); |
|
|
}; |
|
|
|