Merge branch 'fix-typo-drive' into 'main'
[ProtonMail-WebClient.git] / packages / pass / hooks / useShareAccessOptionsPolling.ts
blobf6a9b1f88eee4999ed1eec52b7136aea721bd5b3
1 import { useEffect, useRef, useState } from 'react';
3 import usePrevious from '@proton/hooks/usePrevious';
4 import { ACTIVE_POLLING_TIMEOUT } from '@proton/pass/lib/events/constants';
5 import { getShareAccessOptions } from '@proton/pass/store/actions';
6 import { type Maybe } from '@proton/pass/types';
8 import { useRequest } from './useActionRequest';
10 export const useShareAccessOptionsPolling = (shareId: string) => {
11     const timer = useRef<Maybe<ReturnType<typeof setTimeout>>>();
12     const { loading, dispatch, revalidate } = useRequest(getShareAccessOptions, { initial: { shareId } });
14     const wasLoading = usePrevious(loading);
15     const [didLoad, setDidLoad] = useState(false);
17     useEffect(() => {
18         timer.current = setInterval(() => dispatch({ shareId }), ACTIVE_POLLING_TIMEOUT);
19         revalidate({ shareId });
21         return () => clearInterval(timer.current);
22     }, [shareId]);
24     useEffect(() => {
25         if (wasLoading === true && !loading) setDidLoad(true);
26     }, [loading]);
28     return didLoad ? false : loading;