-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathindex.tsx
More file actions
92 lines (84 loc) · 2.45 KB
/
Copy pathindex.tsx
File metadata and controls
92 lines (84 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Autoplay from 'embla-carousel-autoplay';
import { useAtomValue } from 'jotai';
import { useEffect, useRef, useState } from 'react';
import {
Carousel,
type CarouselApi,
CarouselContent,
CarouselDots,
CarouselItem,
} from '@/components/ui/carousel';
import {
popupOpenAtom,
popupsShowedAtom,
} from '@/features/conversion-popups/atoms';
import { HomeCryptoWorldFairHackathonBanner } from './CryptoWorldFairHackathonBanner';
import { HomeSponsorBanner } from './SponsorBanner';
import { HomeTalentBanner } from './TalentBanner';
interface BannerCarouselProps {
readonly totalUsers?: number | null;
readonly totalSponsors?: number | null;
}
/**
* an optimized carousel that renders a static version first for performance,
* and then progressively enhances to the full interactive version on the client.
*/
export function BannerCarousel({
totalUsers,
totalSponsors,
}: BannerCarouselProps) {
const plugin = useRef(
Autoplay({
delay: 4000,
stopOnInteraction: false,
stopOnFocusIn: false,
}),
);
const isPopupOpen = useAtomValue(popupOpenAtom);
const popupsShowed = useAtomValue(popupsShowedAtom);
const [carouselApi, setCarouselApi] = useState<CarouselApi | undefined>(
undefined,
);
useEffect(() => {
const autoplay = carouselApi?.plugins()?.autoplay;
if (autoplay) {
if (isPopupOpen) return autoplay.stop();
if (popupsShowed > 0) {
const resumeTimer: ReturnType<typeof setTimeout> = setTimeout(() => {
carouselApi.scrollNext();
plugin.current.options.delay = 5000;
autoplay.play();
}, 2000);
return () => clearTimeout(resumeTimer);
}
}
}, [isPopupOpen, carouselApi, popupsShowed]);
return (
<Carousel
plugins={[plugin.current]}
className="w-full"
opts={{ loop: true }}
setApi={setCarouselApi}
>
<CarouselContent>
<CarouselItem>
<HomeCryptoWorldFairHackathonBanner />
</CarouselItem>
<CarouselItem>
<HomeTalentBanner totalUsers={totalUsers} />
</CarouselItem>
<CarouselItem>
<HomeSponsorBanner
totalUsers={totalUsers}
totalSponsors={totalSponsors}
/>
</CarouselItem>
</CarouselContent>
<CarouselDots
className="absolute bottom-3 left-2/4 -translate-x-2/4 md:bottom-6"
activeDotClassName="bg-white"
dotClassName="bg-slate-400"
/>
</Carousel>
);
}