Skip to content

Commit c68596c

Browse files
committed
dry things up
1 parent 4065b0c commit c68596c

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

src/components/docImage.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,17 @@ export default function DocImage({
7676
dimensions = [];
7777
}
7878

79-
// Helper function to safely parse dimension values
80-
const parseDimension = (
81-
value: string | number | undefined,
82-
fallback: number = 800
83-
): number => {
79+
// Helper function to safely parse dimension values - only return valid numbers or undefined
80+
const parseDimension = (value: string | number | undefined): number | undefined => {
8481
if (typeof value === 'number' && value > 0) return value;
8582
if (typeof value === 'string') {
8683
const parsed = parseInt(value, 10);
87-
return parsed > 0 ? parsed : fallback;
84+
return parsed > 0 ? parsed : undefined;
8885
}
89-
return fallback;
86+
return undefined;
9087
};
9188

92-
// Use parsed dimensions, fallback to props, then default to 800
89+
// Use parsed dimensions, fallback to props - let ImageLightbox decide on defaults
9390
const width = dimensions[0] > 0 ? dimensions[0] : parseDimension(propsWidth);
9491
const height = dimensions[1] > 0 ? dimensions[1] : parseDimension(propsHeight);
9592

src/components/imageLightbox/index.tsx

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2236
export 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

Comments
 (0)