import React, { createContext, FC, PropsWithChildren, useCallback, useContext, useEffect, useMemo, useState, } from "react"; import { isNil } from "~/utils/ramda"; import styles from "./styles.module.scss"; interface SidebarStackProps extends PropsWithChildren<{}> { tab?: number; onTabChange?: (index?: number) => void; } interface SidebarStackContextValue { activeTab?: number; setActiveTab: (val: number) => void; closeAllTabs: () => void; } const SidebarStackContext = createContext({ activeTab: undefined, setActiveTab: () => {}, closeAllTabs: () => {}, }); const SidebarCards: FC = ({ children }) => { const { activeTab } = useStackContext(); const nonEmptyChildren = useMemo(() => { if (!children) { return []; } return Array.isArray(children) ? children.filter(it => it) : [children]; }, [children]); if (isNil(activeTab) || !nonEmptyChildren[activeTab]) { return null; } return
{nonEmptyChildren[activeTab]}
; }; const SidebarStack = function({ children, tab, onTabChange, }: SidebarStackProps) { const [activeTab, setActiveTab] = useState(tab); const closeAllTabs = useCallback(() => { setActiveTab(undefined); onTabChange?.(undefined); }, []); const onChangeTab = useCallback( (index: number) => { onTabChange?.(index); setActiveTab(index); }, [onTabChange], ); useEffect(() => setActiveTab(tab), [tab]); return (
{children}
); }; SidebarStack.Cards = SidebarCards; const useStackContext = () => useContext(SidebarStackContext); export { SidebarStack, useStackContext };