Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

7.2 KB
Raw Blame
import {
  FlowAction,
  FlowTrigger,
  flowStructureUtil,
} from '@activepieces/shared';
import { t } from 'i18next';
import { ChevronDown, ChevronRight } from 'lucide-react';

import { FieldTypeIcon } from '@/components/custom/smart-output-viewer/field-type-icon';
import { TextWithTooltip } from '@/components/custom/text-with-tooltip';
import { useApRipple } from '@/components/providers/theme-provider';
import { Button } from '@/components/ui/button';
import { PieceIcon, stepsHooks } from '@/features/pieces';
import { cn } from '@/lib/utils';

import { useBuilderStateContext } from '../builder-hooks';

import { DataSelectorTreeNode } from './type';

const INDENT_PER_DEPTH = 14;
const VALUE_PREVIEW_MAX_LENGTH = 60;

type DataSelectorNodeContentProps = {
  expanded: boolean;
  setExpanded: (expanded: boolean) => void;
  depth: number;
  node: DataSelectorTreeNode;
};

const handleKeyPress = (event: React.KeyboardEvent<HTMLDivElement>) => {
  if (event.key === 'Enter' || event.key === ' ') {
    event.preventDefault();
    if (event.target) {
      (event.target as HTMLDivElement).click();
    }
  }
};

const DataSelectorNodeContent = ({
  node,
  expanded,
  setExpanded,
  depth,
}: DataSelectorNodeContentProps) => {
  const flowVersion = useBuilderStateContext((state) => state.flowVersion);
  const insertMention = useBuilderStateContext((state) => state.insertMention);

  const [ripple, rippleEvent] = useApRipple();

  const rootStepName =
    depth === 0 && (node.data.type === 'value' || node.data.type === 'test')
      ? node.data.stepName
      : undefined;
  const stepForRoot = rootStepName
    ? flowStructureUtil.getStep(rootStepName, flowVersion.trigger)
    : undefined;

  const isExpandable = !!node.children && node.children.length > 0;
  const isStepRoot = depth === 0;
  const isPrimitiveStepRoot = isStepRoot && !isExpandable;
  const isLeafValue =
    !isExpandable && node.data.type === 'value' && !isStepRoot;
  const isInsertable =
    node.data.type === 'value' && node.data.insertable && !node.isLoopStepNode;
  const showInsertButton = isInsertable;

  const arrayValue =
    node.data.type === 'value' && Array.isArray(node.data.value)
      ? (node.data.value as unknown[])
      : null;
  const showArrayCount = isExpandable && arrayValue !== null;

  const handleClick = (e: React.MouseEvent) => {
    if (isExpandable) {
      rippleEvent(e);
      setExpanded(!expanded);
      return;
    }
    if (isInsertable && insertMention && node.data.type === 'value') {
      rippleEvent(e);
      insertMention(node.data.propertyPath);
    }
  };

  const showValuePreview = (isLeafValue || isPrimitiveStepRoot) && isInsertable;
  const valuePreview =
    showValuePreview && node.data.type === 'value'
      ? formatValuePreview(node.data.value)
      : '';

  return (
    <div
      tabIndex={0}
      onKeyDown={handleKeyPress}
      ref={ripple}
      onClick={handleClick}
      aria-expanded={isExpandable ? expanded : undefined}
      className={cn(
        'w-full max-w-full relative select-none focus:outline-hidden cursor-pointer group transition-colors',
        'hover:bg-gray-4/60 focus:bg-gray-4',
      )}
      data-depth={depth}
    >
      <div
        className={cn(
          'flex items-center gap-1.5 pr-2 min-w-0',
          isStepRoot ? 'min-h-[40px] py-1.5' : 'min-h-[32px]',
        )}
        style={{ paddingLeft: depth * INDENT_PER_DEPTH + 12 }}
      >
        {!isStepRoot && isExpandable && (
          <ChevronRight
            className={cn(
              'size-3.5 shrink-0 text-gray-11 transition-transform',
              expanded && 'rotate-90',
            )}
          />
        )}
        {!isStepRoot && !isExpandable && (
          <div className="size-3.5 shrink-0" aria-hidden />
        )}

        {isStepRoot && stepForRoot && <StepRootIcon step={stepForRoot} />}

        <div className="flex items-center gap-1.5 min-w-0 flex-1">
          {!isStepRoot && node.data.type === 'value' && (
            <FieldTypeIcon value={node.data.value} format={node.data.format} />
          )}

          {node.data.type !== 'test' && (
            <span
              className={cn(
                'truncate min-w-0 shrink-0 max-w-[30ch]',
                isStepRoot
                  ? 'font-medium text-gray-12 text-sm'
                  : 'text-gray-12 text-sm',
                node.data.displayNameClassName,
              )}
            >
              {node.data.displayName}
            </span>
          )}

          {showArrayCount && (
            <span className="shrink-0 text-xs text-gray-11">
              {t('{count, plural, =1 {1 item} other {# items}}', {
                count: arrayValue?.length ?? 0,
              })}
            </span>
          )}

          {showValuePreview && valuePreview !== '' && (
            <>
              <span className="shrink-0 text-gray-11">:</span>
              <TextWithTooltip tooltipMessage={String(valuePreview)}>
                <span className="min-w-0 truncate text-accent-11 text-sm flex-1">
                  {valuePreview}
                </span>
              </TextWithTooltip>
            </>
          )}
        </div>

        {showInsertButton && (
          <Button
            variant="basic"
            size="xs"
            tabIndex={-1}
            onClick={(e) => {
              e.stopPropagation();
              if (insertMention && node.data.type === 'value') {
                insertMention(node.data.propertyPath);
              }
            }}
            className={cn(
              'opacity-0 transition-opacity',
              'group-hover:opacity-100 focus-visible:opacity-100',
            )}
          >
            {t('Insert')}
          </Button>
        )}

        {isStepRoot && isExpandable && (
          <ChevronDown
            className={cn(
              'size-4 shrink-0 text-gray-11 transition-transform',
              !expanded && '-rotate-90',
            )}
          />
        )}
      </div>
    </div>
  );
};

const StepRootIcon = ({ step }: { step: FlowAction | FlowTrigger }) => {
  const { stepMetadata } = stepsHooks.useStepMetadata({ step });
  if (!stepMetadata) return null;
  return (
    <div className="shrink-0">
      <PieceIcon
        displayName={stepMetadata.displayName}
        logoUrl={stepMetadata.logoUrl}
        showTooltip={false}
        border={false}
        size="xs"
      />
    </div>
  );
};

const formatValuePreview = (value: unknown): string => {
  if (value === null || value === undefined) return '—';
  if (typeof value === 'string') {
    const trimmed = value.replace(/\s+/g, ' ').trim();
    if (trimmed === '') return '—';
    return trimmed.length > VALUE_PREVIEW_MAX_LENGTH
      ? `${trimmed.slice(0, VALUE_PREVIEW_MAX_LENGTH)}…`
      : trimmed;
  }
  if (typeof value === 'number' || typeof value === 'boolean') {
    return String(value);
  }
  if (Array.isArray(value) && value.length === 0) return t('Empty List');
  if (
    typeof value === 'object' &&
    value !== null &&
    !Array.isArray(value) &&
    Object.keys(value).length === 0
  ) {
    return t('Empty Object');
  }
  const json = JSON.stringify(value);
  return json.length > VALUE_PREVIEW_MAX_LENGTH
    ? `${json.slice(0, VALUE_PREVIEW_MAX_LENGTH)}…`
    : json;
};

DataSelectorNodeContent.displayName = 'DataSelectorNodeContent';
export { DataSelectorNodeContent };