@@ -25,13 +25,27 @@ const isExternalImage = (src: string): boolean => src.startsWith('http');
25
25
const getImageUrl = ( src : string , imgPath : string ) : string =>
26
26
isExternalImage ( src ) ? src : imgPath ;
27
27
28
- const hasValidDimensions = ( width ?: number , height ?: number ) : width is number =>
29
- width != null &&
30
- height != null &&
31
- ! isNaN ( width ) &&
32
- ! isNaN ( height ) &&
33
- width > 0 &&
34
- height > 0 ;
28
+ type ValidDimensions = {
29
+ height : number ;
30
+ width : number ;
31
+ } ;
32
+
33
+ const getValidDimensions = (
34
+ width ?: number ,
35
+ height ?: number
36
+ ) : ValidDimensions | null => {
37
+ if (
38
+ width != null &&
39
+ height != null &&
40
+ ! isNaN ( width ) &&
41
+ ! isNaN ( height ) &&
42
+ width > 0 &&
43
+ height > 0
44
+ ) {
45
+ return { width, height} ;
46
+ }
47
+ return null ;
48
+ } ;
35
49
36
50
export function ImageLightbox ( {
37
51
src,
@@ -48,7 +62,7 @@ export function ImageLightbox({
48
62
// Check if we should use Next.js Image or regular img
49
63
// Use Next.js Image for internal images with valid dimensions
50
64
// Use regular img for external images or when dimensions are invalid/missing
51
- const shouldUseNextImage = ! isExternalImage ( src ) && hasValidDimensions ( width , height ) ;
65
+ const validDimensions = ! isExternalImage ( src ) ? getValidDimensions ( width , height ) : null ;
52
66
53
67
const handleClick = ( e : React . MouseEvent ) => {
54
68
// If Ctrl/Cmd+click, open image in new tab
@@ -88,13 +102,13 @@ export function ImageLightbox({
88
102
89
103
// Render the appropriate image component
90
104
const renderImage = ( ) => {
91
- if ( shouldUseNextImage ) {
92
- // Type assertion is safe here because hasValidDimensions guarantees these are valid numbers
105
+ if ( validDimensions ) {
106
+ // TypeScript knows validDimensions.width and validDimensions.height are both numbers
93
107
return (
94
108
< Image
95
109
src = { src }
96
- width = { width as number }
97
- height = { height as number }
110
+ width = { validDimensions . width }
111
+ height = { validDimensions . height }
98
112
style = { {
99
113
width : '100%' ,
100
114
height : 'auto' ,
@@ -148,12 +162,12 @@ export function ImageLightbox({
148
162
149
163
{ /* Image container */ }
150
164
< div className = "relative flex items-center justify-center" >
151
- { shouldUseNextImage ? (
165
+ { validDimensions ? (
152
166
< Image
153
167
src = { src }
154
168
alt = { alt }
155
- width = { width as number }
156
- height = { height as number }
169
+ width = { validDimensions . width }
170
+ height = { validDimensions . height }
157
171
className = "max-h-[90vh] max-w-[90vw] object-contain"
158
172
style = { {
159
173
width : 'auto' ,
0 commit comments