import { useApp } from "../../../hooks"; import clsx from "clsx"; import Link from "next/link"; import { usePathname, useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import UrlUtils from "../../../utils/app/UrlUtils"; 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 } = useApp(); const navigate = useRouter(); const pathname = usePathname(); const [selected, setSelected] = useState(selectedTab); useEffect(() => { if (selectedTab !== selected) { setSelected(selectedTab); } }, [selected, selectedTab]); useEffect(() => { if (asLinks) { let index = 0; tabs.forEach((tab, idx) => { if (exact) { if (tab.routePath && UrlUtils.stripTrailingSlash(pathname) === UrlUtils.stripTrailingSlash(tab.routePath)) { index = idx; } } else { if (tab.routePath && (pathname + location.search).includes(tab.routePath)) { index = idx; } } }); setSelected(index); } }, [location.pathname, location.search, tabs, asLinks, exact]); function selectTab(idx: number) { const tab = tabs[idx]; setSelected(idx); if (asLinks) { if (tab?.routePath) { navigate.push(tab.routePath); } } else { if (onSelected) { onSelected(idx); } } } return (