Skip to content

The open alternative to Salesforce, designed for AI.

4.0 KB
Raw Blame
import { styled } from '@linaria/react';

import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
import { getObjectColorWithFallback } from '@/object-metadata/utils/getObjectColorWithFallback';
import { isDefined } from 'twenty-shared/utils';
import { TintedIconTile } from 'twenty-ui/components/data-display';
import {
  IconEdit,
  IconPlus,
  IconSearch,
  IconTool,
  IconTrash,
  type IconComponent,
} from 'twenty-ui/icon';
import { useIcons } from 'twenty-ui/icon/provider';
import { Avatar } from 'twenty-ui/primitives/data-display';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';
import { type SettingsAgentToolApplication } from '~/pages/settings/ai/types/SettingsAgentToolApplication';
import { type SettingsAgentToolMarketplaceApp } from '~/pages/settings/ai/types/SettingsAgentToolMarketplaceApp';
import { getAbsoluteImageUrl } from '~/utils/image/getAbsoluteImageUrl';

type SettingsToolIconProps = {
  icon?: string | null;
  toolName?: string;
  objectName?: string;
  application?: ApplicationInfo;
  marketplaceApp?: MarketplaceAppInfo;
};

type ApplicationInfo = Pick<SettingsAgentToolApplication, 'name'>;

type MarketplaceAppInfo = Pick<SettingsAgentToolMarketplaceApp, 'logoUrl'>;

const getOperationIcon = (toolName: string): IconComponent | null => {
  if (toolName.startsWith('create_')) return IconPlus;
  if (toolName.startsWith('update_')) return IconEdit;
  if (toolName.startsWith('delete_')) return IconTrash;
  if (toolName.startsWith('find_') || toolName.startsWith('get_'))
    return IconSearch;

  return null;
};

const StyledCompositeContainer = styled.div`
  align-items: center;
  display: flex;
  flex-shrink: 0;
  height: 16px;
  justify-content: center;
  position: relative;
  width: 16px;
`;

const StyledOperationOverlay = styled.div`
  align-items: center;
  background-color: ${themeCssVariables.grayScale.gray4};
  border-radius: ${themeCssVariables.border.radius.sm};
  bottom: -5px;
  display: flex;
  height: 14px;
  justify-content: center;
  position: absolute;
  right: -6px;
  width: 12px;
`;

export const SettingsToolIcon = ({
  icon,
  toolName,
  objectName,
  application,
  marketplaceApp,
}: SettingsToolIconProps) => {
  const { getIcon } = useIcons();
  const theme = useTheme();
  const { objectMetadataItems } = useObjectMetadataItems();

  if (isDefined(application) && isDefined(marketplaceApp?.logoUrl)) {
    return (
      <Avatar
        src={getAbsoluteImageUrl(marketplaceApp.logoUrl)}
        name={application.name}
        colorSeed={application.name}
        shape="square"
        size="xs"
      />
    );
  }

  if (isDefined(application)) {
    return (
      <Avatar
        name={application.name}
        colorSeed={application.name}
        shape="square"
        size="xs"
      />
    );
  }

  const MainIcon = isDefined(icon) ? getIcon(icon) : IconTool;
  const OperationIcon = isDefined(toolName) ? getOperationIcon(toolName) : null;

  const objectMetadata = isDefined(objectName)
    ? objectMetadataItems.find((item) => item.nameSingular === objectName)
    : undefined;

  const objectColor = isDefined(objectMetadata)
    ? getObjectColorWithFallback(objectMetadata)
    : null;

  const objectIconTile = isDefined(objectColor) ? (
    <TintedIconTile
      color={objectColor}
      icon={<MainIcon size="14px" stroke={theme.icon.stroke.md} />}
    />
  ) : null;

  if (isDefined(OperationIcon)) {
    return (
      <StyledCompositeContainer>
        {objectIconTile ?? (
          <MainIcon
            size="14px"
            stroke={theme.icon.stroke.md}
            color={theme.font.color.secondary}
          />
        )}
        <StyledOperationOverlay>
          <OperationIcon
            size="12px"
            stroke={theme.icon.stroke.md}
            color={theme.font.color.tertiary}
          />
        </StyledOperationOverlay>
      </StyledCompositeContainer>
    );
  }

  if (isDefined(objectIconTile)) {
    return objectIconTile;
  }

  return (
    <MainIcon
      size={16}
      stroke={theme.icon.stroke.md}
      color={theme.font.color.tertiary}
    />
  );
};