import { useEffect, useRef } from "react"; import { useTranslation } from "react-i18next"; import { useActiveIndicator } from "@/hooks/use-active-indicator"; import { cn } from "@/lib/utils"; export default function GroupSwitch({ tabs, currentTab, setCurrentTab, }: { tabs: string[]; currentTab: string; setCurrentTab: (tab: string) => void; }) { const { t } = useTranslation(); const customBackgroundImage = (window.CustomBackgroundImage as string) !== "" ? window.CustomBackgroundImage : undefined; const scrollRef = useRef(null); const { containerRef, enableIndicatorAnimation, indicator, itemRefs, setItemRef, } = useActiveIndicator(tabs, currentTab); useEffect(() => { const container = scrollRef.current; if (!container) return; const isOverflowing = container.scrollWidth > container.clientWidth; if (!isOverflowing) return; const onWheel = (e: WheelEvent) => { e.preventDefault(); container.scrollLeft += e.deltaY; }; container.addEventListener("wheel", onWheel, { passive: false }); return () => { container.removeEventListener("wheel", onWheel); }; }, []); useEffect(() => { if (tabs.length === 1 && tabs[0] === "All") { setCurrentTab("All"); return; } const savedGroup = sessionStorage.getItem("selectedGroup"); if (savedGroup && tabs.includes(savedGroup)) { setCurrentTab(savedGroup); } }, [tabs, setCurrentTab]); useEffect(() => { const currentTagRef = itemRefs.current[tabs.indexOf(currentTab)]; const scrollContainer = scrollRef.current; if (currentTagRef && scrollContainer) { const nextScrollLeft = currentTagRef.offsetLeft - scrollContainer.clientWidth / 2 + currentTagRef.offsetWidth / 2; if (typeof scrollContainer.scrollTo === "function") { scrollContainer.scrollTo({ behavior: "smooth", left: Math.max(0, nextScrollLeft), }); } else { scrollContainer.scrollLeft = Math.max(0, nextScrollLeft); } } }, [currentTab, itemRefs, tabs]); if (tabs.length === 1 && tabs[0] === "All") { return null; } return (
{indicator && (
)} {tabs.map((tab: string, index: number) => (
{ if (currentTab !== tab) { enableIndicatorAnimation(); } setCurrentTab(tab); }} className={cn( "relative cursor-pointer rounded-3xl px-2.5 py-2 text-[13px] font-semibold transition-all duration-500 ease-in-out hover:text-stone-950 hover:dark:text-stone-50", currentTab === tab ? "text-black dark:text-white" : "text-stone-400 dark:text-stone-500", )} >

{tab === "All" ? t("group.all", { defaultValue: "All" }) : tab}

))}
); }