Skip to content

The open alternative to Salesforce, designed for AI.

3.6 KB
Raw Blame
import { msg } from '@lingui/core/macro';
import { getSystemNavigationCommandMenuItemUniversalIdentifier } from 'twenty-shared/application';
import { getMetadataLabelPlaceholder } from 'twenty-shared/i18n';
import { STANDARD_OBJECTS } from 'twenty-shared/metadata';
import { CommandMenuItemAvailabilityType } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';

import { EngineComponentKey } from 'src/engine/metadata-modules/command-menu-item/enums/engine-component-key.enum';
import { i18nLabel } from 'src/engine/workspace-manager/twenty-standard-application/utils/i18n-label.util';
import { type UniversalFlatCommandMenuItem } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-command-menu-item.type';

// The stored label is the source message; the target object's label stays a
// placeholder filled at read time for the reader's locale.
export const NAVIGATION_INTERPOLATED_LABEL = i18nLabel(
  msg({
    message: `Go to {objectLabelPlural}`,
    context: 'commandMenuItem.label',
  }),
);
export const NAVIGATION_INTERPOLATED_SHORT_LABEL =
  getMetadataLabelPlaceholder('objectLabelPlural');
export const NAVIGATION_INTERPOLATED_ICON =
  getMetadataLabelPlaceholder('objectIcon');

const HIDDEN_NAVIGATION_OBJECT_UNIVERSAL_IDENTIFIERS = new Set<string>([
  STANDARD_OBJECTS.workflowVersion.universalIdentifier,
]);

export const buildNavigationConditionalAvailabilityExpression = ({
  universalIdentifier,
  nameSingular,
}: {
  universalIdentifier: string;
  nameSingular: string;
}): string => {
  if (HIDDEN_NAVIGATION_OBJECT_UNIVERSAL_IDENTIFIERS.has(universalIdentifier)) {
    return 'false';
  }

  return `targetObjectReadPermissions.${nameSingular}`;
};

export const buildObjectNavigationUniversalFlatCommandMenuItem = ({
  objectMetadata,
  applicationUniversalIdentifier,
  position,
  now,
}: {
  objectMetadata: {
    id: string;
    universalIdentifier: string;
    nameSingular: string;
    shortcut: string | null;
    isActive: boolean;
  };
  applicationUniversalIdentifier: string;
  position: number;
  now: string;
}): UniversalFlatCommandMenuItem => {
  const universalIdentifier =
    getSystemNavigationCommandMenuItemUniversalIdentifier({
      objectMetadataApplicationUniversalIdentifier:
        applicationUniversalIdentifier,
      objectUniversalIdentifier: objectMetadata.universalIdentifier,
    });

  const conditionalAvailabilityExpression =
    buildNavigationConditionalAvailabilityExpression({
      universalIdentifier: objectMetadata.universalIdentifier,
      nameSingular: objectMetadata.nameSingular,
    });

  return {
    universalIdentifier,
    applicationUniversalIdentifier,
    label: NAVIGATION_INTERPOLATED_LABEL,
    shortLabel: NAVIGATION_INTERPOLATED_SHORT_LABEL,
    icon: NAVIGATION_INTERPOLATED_ICON,
    position,
    isPinned: false,
    availabilityType: CommandMenuItemAvailabilityType.GLOBAL,
    conditionalAvailabilityExpression,
    conditionalPinnedExpression: null,
    frontComponentUniversalIdentifier: null,
    engineComponentKey: EngineComponentKey.NAVIGATION,
    payload: null,
    navigationTargetObjectMetadataUniversalIdentifier:
      objectMetadata.universalIdentifier,
    hotKeys: isDefined(objectMetadata.shortcut)
      ? ['G', objectMetadata.shortcut]
      : null,
    workflowVersionId: null,
    coreWorkflowVersionId: null,
    availabilityObjectMetadataUniversalIdentifier: null,
    pageLayoutUniversalIdentifier: null,
    isActive: objectMetadata.isActive,
    isSystemSideEffect: true,
    universalOverrides: null,
    createdAt: now,
    updatedAt: now,
  };
};