import { Fragment, useEffect, useState } from "react"; import clsx from "../../../utils/shared/ClassesUtils"; import UrlUtils from "../../../utils/app/UrlUtils"; import { useApp } from "../../../hooks"; import { usePathname, useRouter } from "next/navigation"; import Link from "next/link"; export interface TabItem { name: any; routePath?: string; } interface Props { className?: string; tabs: TabItem[]; asLinks?: boolean; onSelected?: (idx: number) => void; exact?: boolean; } export default function TabsVertical({ className = "", tabs = [], asLinks = true, onSelected, exact }: Props) { const { t } = useApp(); const navigate = useRouter(); const pathname = usePathname(); const [selected, setSelected] = useState(0); useEffect(() => { tabs.forEach((tab, index) => { if (tab.routePath && (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.push(tab.routePath); } } else { if (onSelected) { onSelected(idx); } } } function isCurrent(idx: number) { return currentTab() === tabs[idx]; } const currentTab = () => { if (asLinks) { if (exact) { 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 ( ); }