Skip to content

Commit 7fc6c18

Browse files
committed
Home page, random static full width image
1 parent a6009d0 commit 7fc6c18

File tree

7 files changed

+84
-70
lines changed

7 files changed

+84
-70
lines changed

docs/working-notes/todo4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ 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
5960
```
6061

6162

src/components/Gallery.astro

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,16 @@
11
---
2-
import { getImage } from 'astro:assets';
3-
42
import ReactGallery from '@/components/react/Gallery';
5-
import { IMAGE_SIZES } from '@/constants/image';
3+
import { sliceToMod4 } from '@/utils/array';
4+
import { allImagesMetadata, imageMetadataToReactImageProps } from '@/utils/images';
65
import { randomizeArray } from '@/utils/objects';
76
import { cn } from '@/utils/styles';
87
9-
import type { ImageProps } from '@/types/common';
10-
import type { ImageMetadata } from 'astro';
11-
128
export interface Props extends astroHTML.JSX.HTMLAttributes {}
139
14-
// filenames
15-
const EXCLUDE_IMAGES = ['avatar1.jpg'];
16-
17-
const getAllImagesMetadata = (): ImageMetadata[] => {
18-
const imageModules = import.meta.glob<{ default: ImageMetadata }>(
19-
// cant be even variable
20-
'/src/assets/images/all-images/*.jpg',
21-
{ eager: true }
22-
);
23-
24-
// convert map to array
25-
const imagesMetadata = Object.keys(imageModules)
26-
// filter excluded filenames
27-
.filter((path) => !EXCLUDE_IMAGES.some((excludedFileName) => path.endsWith(excludedFileName)))
28-
// return metadata array
29-
.map((path) => imageModules[path].default);
30-
return imagesMetadata;
31-
};
32-
33-
const imagesMetadata = getAllImagesMetadata();
34-
35-
const imageMetadataToReactImageProps = async (
36-
imagesMetadata: ImageMetadata
37-
): Promise<ImageProps> => {
38-
const astroImageProps = {
39-
src: imagesMetadata,
40-
format: 'webp',
41-
};
42-
43-
const thumbnailAstroImageProps = {
44-
...astroImageProps,
45-
...IMAGE_SIZES.FIXED.MDX_XS_16_9,
46-
alt: 'Thumbnail image',
47-
};
48-
49-
const lightboxAstroImageProps = {
50-
...astroImageProps,
51-
...IMAGE_SIZES.FIXED.MDX_XL_16_9,
52-
alt: 'Lightbox image',
53-
};
54-
55-
const optimizedThumbnailAstroImageProps = await getImage(thumbnailAstroImageProps);
56-
const { src, attributes } = optimizedThumbnailAstroImageProps;
57-
58-
const optimizedLightboxAstroImageProps = await getImage(lightboxAstroImageProps);
59-
const { src: originalSrc } = optimizedLightboxAstroImageProps;
60-
61-
const reactImageProps = {
62-
src,
63-
originalSrc,
64-
// width and height only for thumbnails
65-
width: parseInt(String(attributes.width)),
66-
height: parseInt(String(attributes.height)),
67-
};
68-
69-
return reactImageProps;
70-
};
10+
const allImagesMetadataMod4 = sliceToMod4(allImagesMetadata);
7111
7212
const reactImages = await Promise.all(
73-
imagesMetadata.map((metadata) => imageMetadataToReactImageProps(metadata))
13+
allImagesMetadataMod4.map((metadata) => imageMetadataToReactImageProps(metadata))
7414
);
7515
7616
const randomizedReactImages = randomizeArray(reactImages);

src/pages/index.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ description: 'Welcome to my blog'
77
import { Image } from 'astro:assets';
88

99
import { IMAGE_SIZES } from '../constants/image';
10-
import River10Image from '../assets/images/all-images/river10.jpg';
10+
import { getRandomImageMetadata } from '../utils/images';
1111

1212
# Hello and welcome
1313

14-
<Image {...IMAGE_SIZES.FIXED.MDX_MD} src={River10Image} alt="Fishing spot" />
14+
<Image {...IMAGE_SIZES.RESPONSIVE.POST_HERO} src={getRandomImageMetadata()} class="sm:max-h-96 object-cover" alt="Fishing spot" />
1515

1616
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.
1717

src/utils/array.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const getRandomElementFromArray = <T>(arr: T[]): T =>
2+
arr[Math.floor(Math.random() * arr.length)];
3+
4+
export const sliceToMod4 = <T>(arr: T[]): T[] => arr.slice(0, arr.length - (arr.length % 4));

src/utils/gradients.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { default as twColors } from 'tailwindcss/colors';
22

3-
import { getRandomElementFromArray as rnd } from '@/utils/strings';
3+
import { getRandomElementFromArray as rnd } from '@/utils/array';
44

55
import type { DefaultColors } from 'tailwindcss/types/generated/colors';
66

src/utils/images.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { getImage } from 'astro:assets';
2+
3+
import { IMAGE_SIZES } from '@/constants/image';
4+
import { getRandomElementFromArray } from '@/utils/array';
5+
6+
import type { ImageProps } from '@/types/common';
7+
import type { ImageMetadata } from 'astro';
8+
9+
// filenames
10+
const EXCLUDE_IMAGES = ['avatar1.jpg'];
11+
12+
export const getAllImagesMetadata = (): ImageMetadata[] => {
13+
const imageModules = import.meta.glob<{ default: ImageMetadata }>(
14+
// cant be even variable
15+
'/src/assets/images/all-images/*.jpg',
16+
{ eager: true }
17+
);
18+
19+
// convert map to array
20+
const imagesMetadata = Object.keys(imageModules)
21+
// filter excluded filenames
22+
.filter((path) => !EXCLUDE_IMAGES.some((excludedFileName) => path.endsWith(excludedFileName)))
23+
// return metadata array
24+
.map((path) => imageModules[path].default);
25+
26+
return imagesMetadata;
27+
};
28+
29+
// static build, call only once
30+
export const allImagesMetadata = getAllImagesMetadata();
31+
32+
// used in gallery
33+
export const imageMetadataToReactImageProps = async (
34+
imagesMetadata: ImageMetadata
35+
): Promise<ImageProps> => {
36+
const astroImageProps = {
37+
src: imagesMetadata,
38+
format: 'webp',
39+
};
40+
41+
const thumbnailAstroImageProps = {
42+
...astroImageProps,
43+
...IMAGE_SIZES.FIXED.MDX_XS_16_9,
44+
alt: 'Thumbnail image',
45+
};
46+
47+
const lightboxAstroImageProps = {
48+
...astroImageProps,
49+
...IMAGE_SIZES.FIXED.MDX_XL_16_9,
50+
alt: 'Lightbox image',
51+
};
52+
53+
const optimizedThumbnailAstroImageProps = await getImage(thumbnailAstroImageProps);
54+
const { src, attributes } = optimizedThumbnailAstroImageProps;
55+
56+
const optimizedLightboxAstroImageProps = await getImage(lightboxAstroImageProps);
57+
const { src: originalSrc } = optimizedLightboxAstroImageProps;
58+
59+
const reactImageProps = {
60+
src,
61+
originalSrc,
62+
// width and height only for thumbnails
63+
width: parseInt(String(attributes.width)),
64+
height: parseInt(String(attributes.height)),
65+
};
66+
67+
return reactImageProps;
68+
};
69+
70+
// for Home page
71+
export const getRandomImageMetadata = (): ImageMetadata =>
72+
getRandomElementFromArray(allImagesMetadata);

src/utils/strings.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ export const getRandomLengthSubstring = (inputString: string, length: number, ma
88
export const limitString = (str: string, maxLength: number) =>
99
str.length > maxLength ? str.slice(0, maxLength).trim() + '...' : str.trim();
1010

11-
export const getRandomElementFromArray = <T>(arr: T[]): T =>
12-
arr[Math.floor(Math.random() * arr.length)];
13-
1411
export const trimHttpProtocol = (url: string) => {
1512
const trailingSlashRegex = /\/$/;
1613
const protocolRegex = /^(https?:\/\/)/i;

0 commit comments

Comments
 (0)