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 { shareAccessOptionsRequest } from '@proton/pass/store/actions/requests';
7 import { type Maybe } from '@proton/pass/types';
9 import { useRequest } from './useActionRequest';
11 export const useShareAccessOptionsPolling = (shareId: string) => {
12 const timer = useRef<Maybe<ReturnType<typeof setTimeout>>>();
13 const { loading, dispatch, revalidate } = useRequest(getShareAccessOptions, {
14 initialRequestId: shareAccessOptionsRequest(shareId),
17 const wasLoading = usePrevious(loading);
18 const [didLoad, setDidLoad] = useState(false);
21 timer.current = setInterval(() => dispatch({ shareId }), ACTIVE_POLLING_TIMEOUT);
22 revalidate({ shareId });
24 return () => clearInterval(timer.current);
28 if (wasLoading === true && !loading) setDidLoad(true);
31 return didLoad ? false : loading;