Skip to content

The open alternative to Salesforce, designed for AI.

3.4 KB
Raw Blame
import { PageLayoutComponentInstanceContext } from '@/page-layout/states/contexts/PageLayoutComponentInstanceContext';
import { pageLayoutCurrentLayoutsComponentState } from '@/page-layout/states/pageLayoutCurrentLayoutsComponentState';
import { pageLayoutDraftComponentState } from '@/page-layout/states/pageLayoutDraftComponentState';
import { type PageLayoutTab } from '@/page-layout/types/PageLayoutTab';
import { getDefaultTabLayoutMode } from '@/page-layout/utils/getDefaultTabLayoutMode';
import { getEmptyTabLayout } from '@/page-layout/utils/getEmptyTabLayout';
import { activeTabIdComponentState } from '@/ui/layout/tab-list/states/activeTabIdComponentState';
import { useAvailableComponentInstanceIdOrThrow } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceIdOrThrow';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
import { useSetAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useSetAtomComponentState';
import { t } from '@lingui/core/macro';
import { useStore } from 'jotai';
import { useCallback } from 'react';
import { v4 as uuidv4 } from 'uuid';
import { PageLayoutType } from '~/generated-metadata/graphql';

export const useCreatePageLayoutTab = ({
  pageLayoutId: pageLayoutIdFromProps,
  tabListInstanceId,
}: {
  pageLayoutId: string;
  tabListInstanceId: string;
}) => {
  const pageLayoutId = useAvailableComponentInstanceIdOrThrow(
    PageLayoutComponentInstanceContext,
    pageLayoutIdFromProps,
  );

  const pageLayoutDraftState = useAtomComponentStateCallbackState(
    pageLayoutDraftComponentState,
    pageLayoutId,
  );

  const pageLayoutCurrentLayoutsState = useAtomComponentStateCallbackState(
    pageLayoutCurrentLayoutsComponentState,
    pageLayoutId,
  );

  const setActiveTabId = useSetAtomComponentState(
    activeTabIdComponentState,
    tabListInstanceId,
  );

  const store = useStore();

  const createPageLayoutTab = useCallback(
    (title?: string): string => {
      const pageLayoutDraft = store.get(pageLayoutDraftState);

      const newTabId = uuidv4();
      const tabsLength = pageLayoutDraft.tabs.length;
      const maxPosition =
        tabsLength > 0
          ? Math.max(...pageLayoutDraft.tabs.map((t) => t.position))
          : -1;
      const newTab: PageLayoutTab = {
        id: newTabId,
        applicationId: '',
        universalIdentifier: newTabId,
        isSystemSideEffect: false,
        isActive: true,
        title: title || t`Tab ${tabsLength + 1}`,
        position: maxPosition + 1,
        pageLayoutId: pageLayoutId,
        icon:
          pageLayoutDraft.type === PageLayoutType.RECORD_PAGE
            ? 'IconPerspective'
            : null,
        layoutMode: getDefaultTabLayoutMode(pageLayoutDraft.type),
        widgets: [],
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString(),
        deletedAt: null,
      };

      const updatedTabs = [...(pageLayoutDraft.tabs ?? []), newTab];

      store.set(pageLayoutDraftState, (prev) => ({
        ...prev,
        tabs: updatedTabs,
      }));

      store.set(pageLayoutCurrentLayoutsState, (prev) =>
        getEmptyTabLayout(prev, newTabId),
      );

      setActiveTabId(newTabId);

      return newTabId;
    },
    [
      pageLayoutCurrentLayoutsState,
      pageLayoutDraftState,
      pageLayoutId,
      setActiveTabId,
      store,
    ],
  );

  return { createPageLayoutTab };
};