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