You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			46 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			46 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
import { useEffect, useRef, useState } from 'react';
 | 
						|
 | 
						|
import {
 | 
						|
  getAlreadyDecryptedMediaUrl,
 | 
						|
  getDecryptedMediaUrl,
 | 
						|
} from '../session/crypto/DecryptedAttachmentsManager';
 | 
						|
import { perfEnd, perfStart } from '../session/utils/Performance';
 | 
						|
 | 
						|
export const useEncryptedFileFetch = (url: string, contentType: string, isAvatar: boolean) => {
 | 
						|
  const [urlToLoad, setUrlToLoad] = useState('');
 | 
						|
  const [loading, setLoading] = useState(false);
 | 
						|
 | 
						|
  const mountedRef = useRef(true);
 | 
						|
 | 
						|
  const alreadyDecrypted = getAlreadyDecryptedMediaUrl(url);
 | 
						|
 | 
						|
  useEffect(() => {
 | 
						|
    async function fetchUrl() {
 | 
						|
      perfStart(`getDecryptedMediaUrl-${url}`);
 | 
						|
      const decryptedUrl = await getDecryptedMediaUrl(url, contentType, isAvatar);
 | 
						|
      perfEnd(`getDecryptedMediaUrl-${url}`, `getDecryptedMediaUrl-${url}`);
 | 
						|
 | 
						|
      if (mountedRef.current) {
 | 
						|
        setUrlToLoad(decryptedUrl);
 | 
						|
        setLoading(false);
 | 
						|
      }
 | 
						|
    }
 | 
						|
    if (alreadyDecrypted) {
 | 
						|
      return;
 | 
						|
    }
 | 
						|
    setLoading(true);
 | 
						|
    mountedRef.current = true;
 | 
						|
    void fetchUrl();
 | 
						|
 | 
						|
    // eslint-disable-next-line consistent-return
 | 
						|
    return () => {
 | 
						|
      mountedRef.current = false;
 | 
						|
    };
 | 
						|
  }, [url, alreadyDecrypted, contentType, isAvatar]);
 | 
						|
 | 
						|
  if (alreadyDecrypted) {
 | 
						|
    return { urlToLoad: alreadyDecrypted, loading: false };
 | 
						|
  }
 | 
						|
  return { urlToLoad, loading };
 | 
						|
};
 |