@@ -19,6 +19,20 @@ interface ImageLightboxProps
1919 width ?: number ;
2020}
2121
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+
2236export function ImageLightbox ( {
2337 src,
2438 alt,
@@ -34,19 +48,12 @@ export function ImageLightbox({
3448 // Check if we should use Next.js Image or regular img
3549 // Use Next.js Image for internal images with valid dimensions
3650 // 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 ) ;
4552
4653 const handleClick = ( e : React . MouseEvent ) => {
4754 // If Ctrl/Cmd+click, open image in new tab
4855 if ( e . ctrlKey || e . metaKey ) {
49- const url = src . startsWith ( 'http' ) ? src : imgPath ;
56+ const url = getImageUrl ( src , imgPath ) ;
5057 const newWindow = window . open ( url , '_blank' ) ;
5158 if ( newWindow ) {
5259 newWindow . opener = null ; // Security: prevent opener access
@@ -63,7 +70,7 @@ export function ImageLightbox({
6370 e . preventDefault ( ) ;
6471 // If Ctrl/Cmd+key, open image in new tab
6572 if ( e . ctrlKey || e . metaKey ) {
66- const url = src . startsWith ( 'http' ) ? src : imgPath ;
73+ const url = getImageUrl ( src , imgPath ) ;
6774 const newWindow = window . open ( url , '_blank' ) ;
6875 if ( newWindow ) {
6976 newWindow . opener = null ; // Security: prevent opener access
@@ -82,11 +89,12 @@ export function ImageLightbox({
8289 // Render the appropriate image component
8390 const renderImage = ( ) => {
8491 if ( shouldUseNextImage ) {
92+ // Type assertion is safe here because hasValidDimensions guarantees these are valid numbers
8593 return (
8694 < Image
8795 src = { src }
88- width = { width ! }
89- height = { height ! }
96+ width = { width as number }
97+ height = { height as number }
9098 style = { {
9199 width : '100%' ,
92100 height : 'auto' ,
@@ -144,8 +152,8 @@ export function ImageLightbox({
144152 < Image
145153 src = { src }
146154 alt = { alt }
147- width = { width ! }
148- height = { height ! }
155+ width = { width as number }
156+ height = { height as number }
149157 className = "max-h-[90vh] max-w-[90vw] object-contain"
150158 style = { {
151159 width : 'auto' ,
0 commit comments