File size: 1,046 Bytes
b9fe2b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Image as AntImage } from 'antd';
import { useEffect, useState } from 'react';

import { Authorization } from '@/constants/authorization';
import { getAuthorization } from '@/utils/authorization-util';

interface ImageProps {
  src: string;
  preview?: boolean;
}

const Image = ({ src, preview = false }: ImageProps) => {
  const [imageSrc, setImageSrc] = useState<string>('');

  useEffect(() => {
    const loadImage = async () => {
      try {
        const response = await fetch(src, {
          headers: {
            [Authorization]: getAuthorization(),
          },
        });
        const blob = await response.blob();
        const objectUrl = URL.createObjectURL(blob);
        setImageSrc(objectUrl);
      } catch (error) {
        console.error('Failed to load image:', error);
      }
    };

    loadImage();

    return () => {
      if (imageSrc) {
        URL.revokeObjectURL(imageSrc);
      }
    };
  }, [src]);

  return imageSrc ? <AntImage src={imageSrc} preview={preview} /> : null;
};

export default Image;