🔥 Get 11 random published photos on the Homepage collage

Thanks to Directus help, this works!
Concept: fetch all published photos IDs only, get 11 random items and query these 11 IDs
Only 2 queries, one for the IDs and one for the photos
This commit is contained in:
2022-06-13 15:48:29 +02:00
parent 2a3c73f6ea
commit b7a857e2e6
2 changed files with 32 additions and 8 deletions

View File

@@ -110,6 +110,15 @@ export const getRandomItem = <T extends unknown> (array: T[]): T => {
}
/**
* Return random elements from an array
*/
export const getRandomItems = <T extends unknown> (array: any[], amount: number): T[] => {
const shuffled = Array.from(array).sort(() => 0.5 - Math.random())
return shuffled.slice(0, amount)
}
/**
* Get a DOM element's position
*/