Skip to content

The open alternative to Salesforce, designed for AI.

3.1 KB
Raw Blame
import { useCallback } from 'react';

import { useEnrichHeadlessCommandContextApiWithWorkflowVersionTriggerInformation } from '@/command-menu-item/engine-command/hooks/useEnrichHeadlessCommandContextApiWithWorkflowVersionTriggerInformation';
import { headlessCommandContextApisState } from '@/command-menu-item/engine-command/states/headlessCommandContextApisState';
import { buildHeadlessCommandContextApi } from '@/command-menu-item/engine-command/utils/buildHeadlessCommandContextApi';
import { useStore } from 'jotai';
import { isDefined } from 'twenty-shared/utils';
import {
  type CommandMenuItemAvailabilityType,
  type EngineComponentKey,
  type CommandMenuItemPayload,
} from '~/generated-metadata/graphql';

type MountCommandParams = {
  engineCommandId: string;
  contextStoreInstanceId: string;
  engineComponentKey: EngineComponentKey;
  frontComponentId?: string;
  workflowVersionId?: string;
  coreWorkflowVersionId?: string;
  availabilityType?: CommandMenuItemAvailabilityType;
  availabilityObjectMetadataId?: string | null;
  payload?: CommandMenuItemPayload | null;
  navigationTargetObjectMetadataId?: string | null;
  creationTargetObjectMetadataId?: string;
  isInSidePanel?: boolean;
};

export const useMountCommand = () => {
  const store = useStore();

  const {
    enrichHeadlessCommandContextApiWithWorkflowVersionTriggerInformation,
  } = useEnrichHeadlessCommandContextApiWithWorkflowVersionTriggerInformation();

  const mountCommand = useCallback(
    async ({
      engineCommandId,
      contextStoreInstanceId,
      engineComponentKey,
      frontComponentId,
      workflowVersionId,
      coreWorkflowVersionId,
      availabilityType,
      availabilityObjectMetadataId,
      payload,
      navigationTargetObjectMetadataId,
      creationTargetObjectMetadataId,
      isInSidePanel,
    }: MountCommandParams) => {
      const headlessEngineCommandContextApi = buildHeadlessCommandContextApi({
        store,
        contextStoreInstanceId,
        engineComponentKey,
        payload,
        navigationTargetObjectMetadataId,
        creationTargetObjectMetadataId,
        isInSidePanel,
      });

      const commandState = isDefined(frontComponentId)
        ? { ...headlessEngineCommandContextApi, frontComponentId }
        : (isDefined(workflowVersionId) || isDefined(coreWorkflowVersionId)) &&
            isDefined(availabilityType)
          ? await enrichHeadlessCommandContextApiWithWorkflowVersionTriggerInformation(
              {
                headlessEngineCommandContextApi,
                workflowVersionId,
                coreWorkflowVersionId,
                availabilityType,
                availabilityObjectMetadataId,
              },
            )
          : headlessEngineCommandContextApi;

      if (!isDefined(commandState)) {
        return;
      }

      store.set(headlessCommandContextApisState.atom, (previousMap) => {
        const newMap = new Map(previousMap);

        newMap.set(engineCommandId, commandState);

        return newMap;
      });
    },
    [
      store,
      enrichHeadlessCommandContextApiWithWorkflowVersionTriggerInformation,
    ],
  );

  return mountCommand;
};