Merge branch 'pass-lifetime-fixes' into 'main'
[ProtonMail-WebClient.git] / packages / components / containers / offers / hooks / useOnSelectDeal.ts
blob5a297b83e575a80c18dad011a353afc5ac289640
1 import { useCallback } from 'react';
3 import useSettingsLink from '@proton/components/components/link/useSettingsLink';
4 import { type Currency } from '@proton/payments';
5 import { getAppHref } from '@proton/shared/lib/apps/helper';
6 import { APPS } from '@proton/shared/lib/constants';
7 import { isElectronApp } from '@proton/shared/lib/helpers/desktop';
9 import { openLinkInBrowser } from '../../desktop/openExternalLink';
10 import { useHasInboxDesktopInAppPayments } from '../../desktop/useHasInboxDesktopInAppPayments';
11 import getOfferRedirectionParams from '../helpers/getOfferRedirectionParams';
12 import type { Deal, Offer } from '../interface';
14 const useSelectDeal = (callback?: () => void) => {
15     const goToSettingsLink = useSettingsLink();
16     const hasInboxDesktopInAppPayments = useHasInboxDesktopInAppPayments();
18     const handleOnSelectDeal = useCallback(
19         (offer: Offer, deal: Deal, currency: Currency) => {
20             const urlSearchParams = getOfferRedirectionParams({ offer, deal, currency });
21             callback?.();
23             const url = `/dashboard?${urlSearchParams.toString()}`;
24             if (isElectronApp && !hasInboxDesktopInAppPayments) {
25                 openLinkInBrowser(getAppHref(url, APPS.PROTONACCOUNT));
26                 return;
27             } else {
28                 goToSettingsLink(url);
29             }
30         },
31         [callback]
32     );
34     return handleOnSelectDeal;
37 export default useSelectDeal;