1 import { useEffect, useState } from 'react';
3 import { useUser } from '@proton/account/user/hooks';
4 import { useLoading } from '@proton/hooks';
5 import useIsMounted from '@proton/hooks/useIsMounted';
6 import type { LatestSubscription } from '@proton/payments';
7 import { getLastCancelledSubscription } from '@proton/shared/lib/api/payments';
9 import useApi from './useApi';
11 let promise: Promise<LatestSubscription> | undefined;
13 const useLastSubscriptionEnd = (): [latestSubscription: number, loading: boolean] => {
15 const [loading, withLoading] = useLoading();
16 const [{ isPaid }] = useUser();
17 const [latestSubscription, setLatestSubscription] = useState<number>(0);
18 const isMounted = useIsMounted();
22 setLatestSubscription(0);
25 const run = async () => {
28 promise = api<LatestSubscription>(getLastCancelledSubscription());
31 const { LastSubscriptionEnd = 0 } = await promise;
34 setLatestSubscription(LastSubscriptionEnd || 0);
39 // Reset promise to allow a new request
43 void withLoading(run());
46 return [latestSubscription, loading];
49 export default useLastSubscriptionEnd;