Skip to content

Commit addc3a0

Browse files
committed
add ImageRandom client component on Home page
1 parent 7fc6c18 commit addc3a0

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

src/components/Gallery.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
import ReactGallery from '@/components/react/Gallery';
33
import { sliceToMod4 } from '@/utils/array';
4-
import { allImagesMetadata, imageMetadataToReactImageProps } from '@/utils/images';
4+
import { allImagesMetadata, imageMetadataToReactImageProps } from '@/utils/image';
55
import { randomizeArray } from '@/utils/objects';
66
import { cn } from '@/utils/styles';
77

src/components/react/ImageRandom.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { useEffect, useState } from 'react';
2+
3+
import { getRandomImageSrc } from '@/utils/image';
4+
import { cn } from '@/utils/styles';
5+
6+
import type { FC } from 'react';
7+
8+
interface Props extends React.ImgHTMLAttributes<HTMLImageElement> {}
9+
10+
const ImageRandom: FC<Props> = ({ className, ...props }) => {
11+
const [imageSrc, setImageSrc] = useState(''); // use loader image
12+
13+
useEffect(() => {
14+
const run = async () => {
15+
setImageSrc(await getRandomImageSrc());
16+
};
17+
18+
run();
19+
}, [setImageSrc]);
20+
21+
const handleClick = async () => {
22+
setImageSrc(await getRandomImageSrc());
23+
};
24+
25+
return (
26+
<img
27+
{...props}
28+
src={imageSrc}
29+
onClick={handleClick}
30+
className={cn('cursor-pointer', className)}
31+
alt="Random Hero image"
32+
/>
33+
);
34+
};
35+
36+
export default ImageRandom;

src/pages/index.mdx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ title: 'Home'
44
description: 'Welcome to my blog'
55
---
66

7-
import { Image } from 'astro:assets';
8-
9-
import { IMAGE_SIZES } from '../constants/image';
10-
import { getRandomImageMetadata } from '../utils/images';
7+
import ImageRandom from '../components/react/ImageRandom';
118

129
# Hello and welcome
1310

14-
<Image {...IMAGE_SIZES.RESPONSIVE.POST_HERO} src={getRandomImageMetadata()} class="sm:max-h-96 object-cover" alt="Fishing spot" />
11+
<ImageRandom className="w-full sm:max-h-96 object-cover" client:load />
1512

1613
Welcome to my coding blog. Here, I will share my coding experiments and thoughts on React.js, Next.js, Node.js, Astro, Python, DevOps, and more. Feel free to read, explore, and comment. Or, if you are feeling overwhelmed by coding, you can take a break and enjoy some random landscape photos.
1714

src/utils/images.ts renamed to src/utils/image.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,35 @@ export const imageMetadataToReactImageProps = async (
6767
return reactImageProps;
6868
};
6969

70-
// for Home page
70+
/*-------------------------------- Random Hero image ------------------------------*/
71+
7172
export const getRandomImageMetadata = (): ImageMetadata =>
7273
getRandomElementFromArray(allImagesMetadata);
74+
75+
export const imageMetadataToHeroImageSrc = async (
76+
imageMetadata: ImageMetadata
77+
): Promise<string> => {
78+
const astroImageProps = {
79+
src: imageMetadata,
80+
format: 'webp',
81+
};
82+
83+
const lightboxAstroImageProps = {
84+
...astroImageProps,
85+
// cant use responsive sizes here, must pass viewport width from client
86+
...IMAGE_SIZES.FIXED.MDX_XL_16_9,
87+
alt: 'Hero image',
88+
};
89+
90+
const optimizedImageProps = await getImage(lightboxAstroImageProps);
91+
const { src: imageSrc } = optimizedImageProps;
92+
93+
return imageSrc;
94+
};
95+
96+
export const getRandomImageSrc = async (): Promise<string> => {
97+
const imageMetadata = getRandomImageMetadata();
98+
const imageSrc = await imageMetadataToHeroImageSrc(imageMetadata);
99+
100+
return imageSrc;
101+
};

0 commit comments

Comments
 (0)