Skip to content

Commit 6b59878

Browse files
committed
single client image Home page works
1 parent e2009a1 commit 6b59878

File tree

5 files changed

+34
-29
lines changed

5 files changed

+34
-29
lines changed

docs/working-notes/todo4.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ git checkout feat/tailwind4-v2
5656
mdx not formatting, prettierignore, breaks them
5757
for screenshots use new not private window and select window in screenshot tool, not screen or selection, hide dock for 16/9 // important
5858
breadcrumb navigation, projects
59-
random image client component for Home page, onClick
59+
random image client component for Home page, onClick
60+
use picture with srcSet for Home image
6061
```
6162

6263

src/components/ImageRandom.astro

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
import ImageRandomReact from '@/components/react/ImageRandom';
3+
import { allImagesSrc } from '@/utils/image';
4+
5+
// forward Astro (MDX) class attr to className React
6+
const { class: className, ...props } = Astro.props;
7+
---
8+
9+
<ImageRandomReact {...props} {className} {allImagesSrc} client:load />

src/components/react/ImageRandom.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
import { useEffect, useState } from 'react';
22

3-
import { getRandomImageSrc } from '@/utils/image';
3+
import { getRandomElementFromArray } from '@/utils/array';
44
import { cn } from '@/utils/styles';
55

6-
import type { FC } from 'react';
6+
import type { FC, ImgHTMLAttributes } from 'react';
77

8-
interface Props extends React.ImgHTMLAttributes<HTMLImageElement> {}
8+
interface Props extends ImgHTMLAttributes<HTMLImageElement> {
9+
// Important: must pass all allImagesSrc from the server or they wont be included on client
10+
allImagesSrc: string[];
11+
}
912

10-
const ImageRandom: FC<Props> = ({ className, ...props }) => {
11-
const [imageSrc, setImageSrc] = useState(''); // use loader image
13+
const ImageRandomReact: FC<Props> = ({ allImagesSrc, className, ...props }) => {
14+
const [imageSrc, setImageSrc] = useState(allImagesSrc[0]);
1215

1316
useEffect(() => {
1417
const run = async () => {
15-
setImageSrc(await getRandomImageSrc());
18+
setImageSrc(getRandomElementFromArray(allImagesSrc));
1619
};
1720

1821
run();
1922
}, [setImageSrc]);
2023

2124
const handleClick = async () => {
22-
setImageSrc(await getRandomImageSrc());
25+
setImageSrc(getRandomElementFromArray(allImagesSrc));
2326
};
2427

28+
// Todo: use <picture srcSet> for responsive images
29+
2530
return (
2631
<img
2732
{...props}
@@ -33,4 +38,4 @@ const ImageRandom: FC<Props> = ({ className, ...props }) => {
3338
);
3439
};
3540

36-
export default ImageRandom;
41+
export default ImageRandomReact;

src/pages/index.mdx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +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/image';
11-
12-
{/*
13-
import ImageRandom from '../components/react/ImageRandom';
14-
*/}
7+
import ImageRandom from '../components/ImageRandom.astro';
158

169
# Hello and welcome
1710

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

2413
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.
2514

src/utils/image.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export const getAllImagesMetadata = (): ImageMetadata[] => {
2929
// static build, call only once
3030
export const allImagesMetadata = getAllImagesMetadata();
3131

32+
/*-------------------------------- Gallery page ------------------------------*/
33+
3234
// used in gallery
3335
export const imageMetadataToReactImageProps = async (
3436
imagesMetadata: ImageMetadata
@@ -67,7 +69,7 @@ export const imageMetadataToReactImageProps = async (
6769
return reactImageProps;
6870
};
6971

70-
/*-------------------------------- Random Hero image ------------------------------*/
72+
/*-------------------------------- Home page, random image ------------------------------*/
7173

7274
export const getRandomImageMetadata = (): ImageMetadata =>
7375
getRandomElementFromArray(allImagesMetadata);
@@ -82,7 +84,8 @@ export const imageMetadataToHeroImageSrc = async (
8284

8385
const lightboxAstroImageProps = {
8486
...astroImageProps,
85-
// cant use responsive sizes here, must pass viewport width from client
87+
// must use picture tag with srcSet
88+
// ...IMAGE_SIZES.RESPONSIVE.POST_HERO,
8689
...IMAGE_SIZES.FIXED.MDX_XL_16_9,
8790
alt: 'Hero image',
8891
};
@@ -93,9 +96,7 @@ export const imageMetadataToHeroImageSrc = async (
9396
return imageSrc;
9497
};
9598

96-
export const getRandomImageSrc = async (): Promise<string> => {
97-
const imageMetadata = getRandomImageMetadata();
98-
const imageSrc = await imageMetadataToHeroImageSrc(imageMetadata);
99+
export const getAllImagesSrc = (): Promise<string[]> =>
100+
Promise.all(allImagesMetadata.map(imageMetadataToHeroImageSrc));
99101

100-
return imageSrc;
101-
};
102+
export const allImagesSrc = await getAllImagesSrc();

0 commit comments

Comments
 (0)