Skip to content

The open alternative to Salesforce, designed for AI.

5.8 KB
Raw Blame
import { NavigationButton } from '@/ui/input/components/NavigationButton';

import { SettingsEmptyPlaceholder } from '@/settings/components/SettingsEmptyPlaceholder';
import { styled } from '@linaria/react';
import { type ComponentType } from 'react';
import { isDefined } from 'twenty-shared/utils';

import { Table } from '@/ui/layout/table/components/Table';
import { TableCell } from '@/ui/layout/table/components/TableCell';
import { TableHeader } from '@/ui/layout/table/components/TableHeader';
import { TableRow } from '@/ui/layout/table/components/TableRow';
import { TooltipDelay } from '@/ui/layout/tooltip/constants/TooltipDelay';
import { OverflowingTextWithTooltip } from 'twenty-ui/primitives/typography';
import {
  IconArrowUpRight,
  IconInfoCircle,
  IllustrationIconArray,
  IllustrationIconCalendarEvent,
  IllustrationIconCalendarTime,
  IllustrationIconJson,
  IllustrationIconNumbers,
  IllustrationIconText,
  IllustrationIconToggle,
} from 'twenty-ui/icon';
import { Tooltip } from 'twenty-ui/primitives/surfaces';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';
import { useLingui } from '@lingui/react/macro';

type SchemaProperty = {
  type?: string;
  description?: string;
  format?: string;
  items?: { type?: string };
};

export type SettingsToolParameterTableProps = {
  schemaProperties: Record<string, SchemaProperty>;
  requiredFields?: string[];
  functionLink?: string;
};

const PARAMETER_TABLE_GRID = '140px 1fr 80px 24px';

const TYPE_ICON_MAP: Record<string, ComponentType<{ size?: number }>> = {
  string: IllustrationIconText,
  date: IllustrationIconCalendarEvent,
  'date-time': IllustrationIconCalendarTime,
  number: IllustrationIconNumbers,
  integer: IllustrationIconNumbers,
  boolean: IllustrationIconToggle,
  array: IllustrationIconArray,
  object: IllustrationIconJson,
};

const StyledTableHeaderRow = styled.div`
  margin-bottom: ${themeCssVariables.spacing[2]};
`;

const StyledFieldsContainer = styled.div`
  border-bottom: 1px solid ${themeCssVariables.border.color.light};
  padding-bottom: ${themeCssVariables.spacing[2]};
`;

const StyledFooter = styled.div`
  display: flex;
  justify-content: flex-end;
  padding: ${themeCssVariables.spacing[2]} 0;
`;

const StyledInfoIconContainer = styled.div`
  align-items: center;
  display: flex;
  justify-content: center;
`;

const getDisplayType = (property: SchemaProperty): string => {
  if (property.format) {
    return property.format;
  }

  if (property.type === 'array' && property.items?.type) {
    return `${property.items.type}[]`;
  }

  return property.type ?? '';
};

const getTypeIconKey = (property: SchemaProperty): string =>
  isDefined(property.format) && isDefined(TYPE_ICON_MAP[property.format])
    ? property.format
    : (property.type ?? '');

export const SettingsToolParameterTable = ({
  schemaProperties,
  requiredFields,
  functionLink,
}: SettingsToolParameterTableProps) => {
  const { t } = useLingui();
  const theme = useTheme();
  const entries = Object.entries(schemaProperties);

  if (entries.length === 0 && !functionLink) {
    return (
      <SettingsEmptyPlaceholder>{t`No parameters`}</SettingsEmptyPlaceholder>
    );
  }

  return (
    <Table>
      {entries.length > 0 && (
        <>
          <StyledTableHeaderRow>
            <TableRow gridTemplateColumns={PARAMETER_TABLE_GRID}>
              <TableHeader>{t`Name`}</TableHeader>
              <TableHeader>{t`Type`}</TableHeader>
              <TableHeader>{t`Required`}</TableHeader>
              <TableHeader />
            </TableRow>
          </StyledTableHeaderRow>
          <StyledFieldsContainer>
            {entries.map(([paramName, property]) => {
              const displayType = getDisplayType(property);
              const TypeIcon = TYPE_ICON_MAP[getTypeIconKey(property)];

              return (
                <TableRow
                  key={paramName}
                  gridTemplateColumns={PARAMETER_TABLE_GRID}
                >
                  <TableCell
                    color={themeCssVariables.font.color.primary}
                    overflow="hidden"
                    textOverflow="ellipsis"
                    whiteSpace="nowrap"
                  >
                    <OverflowingTextWithTooltip text={paramName} />
                  </TableCell>
                  <TableCell gap={themeCssVariables.spacing[1]}>
                    {isDefined(TypeIcon) && (
                      <TypeIcon size={theme.icon.size.md} />
                    )}
                    {displayType}
                  </TableCell>
                  <TableCell color={themeCssVariables.font.color.tertiary}>
                    {requiredFields?.includes(paramName) ? t`Yes` : ''}
                  </TableCell>
                  <TableCell>
                    {property.description && (
                      <StyledInfoIconContainer>
                        <Tooltip
                          content={property.description}
                          sideOffset={5}
                          side="bottom"
                          positionMethod="fixed"
                          delay={TooltipDelay.shortDelay}
                        >
                          <IconInfoCircle
                            size={theme.icon.size.md}
                            color={theme.font.color.tertiary}
                          />
                        </Tooltip>
                      </StyledInfoIconContainer>
                    )}
                  </TableCell>
                </TableRow>
              );
            })}
          </StyledFieldsContainer>
        </>
      )}
      {functionLink && (
        <StyledFooter>
          <NavigationButton
            to={functionLink}
            startIcon={<IconArrowUpRight />}
            size="sm"
            variant="outline"
          >{t`See function`}</NavigationButton>
        </StyledFooter>
      )}
    </Table>
  );
};