session-desktop/ts/hooks/useEncryptedFileFetch.ts

37 lines
982 B
TypeScript
Raw Normal View History

import { useEffect, useRef, useState } from 'react';
2021-04-13 06:10:31 +02:00
import { getDecryptedMediaUrl } from '../session/crypto/DecryptedAttachmentsManager';
import { perfEnd, perfStart } from '../session/utils/Performance';
2021-03-26 03:07:42 +01:00
export const useEncryptedFileFetch = (url: string, contentType: string) => {
// tslint:disable-next-line: no-bitwise
2021-03-26 03:07:42 +01:00
const [urlToLoad, setUrlToLoad] = useState('');
const [loading, setLoading] = useState(true);
const mountedRef = useRef(true);
async function fetchUrl() {
perfStart(`getDecryptedMediaUrl${url}`);
2021-04-13 06:10:31 +02:00
const decryptedUrl = await getDecryptedMediaUrl(url, contentType);
perfEnd(`getDecryptedMediaUrl${url}`, 'getDecryptedMediaUrl');
if (mountedRef.current) {
setUrlToLoad(decryptedUrl);
2021-03-26 03:07:42 +01:00
setLoading(false);
}
}
useEffect(() => {
setLoading(true);
mountedRef.current = true;
void fetchUrl();
2021-07-22 08:34:17 +02:00
return () => {
mountedRef.current = false;
};
}, [url]);
2021-03-26 03:07:42 +01:00
return { urlToLoad, loading };
};