Skip to content

Commit 4de65d7

Browse files
committed
refactor: remove duplication on fetch speaker logic
1 parent f49aebc commit 4de65d7

File tree

19 files changed

+327
-379
lines changed

19 files changed

+327
-379
lines changed

src/2023/Home/components/SpeakersCarousel/SpeakerSwiper.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import "swiper/swiper-bundle.min.css";
77
import "./SpeakersCarousel.scss";
88
import { Link } from "react-router";
99
import { ROUTE_SPEAKER_DETAIL } from "../../../../constants/routes";
10-
import { useFetchSpeakers } from "../../../Speakers/UseFetchSpeakers";
10+
import { useFetchSpeakers } from "../../../../hooks/useFetchSpeakers";
1111
import * as Sentry from "@sentry/react";
1212

1313
const StyledSlideImage = styled.img`
@@ -35,7 +35,7 @@ const StyledSlideText = styled.p`
3535
color: white;
3636
`;
3737
const SpeakerSwiper: FC<React.PropsWithChildren<unknown>> = () => {
38-
const { isLoading, data, error } = useFetchSpeakers();
38+
const { isLoading, data, error } = useFetchSpeakers("2023");
3939

4040
const swiperSpeakers = data?.sort(() => 0.5 - Math.random()).slice(0, 20);
4141

src/2023/SpeakerDetail/SpeakerDetailContainer2023.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import SpeakerDetail2023 from "./SpeakerDetail2023";
66
import { useParams } from "react-router";
77
import { StyledContainer, StyledWaveContainer } from "./Speaker.style";
88
import conferenceData from "../../data/2023.json";
9-
import { useFetchSpeakers } from "../Speakers/UseFetchSpeakers";
9+
import { useFetchSpeakers } from "../../hooks/useFetchSpeakers";
1010
import * as Sentry from "@sentry/react";
1111

1212
const SpeakerDetailContainer2023: FC<React.PropsWithChildren<unknown>> = () => {
1313
const { id } = useParams<{ id: string }>();
1414

15-
const { isLoading, error, data } = useFetchSpeakers(id);
15+
const { isLoading, error, data } = useFetchSpeakers("2023", id);
1616

1717
if (error) {
1818
Sentry.captureException(error);

src/2023/Speakers/Speakers2023.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
import webData from "../../data/2023.json";
2121
import Button from "../../components/UI/Button";
2222
import {gaEventTracker} from "../../components/analytics/Analytics";
23-
import {useFetchSpeakers} from "./UseFetchSpeakers";
23+
import {useFetchSpeakers} from "../../hooks/useFetchSpeakers";
2424
import * as Sentry from "@sentry/react";
2525
import {ISpeaker} from "../../types/speakers";
2626

@@ -41,7 +41,7 @@ const Speakers2023: FC<React.PropsWithChildren<unknown>> = () => {
4141
const isBetween = (startDay: Date, endDay: Date): boolean =>
4242
startDay < new Date() && endDay > today;
4343

44-
const { error, data, isLoading } = useFetchSpeakers();
44+
const { error, data, isLoading } = useFetchSpeakers("2023");
4545

4646
if (error) {
4747
Sentry.captureException(error);

src/2023/Speakers/UseFetchSpeakers.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/2023/TalkDetail/TalkDetailContainer2023.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {useParams} from "react-router";
77
import conferenceData from "../../data/2023.json";
88
import {useFetchTalksById} from "../Talks/UseFetchTalks";
99
import * as Sentry from "@sentry/react";
10-
import {useFetchSpeakers} from "../Speakers/UseFetchSpeakers";
10+
import {useFetchSpeakers} from "../../hooks/useFetchSpeakers";
1111
import {Session} from "../../types/sessions";
1212
import TalkDetail from "./TalkDetail";
1313
import {ISpeaker} from "../../types/speakers";
@@ -19,7 +19,7 @@ const StyledContainer = styled.div`
1919
const TalkDetailContainer2023: FC<React.PropsWithChildren<unknown>> = () => {
2020
const { id } = useParams<{ id: string }>();
2121
const { isLoading, error, data } = useFetchTalksById(id!);
22-
const { data: speakerData } = useFetchSpeakers();
22+
const { data: speakerData } = useFetchSpeakers("2023");
2323

2424
const getTalkSpeakers = (
2525
data: Session[] | undefined,

src/2024/SpeakerDetail/SpeakerDetailContainer2024.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import SectionWrapper from "../../components/SectionWrapper/SectionWrapper";
55
import SpeakerDetail from "./SpeakerDetail";
66
import {useParams} from "react-router";
77
import conferenceData from "../../data/2024.json";
8-
import {useFetchSpeakers} from "../Speakers/UseFetchSpeakers";
8+
import {useFetchSpeakers} from "../../hooks/useFetchSpeakers";
99
import * as Sentry from "@sentry/react";
1010
import {StyledContainer} from "../../views/SpeakerDetail/Speaker.style";
1111
import {StyledWaveContainer} from "../../views/Talks/Talks.style";
1212

1313
const SpeakerDetailContainer2024: FC<React.PropsWithChildren<unknown>> = () => {
1414
const {id} = useParams<{ id: string }>();
1515

16-
const {isLoading, error, data} = useFetchSpeakers(id);
16+
const {isLoading, error, data} = useFetchSpeakers("2024", id);
1717

1818
if (error) {
1919
Sentry.captureException(error);

src/2024/Speakers/Speakers2024.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import webData from "../../data/2024.json";
2020
import Button from "../../components/UI/Button";
2121
import {gaEventTracker} from "../../components/analytics/Analytics";
22-
import {useFetchSpeakers} from "./UseFetchSpeakers";
22+
import {useFetchSpeakers} from "../../hooks/useFetchSpeakers";
2323
import * as Sentry from "@sentry/react";
2424
import {SpeakerCard} from "../../views/Speakers/components/SpeakersCard";
2525
import {ISpeaker} from "../../types/speakers";
@@ -41,7 +41,7 @@ const Speakers2024: FC<React.PropsWithChildren<unknown>> = () => {
4141
const isBetween = (startDay: Date, endDay: Date): boolean =>
4242
startDay < new Date() && endDay > today;
4343

44-
const {error, data, isLoading} = useFetchSpeakers();
44+
const {error, data, isLoading} = useFetchSpeakers("2024");
4545

4646
if (error) {
4747
Sentry.captureException(error);

src/2024/Speakers/UseFetchSpeakers.test.tsx

Lines changed: 0 additions & 144 deletions
This file was deleted.

src/2024/Speakers/UseFetchSpeakers.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/2024/SpeakersCarousel/SpeakerSwiper.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import "swiper/swiper-bundle.min.css";
66
import "./SpeakersCarousel.scss";
77
import {Link} from "react-router";
88
import conferenceData from "../../data/2024.json";
9-
import {useFetchSpeakers} from "../Speakers/UseFetchSpeakers";
9+
import {useFetchSpeakers} from "../../hooks/useFetchSpeakers";
1010
import * as Sentry from "@sentry/react";
1111
import {Color} from "../../styles/colors";
1212
import {ROUTE_SPEAKER_DETAIL} from "../../constants/routes";
@@ -36,28 +36,28 @@ const StyledSlideText = styled.p`
3636
color: white;
3737
`;
3838
const SpeakerSwiper: FC<React.PropsWithChildren<unknown>> = () => {
39-
const {isLoading, data, error} = useFetchSpeakers();
39+
const {isLoading, data, error} = useFetchSpeakers("2024");
4040

4141
// Securely shuffle the speakers using Fisher-Yates algorithm with crypto API
4242
const swiperSpeakers = React.useMemo(() => {
4343
if (!data) return null;
44-
44+
4545
// Create a copy of the data to avoid mutating the original
4646
const speakersCopy = [...data];
47-
47+
4848
// Fisher-Yates shuffle with crypto.getRandomValues for secure randomization
4949
for (let i = speakersCopy.length - 1; i > 0; i--) {
5050
// Generate a secure random value using crypto API
5151
const randomBuffer = new Uint32Array(1);
5252
window.crypto.getRandomValues(randomBuffer);
53-
53+
5454
// Use the random value to get an index between 0 and i (inclusive)
5555
const j = randomBuffer[0] % (i + 1);
56-
56+
5757
// Swap elements at i and j
5858
[speakersCopy[i], speakersCopy[j]] = [speakersCopy[j], speakersCopy[i]];
5959
}
60-
60+
6161
// Return the first 20 speakers from the shuffled array
6262
return speakersCopy.slice(0, 20);
6363
}, [data]);

0 commit comments

Comments
 (0)