11/* eslint-disable @next/next/no-img-element */
22import React , { useEffect , useRef , useState } from "react" ;
33import lottie from "lottie-web" ;
4- import { generateLottie } from "../lottiefiles/generateLottie" ;
4+ import { generateLottie , LottieAnimation } from "../lottiefiles/generateLottie" ;
55import { ClassificationEnum } from "../types/enums" ;
66
7+ interface ImageDimensions {
8+ width : number ;
9+ height : number ;
10+ }
11+
12+ const getImageMeta = ( url : string ) : Promise < HTMLImageElement > =>
13+ new Promise ( ( resolve , reject ) => {
14+ const img = new Image ( ) ;
15+ img . onload = ( ) => resolve ( img ) ;
16+ img . onerror = ( ) => reject ( new Error ( `Failed to load image: ${ url } ` ) ) ;
17+ img . src = url ;
18+ } ) ;
19+
20+ const getDimensions = async ( imageData : string ) : Promise < ImageDimensions > => {
21+ try {
22+ const image = await getImageMeta ( imageData ) ;
23+ const width = image . naturalWidth ;
24+ const height = image . naturalHeight ;
25+
26+ return { width, height } ;
27+ } catch ( error ) {
28+ console . error ( "Failed to load image dimensions:" , error ) ;
29+ return { width : 500 , height : 500 } ;
30+ }
31+ } ;
32+
733const ReviewedImage = ( {
834 imageUrl,
935 title = "" ,
@@ -13,49 +39,12 @@ const ReviewedImage = ({
1339 title : string ;
1440 classification ?: keyof typeof ClassificationEnum ;
1541} ) => {
16- const [ animation , setAnimation ] = useState < any > ( null ) ;
17- const container = useRef ( null ) ;
18- const getImageMeta = ( url ) =>
19- new Promise ( ( resolve , reject ) => {
20- const img = new Image ( ) ;
21- img . onload = ( ) => resolve ( img ) ;
22- img . onerror = ( err ) => reject ( err ) ;
23- img . src = url ;
24- } ) ;
42+ const [ animation , setAnimation ] = useState < LottieAnimation | null > ( null ) ;
43+ const container = useRef < HTMLDivElement > ( null ) ;
2544
26- // get the natural width and height of the image or fit it to the window
27- const getDimensions = async ( imageData : any ) => {
28- try {
29- const image = await getImageMeta ( imageData ) ;
30-
31- // @ts -ignore
32- let { naturalWidth : width , naturalHeight : height } = image ;
33- const windowHeight = window . innerHeight ;
34- // Responsive width calculation
35- // Mobile: use 90% of viewport for better display
36- // Desktop: use 1.5 ratio (was 2.25, now larger images)
37- const isMobile = window . innerWidth < 768 ;
38- const windowWidth = isMobile
39- ? window . innerWidth * 0.9
40- : window . innerWidth / 1.5 ;
41-
42- const aspectRatio = width / height ;
43-
44- if ( height > windowHeight ) {
45- height = windowHeight ;
46- width = height * aspectRatio ;
47- }
48- if ( width > windowWidth ) {
49- width = windowWidth ;
50- height = width / aspectRatio ;
51- }
52- return { width, height } ;
53- } catch ( error ) {
54- console . log ( error ) ;
55- return { width : 500 , height : 500 } ;
56- }
57- } ;
5845 useEffect ( ( ) => {
46+ if ( ! classification ) return ;
47+
5948 getDimensions ( imageUrl ) . then ( ( { width, height } ) => {
6049 const newAnimation = generateLottie (
6150 classification ,
@@ -65,7 +54,7 @@ const ReviewedImage = ({
6554 ) ;
6655 setAnimation ( newAnimation ) ;
6756 } ) ;
68- } , [ ] ) ;
57+ } , [ imageUrl , classification ] ) ;
6958
7059 useEffect ( ( ) => {
7160 if ( classification ) {
@@ -76,8 +65,7 @@ const ReviewedImage = ({
7665 autoplay : false ,
7766 animationData : animation ,
7867 rendererSettings : {
79- preserveAspectRatio : "xMinYMin meet" ,
80- viewBoxSize : "10 10" ,
68+ preserveAspectRatio : "xMidYMid meet" ,
8169 } ,
8270 } ) ;
8371 lottie . setSpeed ( 1.5 ) ;
@@ -105,9 +93,13 @@ const ReviewedImage = ({
10593 onMouseEnter = { handleMouseEnter }
10694 onMouseLeave = { handleMouseLeave }
10795 ref = { container }
96+ aria-hidden = "true"
10897 style = { {
10998 maxWidth : "100%" ,
11099 maxHeight : "100vh" ,
100+ display : "flex" ,
101+ justifyContent : "center" ,
102+ alignItems : "center" ,
111103 } }
112104 />
113105 ) }
@@ -119,6 +111,8 @@ const ReviewedImage = ({
119111 style = { {
120112 maxWidth : "100%" ,
121113 maxHeight : "100vh" ,
114+ display : "block" ,
115+ margin : "0 auto" ,
122116 } }
123117 />
124118 ) }
0 commit comments