Skip to content

The open alternative to Salesforce, designed for AI.

3.5 KB
Raw Blame
import { styled } from '@linaria/react';
import { useLingui } from '@lingui/react/macro';
import { isDefined } from 'twenty-shared/utils';
import { ListItem } from 'twenty-ui/primitives/navigation';
import { useIcons } from 'twenty-ui/icon/provider';
import { themeCssVariables } from 'twenty-ui/theme';

import { SettingsValidationRuleHelperDetails } from '@/validation-rules/components/SettingsValidationRuleHelperDetails';
import { type ValidationRuleEditorField } from '@/validation-rules/types/ValidationRuleEditorField';
import { type ValidationRuleHelperItem } from '@/validation-rules/types/ValidationRuleHelperItem';
import { getValidationRuleHelperItemIcon } from '@/validation-rules/utils/getValidationRuleHelperItemIcon';

const StyledPanel = styled.div`
  display: grid;
  font-size: ${themeCssVariables.font.size.md};
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  height: 240px;
  overflow: hidden;
`;

const StyledList = styled.div`
  border-right: 1px solid ${themeCssVariables.border.color.medium};
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: ${themeCssVariables.spacing[1]} ${themeCssVariables.spacing[4]};
`;

const StyledEmpty = styled.div`
  color: ${themeCssVariables.font.color.light};
  padding: ${themeCssVariables.spacing[2]};
`;

const getItemKey = (item: ValidationRuleHelperItem) =>
  item.kind === 'field'
    ? `field:${item.field.path}`
    : `${item.kind}:${item.definition.name}`;

type SettingsValidationRuleHelperPanelProps = {
  items: ValidationRuleHelperItem[];
  highlightedIndex: number;
  isFocused: boolean;
  editorFields: ValidationRuleEditorField[];
  onHighlight: (index: number) => void;
  onSelect: (item: ValidationRuleHelperItem) => void;
};

export const SettingsValidationRuleHelperPanel = ({
  items,
  highlightedIndex,
  isFocused,
  editorFields,
  onHighlight,
  onSelect,
}: SettingsValidationRuleHelperPanelProps) => {
  const { t } = useLingui();
  const { getIcon } = useIcons();

  const highlightedItem = items[highlightedIndex];
  const EnterHintIcon = getIcon('IconArrowBack');

  return (
    <StyledPanel>
      <StyledList role="listbox" aria-label={t`Suggestions`}>
        {items.length === 0 && (
          <StyledEmpty>{t`Nothing to suggest here.`}</StyledEmpty>
        )}
        {items.map((item, index) => {
          const isHighlighted = index === highlightedIndex;
          const isFocusedItem = isFocused && isHighlighted;
          const ItemIcon = getValidationRuleHelperItemIcon({ item, getIcon });

          return (
            <ListItem
              key={getItemKey(item)}
              role="option"
              aria-selected={isFocusedItem}
              ref={
                isHighlighted
                  ? (element: HTMLElement | null) =>
                      element?.scrollIntoView({ block: 'nearest' })
                  : undefined
              }
              onMouseDown={(event) => event.preventDefault()}
              startIcon={<ItemIcon />}
              endIcon={isFocusedItem ? <EnterHintIcon /> : undefined}
              focused={isFocusedItem}
              onMouseEnter={() => onHighlight(index)}
              onClick={() => onSelect(item)}
            >
              {item.kind === 'field' ? item.field.label : item.definition.name}
            </ListItem>
          );
        })}
      </StyledList>
      {isDefined(highlightedItem) ? (
        <SettingsValidationRuleHelperDetails
          item={highlightedItem}
          editorFields={editorFields}
        />
      ) : (
        <div />
      )}
    </StyledPanel>
  );
};