Skip to content

Commit 2238bf7

Browse files
committed
replace thumbnail, lightbox, hero with xs, md, xl
1 parent 04efeb1 commit 2238bf7

File tree

4 files changed

+37
-61
lines changed

4 files changed

+37
-61
lines changed

src/components/ImageRandom.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
import ImageRandomReact from '@/components/react/ImageRandom';
3-
import { getHeroImagesProps } from '@/libs/gallery/images';
3+
import { getImagesProps } from '@/libs/gallery/images';
44
55
// forward Astro (MDX) class attr to className React
66
const { class: className, ...props } = Astro.props;
77
8-
const galleryImages = await getHeroImagesProps();
8+
const galleryImages = await getImagesProps();
99
---
1010

1111
<ImageRandomReact {...props} {className} {galleryImages} client:load />

src/components/react/ImageRandom.tsx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ import { useEffect, useMemo, useState } from 'react';
33
import { getRandomElementFromArray } from '@/utils/array';
44
import { cn } from '@/utils/styles';
55

6-
import type { HeroImageProps } from '@/libs/gallery/transform';
6+
import type { ImageAttributes, ImageProps } from '@/libs/gallery/transform';
77
import type { FC, ImgHTMLAttributes } from 'react';
88

99
interface Props extends ImgHTMLAttributes<HTMLImageElement> {
1010
// Important: must pass all allImagesSrc from the server or they wont be included on client
11-
galleryImages: HeroImageProps[];
11+
galleryImages: ImageProps[];
1212
}
1313

14-
const initialImage: HeroImageProps = {
15-
blur: {
16-
src: '',
17-
width: 0,
18-
height: 0,
19-
},
20-
hero: {
21-
src: '',
22-
width: 0,
23-
height: 0,
24-
},
14+
const imageAttributes: ImageAttributes = {
15+
src: '',
16+
width: 0,
17+
height: 0,
18+
};
19+
20+
const initialImage: ImageProps = {
21+
blur: { ...imageAttributes },
22+
xs: { ...imageAttributes },
23+
md: { ...imageAttributes },
24+
xl: { ...imageAttributes },
2525
};
2626

2727
const ImageRandomReact: FC<Props> = ({ galleryImages, className, ...props }) => {
@@ -54,10 +54,10 @@ const ImageRandomReact: FC<Props> = ({ galleryImages, className, ...props }) =>
5454
/>
5555
)}
5656

57-
{image.hero.src && (
57+
{image.xl.src && (
5858
<img
5959
{...props}
60-
src={image.hero.src}
60+
src={image.xl.src}
6161
onClick={handleClick}
6262
onLoad={() => setIsLoading(false)}
6363
className={cn(
@@ -69,7 +69,7 @@ const ImageRandomReact: FC<Props> = ({ galleryImages, className, ...props }) =>
6969
/>
7070
)}
7171

72-
{!image.blur.src && !image.hero.src && <div className={cn('', className)}>placeholder</div>}
72+
{!image.blur.src && !image.xl.src && <div className={cn('', className)}>placeholder</div>}
7373
</>
7474
);
7575
};

src/libs/gallery/images.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,3 @@ export const getGalleryImagesMetadata = (): ImageMetadata[] => {
2525

2626
export const getImagesProps = async (): Promise<ImageProps[]> =>
2727
Promise.all(getGalleryImagesMetadata().map(imageMetadataToImageProps));
28-
29-
export const getHeroImagesProps = (): Promise<HeroImageProps[]> =>
30-
getImagesProps().then((imagesProps) =>
31-
imagesProps.map((imageProps) => ({
32-
blur: imageProps.blur,
33-
hero: imageProps.hero,
34-
}))
35-
);
36-
37-
export const getGalleryImagesProps = (): Promise<GalleryImageProps[]> =>
38-
getImagesProps().then((imagesProps) =>
39-
imagesProps.map((imageProps) => ({
40-
blur: imageProps.blur,
41-
thumbnail: imageProps.thumbnail,
42-
lightbox: imageProps.lightbox,
43-
}))
44-
);

src/libs/gallery/transform.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@ export interface ImageAttributes {
1212

1313
export interface ImageProps {
1414
blur: ImageAttributes;
15-
color?: ImageAttributes;
16-
thumbnail: ImageAttributes;
17-
lightbox: ImageAttributes; // fullSize, 1280x720
18-
hero: ImageAttributes;
15+
color?: ImageAttributes; // Todo:
16+
xs: ImageAttributes;
17+
md: ImageAttributes;
18+
xl: ImageAttributes; // fullSize, 1280x720
1919
}
2020

21-
export type HeroImageProps = Pick<ImageProps, 'blur' | 'hero'>;
22-
23-
export type GalleryImageProps = Pick<ImageProps, 'blur' | 'thumbnail' | 'lightbox'>;
24-
2521
export const imageMetadataToImageProps = async (
2622
imagesMetadata: ImageMetadata
2723
): Promise<ImageProps> => {
@@ -30,39 +26,36 @@ export const imageMetadataToImageProps = async (
3026
format: 'webp',
3127
};
3228

33-
const blurAstroImageProps = {
29+
const blurImageProps = {
3430
...astroImageProps,
3531
...IMAGE_SIZES.FIXED.BLUR,
36-
alt: 'Blur image',
3732
};
3833

39-
const thumbnailAstroImageProps = {
34+
const xsImageProps = {
4035
...astroImageProps,
4136
...IMAGE_SIZES.FIXED.MDX_XS_16_9,
42-
alt: 'Thumbnail image',
4337
};
4438

45-
const lightboxAstroImageProps = {
39+
const mdImageProps = {
4640
...astroImageProps,
47-
...IMAGE_SIZES.FIXED.MDX_XL_16_9,
48-
alt: 'Lightbox image',
41+
...IMAGE_SIZES.FIXED.MDX_MD_16_9,
4942
};
5043

51-
const heroAstroImageProps = {
52-
...lightboxAstroImageProps,
53-
alt: 'Hero image',
44+
const xlImageProps = {
45+
...astroImageProps,
46+
...IMAGE_SIZES.FIXED.MDX_XL_16_9,
5447
};
5548

56-
const optimizedBlurAstroImageProps = await getImage(blurAstroImageProps);
57-
const optimizedThumbnailAstroImageProps = await getImage(thumbnailAstroImageProps);
58-
const optimizedLightboxAstroImageProps = await getImage(lightboxAstroImageProps);
59-
const optimizedHeroAstroImageProps = await getImage(heroAstroImageProps);
49+
const optimizedBlurImageProps = await getImage(blurImageProps);
50+
const optimizedXsImageProps = await getImage(xsImageProps);
51+
const optimizedMdImageProps = await getImage(mdImageProps);
52+
const optimizedXlImageProps = await getImage(xlImageProps);
6053

6154
const imageProps = {
62-
blur: getImageAttributes(optimizedBlurAstroImageProps),
63-
thumbnail: getImageAttributes(optimizedThumbnailAstroImageProps),
64-
lightbox: getImageAttributes(optimizedLightboxAstroImageProps),
65-
hero: getImageAttributes(optimizedHeroAstroImageProps),
55+
blur: getImageAttributes(optimizedBlurImageProps),
56+
xs: getImageAttributes(optimizedXsImageProps),
57+
md: getImageAttributes(optimizedMdImageProps),
58+
xl: getImageAttributes(optimizedXlImageProps),
6659
};
6760

6861
return imageProps;

0 commit comments

Comments
 (0)