import { Link, useLocation, useNavigate } from "react-router"; import clsx from "clsx"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import UrlUtils from "~/utils/app/UrlUtils"; import InputSelect from "../input/InputSelect"; export interface TabItem { name: any; routePath?: string; } 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, selectedTab = 0 }: Props) { const { t } = useTranslation(); const navigate = useNavigate(); const location = useLocation(); const [selected, setSelected] = useState(selectedTab); useEffect(() => { tabs.forEach((tab, index) => { if (tab.routePath && (location.pathname + location.search).includes(tab.routePath)) { setSelected(index); } }); // 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) { if (true) { return tabs.find((element) => element.routePath && UrlUtils.stripTrailingSlash(location.pathname) === UrlUtils.stripTrailingSlash(element.routePath)); } else { return tabs.find((element) => element.routePath && (location.pathname + location.search).includes(element.routePath)); } } else { return tabs[selected]; } }; return (