import 'swiper/css' import { AnimatePresence, motion, useAnimation } from 'framer-motion' import Image from 'next/image' import Link from 'next/link' import { ReactNode, useEffect, useState } from 'react' import { Swiper, SwiperSlide } from 'swiper/react' import { Button } from 'ui' import CodeBlock from '~/components/CodeBlock/CodeBlock' import SectionContainer from '~/components/Layouts/SectionContainer' import { ArrowUpRight } from 'lucide-react' interface TabProps { label: string paragraph?: string isActive: boolean onClick: VoidFunction progress: number | undefined intervalDuration?: number } const Tab = ({ isActive, label, paragraph, onClick, progress, intervalDuration }: TabProps) => ( ) interface Tab { label: string paragraph?: string panel?: JSX.Element colabUrl: string code?: string } interface Props { title: string | ReactNode paragraph: string cta?: { label?: string link: string } tabs: Tab[] intervalDuration?: number updateFrequency?: number } const TimedTabsSection = ({ title, paragraph, cta, tabs, intervalDuration = 25, updateFrequency = 10, }: Props) => { const [activeTab, setActiveTab] = useState(0) const [progress, setProgress] = useState(0) const [apiSwiper, setApiSwiper] = useState(undefined) const controls = useAnimation() useEffect(() => { if (!apiSwiper) return // @ts-ignore apiSwiper.slideTo(activeTab) }, [activeTab]) const restartInterval = (tabIndex: number) => { setProgress(0) setActiveTab(tabIndex) } const animation = { width: '100%', transition: { duration: intervalDuration, ease: 'linear' }, } useEffect(() => { const progressIncrement = (100 / intervalDuration) * (updateFrequency / 1000) controls.start(animation) const progressInterval = setInterval(() => { setProgress((prevProgress) => (prevProgress + progressIncrement) % 101) }, updateFrequency) return () => { clearInterval(progressInterval) setProgress(0) } }, [activeTab, controls]) useEffect(() => { if (progress >= 100.9) { setActiveTab((prevActiveTab) => (prevActiveTab === tabs.length - 1 ? 0 : prevActiveTab + 1)) } }, [progress]) const handleTabClick = (tabIndex: number) => { restartInterval(tabIndex) } return (

{title}

{paragraph}

{cta && ( )}
{tabs.map((tab, i) => ( <> {tab.code} ))}
{tabs.map((tab, index) => ( handleTabClick(index)} progress={index === activeTab ? progress : undefined} intervalDuration={intervalDuration} /> ))}
{tabs[activeTab]?.paragraph}
) } const OpenInColab = ({ colabUrl, className }: { colabUrl: string; className?: string }) => ( Google Colaboratory logo Open in Colab ) export default TimedTabsSection