Created
October 10, 2024 11:03
-
-
Save kaineer/d40fa4204032d02a2844407eb0a2bbbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| 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