Skip to content

Commit 1c7aa9c

Browse files
committed
shuffle in react component
1 parent 27d15f5 commit 1c7aa9c

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/components/PeopleAvatarGrid.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,29 @@ interface Person {
1212
}
1313
}
1414

15+
function shuffleArray(array: any[]) {
16+
for (let i = array.length - 1; i > 0; i--) {
17+
const j = Math.floor(Math.random() * (i + 1))
18+
;[array[i], array[j]] = [array[j], array[i]]
19+
}
20+
return array
21+
}
22+
1523
interface PeopleAvatarGridProps {
1624
people: Person[]
1725
className?: string
26+
shuffle?: boolean
1827
}
1928

20-
export function PeopleAvatarGrid({ people, className }: PeopleAvatarGridProps) {
29+
export function PeopleAvatarGrid({
30+
people: _people,
31+
className,
32+
shuffle,
33+
}: PeopleAvatarGridProps) {
34+
let people = _people
35+
if (shuffle) {
36+
people = shuffleArray(people)
37+
}
2138
const scrollRef = React.useRef<HTMLDivElement>(null)
2239
const [showTopBlur, setShowTopBlur] = React.useState(false)
2340
const [showBottomBlur, setShowBottomBlur] = React.useState(false)

src/pages/index.astro

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,8 @@ import { getAllPeople, getAllProjects, getRecentPosts } from '@/lib/data-utils'
1111
import ImageCarousel from '@/components/ui/image-carousel'
1212
import { getCollection } from 'astro:content'
1313
14-
function shuffleArray(array: any[]) {
15-
for (let i = array.length - 1; i > 0; i--) {
16-
const j = Math.floor(Math.random() * (i + 1));
17-
[array[i], array[j]] = [array[j], array[i]];
18-
}
19-
}
20-
2114
const blog = await getRecentPosts(SITE.featuredPostCount)
2215
const people = await getAllPeople()
23-
shuffleArray(people);
2416
const projects = await getAllProjects()
2517
const groupPhotos = await getCollection('groupPhotos')
2618
---
@@ -81,7 +73,7 @@ const groupPhotos = await getCollection('groupPhotos')
8173
</Link>
8274
</div>
8375
<div class="flex-1 min-h-0 rounded-lg border p-4 bg-background">
84-
<PeopleAvatarGrid client:load people={people} className="h-full"/>
76+
<PeopleAvatarGrid client:load people={people} className="h-full" shuffle={true} />
8577
</div>
8678
</section>
8779
<section class="flex flex-col gap-1">

0 commit comments

Comments
 (0)