1 import { useEffect, useMemo } from 'react';
2 import { useSelector } from 'react-redux';
4 import { getAliasOptionsIntent } from '@proton/pass/store/actions';
5 import { aliasOptionsRequest } from '@proton/pass/store/actions/requests';
6 import { selectAliasOptions } from '@proton/pass/store/selectors';
7 import type { MaybeNull, MaybePromise } from '@proton/pass/types';
8 import type { AliasMailbox } from '@proton/pass/types/data/alias';
9 import noop from '@proton/utils/noop';
11 import { useActionRequest } from './useActionRequest';
13 export type SanitizedAliasOptions = {
14 suffixes: { value: string; signature: string }[];
15 mailboxes: AliasMailbox[];
18 export type UseAliasOptionsParams = {
19 lazy?: boolean /* defer the alias options dispatch */;
21 onAliasOptionsLoaded?: (aliasOptions: SanitizedAliasOptions) => MaybePromise<void>;
24 export type UseAliasOptionsResult = {
27 value: MaybeNull<SanitizedAliasOptions>;
30 export const useAliasOptions = ({
34 }: UseAliasOptionsParams): UseAliasOptionsResult => {
35 const aliasOptions = useSelector(selectAliasOptions);
37 const sanitizedAliasOptions = useMemo(
41 suffixes: aliasOptions.suffixes.map(({ suffix, signedSuffix }) => ({
43 signature: signedSuffix,
45 mailboxes: aliasOptions.mailboxes,
51 const getAliasOptions = useActionRequest(getAliasOptionsIntent, {
52 initialRequestId: aliasOptionsRequest(shareId),
53 onSuccess: sanitizedAliasOptions ? () => onAliasOptionsLoaded?.(sanitizedAliasOptions) : noop,
57 if (!lazy) getAliasOptions.dispatch({ shareId });
62 loading: getAliasOptions.loading,
63 request: () => getAliasOptions.dispatch({ shareId }),
64 value: sanitizedAliasOptions,
66 [sanitizedAliasOptions, getAliasOptions.loading, shareId]