Skip to content

The open alternative to Salesforce, designed for AI.

1.1 KB
Raw Blame
import { type PageLayoutTab } from '@/page-layout/types/PageLayoutTab';
import { isDefined } from 'twenty-shared/utils';

type GetPageLayoutTabListInitialActiveTabIdParams = {
  activeTabId: string | null;
  tabs: PageLayoutTab[];
  defaultTabToFocusOnMobileAndSidePanelId?: string;
  isMobile: boolean;
  isInSidePanel: boolean;
};

export const getPageLayoutTabListInitialActiveTabId = ({
  activeTabId,
  tabs,
  defaultTabToFocusOnMobileAndSidePanelId,
  isMobile,
  isInSidePanel,
}: GetPageLayoutTabListInitialActiveTabIdParams): string | null => {
  const activeTabExists = tabs.some((tab) => tab.id === activeTabId);

  if (activeTabExists) {
    return activeTabId;
  }

  const isOnMobileOrSidePanel = isMobile || isInSidePanel;

  if (
    isOnMobileOrSidePanel &&
    isDefined(defaultTabToFocusOnMobileAndSidePanelId)
  ) {
    const defaultTabExists = tabs.some(
      (tab) => tab.id === defaultTabToFocusOnMobileAndSidePanelId,
    );

    if (defaultTabExists) {
      return defaultTabToFocusOnMobileAndSidePanelId;
    }
  }

  return tabs[0]?.id ?? null;
};