src/entities/LazyImage/LazyImage.tsx   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 0

Size

Lines of Code 40
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 32
mnd 7
bc 7
fnc 0
dl 0
loc 40
rs 10
bpm 0
cpm 0
noi 14
c 0
b 0
f 0
1
import { useRef, useState } from 'react'
2
import { useIntersectionObserver } from 'shared/hooks/useIntersectionObserver'
3
4
interface IProps {
5
  imageUrl: string
6
  alt: string
7
  width: string
8
  height: string
9
}
10
11
export const LazyImage = ({ imageUrl, alt, width, height }: IProps) => {
0 ignored issues
show
introduced by
Function component is not a function declaration
Loading history...
12
  const imgRef = useRef<HTMLImageElement | null>(null)
13
  const [isLoaded, setLoaded] = useState<boolean>(false)
14
15
  const setSrc = () => {
16
    let srcRef = imgRef?.current?.src
17
    srcRef = imgRef?.current?.dataset?.src
0 ignored issues
show
introduced by
'srcRef' is assigned a value but never used.
Loading history...
18
    imgRef?.current?.removeAttribute('data-src')
19
    setLoaded(true)
20
  }
21
22
  useIntersectionObserver({
23
    target: imgRef,
24
    onIntersect: setSrc,
25
    rootMargin: '1200px',
26
    enabled: !isLoaded,
27
  })
28
29
  return (
30
    <img
0 ignored issues
show
introduced by
Expected indentation of 6 space characters but found 4.
Loading history...
introduced by
JSX not allowed in files with extension '.tsx'
Loading history...
31
      ref={imgRef}
0 ignored issues
show
introduced by
Expected indentation of 8 space characters but found 6.
Loading history...
32
      data-src={imageUrl}
0 ignored issues
show
introduced by
Expected indentation of 8 space characters but found 6.
Loading history...
introduced by
Props should be sorted alphabetically
Loading history...
33
      alt={alt}
0 ignored issues
show
introduced by
Expected indentation of 8 space characters but found 6.
Loading history...
introduced by
Props should be sorted alphabetically
Loading history...
34
      width={width}
0 ignored issues
show
introduced by
Expected indentation of 8 space characters but found 6.
Loading history...
35
      height={height}
0 ignored issues
show
introduced by
Expected indentation of 8 space characters but found 6.
Loading history...
introduced by
Props should be sorted alphabetically
Loading history...
36
      loading="lazy"
0 ignored issues
show
introduced by
Expected indentation of 8 space characters but found 6.
Loading history...
introduced by
Props should be sorted alphabetically
Loading history...
37
    />
38
  )
39
}
40