File tree Expand file tree Collapse file tree 3 files changed +14
-7
lines changed Expand file tree Collapse file tree 3 files changed +14
-7
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { getImage } from 'astro:assets';
3
3
4
4
import ReactGallery from ' @/components/react/Gallery' ;
5
5
import { IMAGE_SIZES } from ' @/constants/image' ;
6
+ import { randomizeArray } from ' @/utils/objects' ;
6
7
import { cn } from ' @/utils/styles' ;
7
8
8
9
import type { ImageProps } from ' @/types/common' ;
@@ -72,11 +73,13 @@ const reactImages = await Promise.all(
72
73
imagesMetadata .map ((metadata ) => imageMetadataToReactImageProps (metadata ))
73
74
);
74
75
76
+ const randomizedReactImages = randomizeArray (reactImages );
77
+
75
78
// console.log('reactImages', reactImages);
76
79
77
80
const { class : className } = Astro .props ;
78
81
---
79
82
80
83
<div class ={ cn (' ' , className )} >
81
- <ReactGallery client:only =" react" images ={ reactImages } />
84
+ <ReactGallery client:only =" react" images ={ randomizedReactImages } />
82
85
</div >
Original file line number Diff line number Diff line change 1
1
import { CONFIG } from '@/config' ;
2
+ import { randomizeArray } from '@/utils/objects' ;
2
3
3
4
import type { Post } from '@/types/post' ;
4
5
@@ -22,12 +23,9 @@ export const getRandomPosts = ({
22
23
23
24
if ( ! ( filteredPosts . length > 0 ) ) return [ ] ;
24
25
25
- const shuffledPosts = filteredPosts
26
- . map ( ( post ) => ( { post, sort : Math . random ( ) } ) )
27
- . sort ( ( a , b ) => a . sort - b . sort )
28
- . map ( ( { post } ) => post ) ;
26
+ const randomizedPosts = randomizeArray ( filteredPosts ) ;
29
27
30
- if ( shuffledPosts . length < count ) return shuffledPosts ;
28
+ if ( randomizedPosts . length < count ) return randomizedPosts ;
31
29
32
- return shuffledPosts . slice ( 0 , count ) ;
30
+ return randomizedPosts . slice ( 0 , count ) ;
33
31
} ;
Original file line number Diff line number Diff line change 1
1
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2
2
export const filterUndefined = < T extends Record < string , any > > ( obj : T ) : T =>
3
3
Object . fromEntries ( Object . entries ( obj ) . filter ( ( [ _key , value ] ) => value !== undefined ) ) as T ;
4
+
5
+ export const randomizeArray = < T > ( array : T [ ] ) : T [ ] =>
6
+ array
7
+ . map ( ( item ) => ( { item, sort : Math . random ( ) } ) )
8
+ . sort ( ( a , b ) => a . sort - b . sort )
9
+ . map ( ( { item } ) => item ) ;
You can’t perform that action at this time.
0 commit comments