import { Link, useLocation, useNavigate } from "react-router"; import clsx from "clsx"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { getBestActiveRouteIndex } from "~/utils/navigation/routeMatching"; import InputSelect from "../input/InputSelect"; export interface TabItem { name: any; routePath?: string; exact?: boolean; } interface Props { className?: string; tabs: TabItem[]; asLinks?: boolean; onSelected?: (idx: number) => void; breakpoint?: "sm" | "md" | "lg" | "xl" | "2xl"; exact?: boolean; selectedTab?: number; } export default function Tabs({ className = "", breakpoint = "md", tabs = [], asLinks = true, onSelected, exact: _exact, selectedTab = 0 }: Props) { const { t } = useTranslation(); const navigate = useNavigate(); const location = useLocation(); const [selected, setSelected] = useState(selectedTab); useEffect(() => { const activeIndex = getBestActiveRouteIndex(tabs, location.pathname); if (activeIndex >= 0) setSelected(activeIndex); // eslint-disable-next-line react-hooks/exhaustive-deps }, [tabs, location.pathname]); function selectTab(idx: number) { const tab = tabs[idx]; setSelected(idx); if (asLinks) { if (tab?.routePath) { navigate(tab.routePath); } } else { if (onSelected) { onSelected(idx); } } } function isCurrent(idx: number) { return currentTab() === tabs[idx]; } const currentTab = () => { if (asLinks) { const activeIndex = getBestActiveRouteIndex(tabs, location.pathname); return activeIndex >= 0 ? tabs[activeIndex] : undefined; } else { return tabs[selected]; } }; return (