Skip to content

Instantly share code, notes, and snippets.

@kaineer
Created October 10, 2024 11:03
Show Gist options
  • Save kaineer/d40fa4204032d02a2844407eb0a2bbbe to your computer and use it in GitHub Desktop.
Save kaineer/d40fa4204032d02a2844407eb0a2bbbe to your computer and use it in GitHub Desktop.
//
import { prepareBaseUrl, getLocalStorageToken } from '@/app/core/utils/baseQuery';
import { useEffect, useRef } from 'react';
import { useParams } from 'react-router';
const LoadingImage = () => {
const { fileId } = useParams();
const baseUrl = prepareBaseUrl({
apiVersion: 2,
});
const imgRef = useRef(null);
useEffect(() => {
const blobToDataURL = (blob: Blob, fn: any) => {
const fr = new FileReader();
fr.onload = (e) => fn(e.target?.result);
fr.readAsDataURL(blob);
}
fetch(baseUrl + '/file/' + fileId, {
headers: {
'Authorization': 'Bearer ' + getLocalStorageToken()
}
})
.then((response) => {
return response.blob();
})
.then((blob) => {
blobToDataURL(blob, (url: string) => {
if (imgRef.current) {
imgRef.current.src = url;
}
});
});
}, []);
return (
<img src="" alt="" ref={imgRef} />
)
}
export default LoadingImage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment