"use client"; import React, { useEffect, useState } from "react"; import { OrderedAccordionView, OrderedSidebarView, OrderedTabView, } from "./ordered-tab-views"; import { AccordionView, SidebarView, TabView } from "./tab-views"; import { DynamicTabsT } from "./types"; import { Text } from "@shared/components/text"; export function DynamicTabs(props: DynamicTabsT) { const [currentTabIndex, setCurrentTabIndex] = useState(0); const [fragment, setFragment] = useState(""); const [subitemColCount, setSubitemColCount] = useState(2); const { title, subTitle, tabs, navigationType = true, mobileSidebarTabType = true, dynamic = false, isOrderedList = false, } = props; const items = tabs?.items ?? []; useEffect(() => { if (typeof window !== "undefined") { setFragment(window.location.hash); const handleHashChange = () => { setFragment(window.location.hash); }; window.addEventListener("hashchange", handleHashChange); return () => { window.removeEventListener("hashchange", handleHashChange); }; } }, []); useEffect(() => { tabs?.items?.some((tab, index) => { const tabId = `#${tab?.anchorId || "tab-" + index}`; if (tabId === fragment) { setCurrentTabIndex(index); return true; } }); }, [tabs, fragment]); useEffect(() => { if (!dynamic) return; const updateSubitemColCount = () => { if (typeof window === "undefined") return; const width = window.innerWidth; if (width <= 640) { setSubitemColCount(2); // Small devices } else if (width > 750 || width <= 768) { setSubitemColCount(3); // Medium devices } else if (width <= 1024) { setSubitemColCount(2); // Large devices } else { setSubitemColCount(3); // Large devices } }; updateSubitemColCount(); window.addEventListener("resize", updateSubitemColCount); return () => { window.removeEventListener("resize", updateSubitemColCount); }; }, [dynamic]); const handleTabClick = (index: number, anchor: string) => { setCurrentTabIndex(index); if (typeof window !== "undefined" && fragment) { window.location.hash = anchor || ""; } }; return (
{title || subTitle ? (
{title ? ( {title} ) : null} {subTitle ? ( {subTitle} ) : null}
) : null} {dynamic && isOrderedList ? ( <>
{mobileSidebarTabType ? ( ) : ( )}
{navigationType ? ( ) : ( )}
) : ( <>
{mobileSidebarTabType ? ( ) : ( )}
{navigationType ? ( ) : ( )}
)}
); }