1 import { useEffect, useState } from 'react';
3 import useModalState from '@proton/components/components/modalTwo/useModalState';
4 import { useAutomaticCurrency } from '@proton/components/payments/client-extensions';
5 import { type Currency } from '@proton/payments';
7 import type { OfferConfig } from '../interface';
8 import useFetchOffer from './useFetchOffer';
10 const useOfferModal = (offerConfig: OfferConfig) => {
11 const [offerModalProps, setOfferModalOpen, renderOfferModal] = useModalState();
12 const [fetchOffer, setFetchOffer] = useState(false);
13 const [automaticCurrency, loadingCurrency] = useAutomaticCurrency();
14 const [controlledCurrency, setCurrency] = useState<Currency | undefined>();
15 const currency = controlledCurrency ?? automaticCurrency;
18 if (!loadingCurrency) {
19 setCurrency(automaticCurrency);
21 }, [automaticCurrency, loadingCurrency]);
23 const [offer, loadingOffer] = useFetchOffer({
24 offerConfig: fetchOffer ? offerConfig : undefined,
27 // This is like a retry. Resetting the offer config so that the calls get retried if the user clicks the button again.
38 onChangeCurrency: setCurrency,
44 export default useOfferModal;