Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / hooks / useShareAccessOptionsPolling.ts
blobb8b3617c3a69a751afb9aa6a2ee1d1a19ee7b4ea
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),
15     });
17     const wasLoading = usePrevious(loading);
18     const [didLoad, setDidLoad] = useState(false);
20     useEffect(() => {
21         timer.current = setInterval(() => dispatch({ shareId }), ACTIVE_POLLING_TIMEOUT);
22         revalidate({ shareId });
24         return () => clearInterval(timer.current);
25     }, [shareId]);
27     useEffect(() => {
28         if (wasLoading === true && !loading) setDidLoad(true);
29     }, [loading]);
31     return didLoad ? false : loading;