@@ -19,6 +19,20 @@ interface ImageLightboxProps
19
19
width ?: number ;
20
20
}
21
21
22
+ // Helper functions
23
+ const isExternalImage = ( src : string ) : boolean => src . startsWith ( 'http' ) ;
24
+
25
+ const getImageUrl = ( src : string , imgPath : string ) : string =>
26
+ isExternalImage ( src ) ? src : imgPath ;
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 ;
35
+
22
36
export function ImageLightbox ( {
23
37
src,
24
38
alt,
@@ -34,19 +48,12 @@ export function ImageLightbox({
34
48
// Check if we should use Next.js Image or regular img
35
49
// Use Next.js Image for internal images with valid dimensions
36
50
// Use regular img for external images or when dimensions are invalid/missing
37
- const shouldUseNextImage =
38
- ! src . startsWith ( 'http' ) && // Internal image
39
- width != null &&
40
- height != null && // Dimensions provided
41
- ! isNaN ( width ) &&
42
- ! isNaN ( height ) && // Valid numbers
43
- width > 0 &&
44
- height > 0 ; // Positive values
51
+ const shouldUseNextImage = ! isExternalImage ( src ) && hasValidDimensions ( width , height ) ;
45
52
46
53
const handleClick = ( e : React . MouseEvent ) => {
47
54
// If Ctrl/Cmd+click, open image in new tab
48
55
if ( e . ctrlKey || e . metaKey ) {
49
- const url = src . startsWith ( 'http' ) ? src : imgPath ;
56
+ const url = getImageUrl ( src , imgPath ) ;
50
57
const newWindow = window . open ( url , '_blank' ) ;
51
58
if ( newWindow ) {
52
59
newWindow . opener = null ; // Security: prevent opener access
@@ -63,7 +70,7 @@ export function ImageLightbox({
63
70
e . preventDefault ( ) ;
64
71
// If Ctrl/Cmd+key, open image in new tab
65
72
if ( e . ctrlKey || e . metaKey ) {
66
- const url = src . startsWith ( 'http' ) ? src : imgPath ;
73
+ const url = getImageUrl ( src , imgPath ) ;
67
74
const newWindow = window . open ( url , '_blank' ) ;
68
75
if ( newWindow ) {
69
76
newWindow . opener = null ; // Security: prevent opener access
@@ -82,11 +89,12 @@ export function ImageLightbox({
82
89
// Render the appropriate image component
83
90
const renderImage = ( ) => {
84
91
if ( shouldUseNextImage ) {
92
+ // Type assertion is safe here because hasValidDimensions guarantees these are valid numbers
85
93
return (
86
94
< Image
87
95
src = { src }
88
- width = { width ! }
89
- height = { height ! }
96
+ width = { width as number }
97
+ height = { height as number }
90
98
style = { {
91
99
width : '100%' ,
92
100
height : 'auto' ,
@@ -144,8 +152,8 @@ export function ImageLightbox({
144
152
< Image
145
153
src = { src }
146
154
alt = { alt }
147
- width = { width ! }
148
- height = { height ! }
155
+ width = { width as number }
156
+ height = { height as number }
149
157
className = "max-h-[90vh] max-w-[90vw] object-contain"
150
158
style = { {
151
159
width : 'auto' ,
0 commit comments