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);
18 timer.current = setInterval(() => dispatch({ shareId }), ACTIVE_POLLING_TIMEOUT);
19 revalidate({ shareId });
21 return () => clearInterval(timer.current);
25 if (wasLoading === true && !loading) setDidLoad(true);
28 return didLoad ? false : loading;