Skip to content

Commit

Permalink
feat(mespapiers): Add useRotateImage hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Merkur39 committed May 30, 2023
1 parent 93afdea commit 7199108
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useEffect, useRef, useState } from 'react'

import log from 'cozy-logger'

import { makeRotatedImage } from '../ModelSteps/helpers'

export const useRotatedImage = (imageSrc, rotation) => {
const imageRef = useRef(null)
const [rotatedImage, setRotatedImage] = useState(imageSrc)

useEffect(() => {
const rotateImage = async () => {
let currImage = imageRef.current

if (currImage?.currentSrc !== imageSrc) {
currImage = new Image()
imageRef.current = currImage

currImage.src = imageSrc
}

try {
await currImage.decode()
setRotatedImage(makeRotatedImage(currImage, rotation))
} catch (e) {
log('error', `Rotate image failed : ${e.message}`)
setRotatedImage(imageSrc)
}
}

if (typeof rotation === 'number') {
rotateImage()
}
}, [imageSrc, rotation])

return rotatedImage
}

0 comments on commit 7199108

Please sign in to comment.