Skip to content

Commit 501c5dc

Browse files
committed
fix: fix swiper
1 parent 5c4b735 commit 501c5dc

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/2024/SpeakersCarousel/SpeakerSwiper.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,29 @@ const StyledSlideText = styled.p`
3838
const SpeakerSwiper: FC<React.PropsWithChildren<unknown>> = () => {
3939
const {isLoading, data, error} = useFetchSpeakers();
4040

41-
const swiperSpeakers = data?.sort(() => 0.5 - Math.random()).slice(0, 20);
41+
// Securely shuffle the speakers using Fisher-Yates algorithm with crypto API
42+
const swiperSpeakers = React.useMemo(() => {
43+
if (!data) return null;
44+
45+
// Create a copy of the data to avoid mutating the original
46+
const speakersCopy = [...data];
47+
48+
// Fisher-Yates shuffle with crypto.getRandomValues for secure randomization
49+
for (let i = speakersCopy.length - 1; i > 0; i--) {
50+
// Generate a secure random value using crypto API
51+
const randomBuffer = new Uint32Array(1);
52+
window.crypto.getRandomValues(randomBuffer);
53+
54+
// Use the random value to get an index between 0 and i (inclusive)
55+
const j = randomBuffer[0] % (i + 1);
56+
57+
// Swap elements at i and j
58+
[speakersCopy[i], speakersCopy[j]] = [speakersCopy[j], speakersCopy[i]];
59+
}
60+
61+
// Return the first 20 speakers from the shuffled array
62+
return speakersCopy.slice(0, 20);
63+
}, [data]);
4264

4365
if (error) {
4466
Sentry.captureException(error);

0 commit comments

Comments
 (0)