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

3.7 KB
Raw Blame
import { isNil, isObject } from '@activepieces/core-utils';
import { t } from 'i18next';
import { ChevronDown, ChevronRight } from 'lucide-react';
import { useState } from 'react';

import { StepFileDownloadButton } from '@/components/custom/step-file-download-button';
import { VirtualizedList } from '@/components/ui/virtualized-list';
import { isStepFileUrl } from '@/lib/dom-utils';

import { FieldTypeIcon } from './field-type-icon';
import { previewObject } from './object-preview';

const MAX_NESTED_DEPTH = 10;

function truncateValue(value: unknown): string {
  if (isNil(value) || value === '') return '';
  if (Array.isArray(value)) return t('itemCount', { count: value.length });
  if (isObject(value)) {
    return previewObject(value as Record<string, unknown>);
  }
  return String(value);
}

function ValueRow({ label, value, depth }: ValueRowProps) {
  const [expanded, setExpanded] = useState(false);
  const paddingLeft = 16 + depth * 24;

  if (isObject(value) || (Array.isArray(value) && value.length > 0)) {
    if (depth >= MAX_NESTED_DEPTH) {
      return (
        <div
          className="py-1.5 text-xs text-gray-11 italic"
          style={{ paddingLeft, paddingRight: 16 }}
        >
          {label}: {t('Too deep to display')}
        </div>
      );
    }

    const nestedEntries: Array<readonly [string, unknown]> = Array.isArray(
      value,
    )
      ? value.map((item, idx) => [`${t('Item')} ${idx + 1}`, item] as const)
      : Object.entries(value);

    return (
      <div>
        <button
          type="button"
          onClick={() => setExpanded(!expanded)}
          className="flex items-center gap-3 py-1.5 hover:bg-gray-4/50 cursor-pointer w-full text-left"
          style={{ paddingLeft, paddingRight: 16 }}
        >
          <div className="shrink-0 w-4 h-4 flex items-center justify-center text-gray-11">
            {expanded ? (
              <ChevronDown className="h-3.5 w-3.5" />
            ) : (
              <ChevronRight className="h-3.5 w-3.5" />
            )}
          </div>
          <FieldTypeIcon value={value} />
          <span className="text-sm text-gray-11 min-w-[100px] max-w-[160px] shrink-0 truncate">
            {label}
          </span>
          <span
            className="text-sm text-gray-12/50 truncate flex-1 min-w-0"
            title={JSON.stringify(value)}
          >
            {truncateValue(value)}
          </span>
        </button>
        {expanded && (
          <div>
            <VirtualizedList
              items={nestedEntries}
              estimateSize={30}
              getItemKey={(index) => nestedEntries[index][0]}
              renderItem={([key, childValue]) => (
                <ValueRow label={key} value={childValue} depth={depth + 1} />
              )}
            />
          </div>
        )}
      </div>
    );
  }

  return (
    <div
      className="flex items-start gap-3 py-1.5 hover:bg-gray-4/50"
      style={{ paddingLeft: paddingLeft + 24, paddingRight: 16 }}
    >
      <span className="flex h-5 items-center shrink-0">
        <FieldTypeIcon value={value} />
      </span>
      <span className="text-sm text-gray-11 min-w-[100px] max-w-[160px] shrink-0 truncate">
        {label}
      </span>
      <span className="text-sm text-gray-12/70 flex-1 min-w-0 break-words whitespace-pre-wrap">
        {isStepFileUrl(value) ? (
          <StepFileDownloadButton fileUrl={value} />
        ) : isNil(value) || value === '' ? (
          <span className="text-gray-11 italic">{t('empty')}</span>
        ) : Array.isArray(value) ? (
          t('itemCount', { count: value.length })
        ) : (
          String(value)
        )}
      </span>
    </div>
  );
}

export { ValueRow, truncateValue };

type ValueRowProps = {
  label: string;
  value: unknown;
  depth: number;
};