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

4.4 KB
Raw Blame
import { isNil, Permission } from '@activepieces/core-utils';
import { ApFlagId } from '@activepieces/shared';
import { Plus } from 'lucide-react';
import { ReactNode } from 'react';
import { Column, RenderCellProps } from 'react-data-grid';

import { useAuthorization } from '@/hooks/authorization-hooks';
import { flagsHooks } from '@/hooks/flags-hooks';

import { ClientRecordData } from '../stores/store/ap-tables-client-state';
import { Row } from '../types/types';

import { ApFieldHeader } from './ap-field-header';
import { useTableState } from './ap-table-state-provider';
import { EditableCell } from './editable-cell';
import { NewFieldPopup } from './new-field-popup';
import { SelectCell, SelectHeaderCell } from './select-column';

export function useTableColumns(createEmptyRecord: () => void) {
  const [fields, setSelectedAgentRunId] = useTableState((state) => [
    state.fields,
    state.setSelectedAgentRunId,
  ]);

  const { data: maxFields } = flagsHooks.useFlag<number>(
    ApFlagId.MAX_FIELDS_PER_TABLE,
  );

  const lockedByOtherUser = useTableState((state) => state.lockedByOtherUser);
  const userHasTableWritePermission = useAuthorization().checkAccess(
    Permission.WRITE_TABLE,
  );
  const canEdit = userHasTableWritePermission && !lockedByOtherUser;
  const isAllowedToCreateField =
    canEdit && maxFields && fields.length < maxFields;

  const newFieldColumn: Column<Row, { id: string }> = {
    key: 'new-field',
    minWidth: 67,
    maxWidth: 67,
    width: 67,
    name: '',
    renderHeaderCell: () => <AddFieldButton />,
    renderCell: () => <div className="empty-cell"></div>,
  };

  const columns: Column<Row, { id: string }>[] = [
    {
      key: 'select-row',
      name: 'Select',
      width: 66,
      minWidth: 66,
      maxWidth: 66,
      resizable: false,
      sortable: false,
      frozen: true,
      renderHeaderCell: () => <SelectHeaderCell />,
      renderCell: (props) => (
        <SelectCell
          row={props.row}
          rowIndex={props.rowIdx + 1}
          onClick={() => {
            if (props.row.locked && props.row.agentRunId) {
              setSelectedAgentRunId(props.row.agentRunId);
            }
          }}
        />
      ),
      renderSummaryCell: () => (
        <AddRecordButton
          handleClick={createEmptyRecord}
          icon={<Plus className="size-4" />}
        />
      ),
    },
    ...(fields.map((field, index) => ({
      key: field.uuid,
      minWidth: 207,
      width: 207,
      minHeight: 37,
      resizable: true,
      draggable: canEdit,
      name: '',
      renderHeaderCell: () => <ApFieldHeader field={{ ...field, index }} />,
      renderCell: ({
        row,
        column,
        rowIdx,
      }: RenderCellProps<Row, { id: string }>) => (
        <EditableCell
          key={row.id + '_' + field.uuid}
          field={field}
          value={row[field.uuid]}
          row={row}
          column={column}
          rowIdx={rowIdx}
          disabled={!canEdit}
          locked={row.locked}
          onClick={() => {
            if (row.locked && row.agentRunId) {
              setSelectedAgentRunId(row.agentRunId);
            }
          }}
        />
      ),
      renderSummaryCell: () => (
        <AddRecordButton handleClick={createEmptyRecord} />
      ),
    })) ?? []),
  ];

  if (isAllowedToCreateField) {
    columns.push(newFieldColumn);
  }
  return columns;
}

export function mapRecordsToRows(
  records: ClientRecordData[],
  fields: any[],
): Row[] {
  if (!records || records.length === 0) return [];
  return records.map((record: ClientRecordData) => {
    const row: Row = {
      id: record.uuid,
      agentRunId: record.agentRunId ?? null,
      locked: !isNil(record.agentRunId),
    };
    record.values.forEach((cell) => {
      const field = fields[cell.fieldIndex];
      if (field) {
        row[field.uuid] = cell.value;
      }
    });
    return row;
  });
}

type AddRecordButtonProps = {
  handleClick: () => void;
  icon?: ReactNode;
};

function AddRecordButton({ handleClick, icon }: AddRecordButtonProps) {
  return (
    <div
      className="w-full h-full border-t border-gray-6  flex items-center justify-start cursor-pointer pl-4"
      onClick={handleClick}
    >
      {icon}
    </div>
  );
}

function AddFieldButton() {
  return (
    <NewFieldPopup>
      <div className="w-full h-full flex items-center justify-center cursor-pointer">
        <Plus className="h-4 w-4" />
      </div>
    </NewFieldPopup>
  );
}