import { RawParams } from '@uirouter/react'; import clsx from 'clsx'; import { ReactNode } from 'react'; import { Icon } from '@@/Icon'; import { Link } from '@@/Link'; export interface Tab { name: ReactNode; icon: ReactNode; widget: ReactNode; selectedTabParam: string; } interface Props { currentTabIndex: number; tabs: Tab[]; } // https://www.figma.com/file/g5TUMngrblkXM7NHSyQsD1/New-UI?type=design&node-id=148-2676&mode=design&t=JKyBWBupeC5WADk6-0 export function WidgetTabs({ currentTabIndex, tabs }: Props) { // ensure that the selectedTab param is always valid const invalidQueryParamValue = tabs.every( (tab) => encodeURIComponent(tab.selectedTabParam) !== tab.selectedTabParam ); if (invalidQueryParamValue) { throw new Error('Invalid query param value for tab'); } return (
{tabs.map(({ name, icon }, index) => ( {name} ))}
); } // findSelectedTabIndex returns the index of the tab, or 0 if none is selected export function findSelectedTabIndex( { params }: { params: RawParams }, tabs: Tab[] ) { const selectedTabParam = params.tab || tabs[0].selectedTabParam; const currentTabIndex = tabs.findIndex( (tab) => tab.selectedTabParam === selectedTabParam ); return currentTabIndex || 0; }