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

1.6 KB
Raw Blame
import { CustomProperty as CustomPropertyType } from '@activepieces/pieces-framework';
import { useEffect, useId } from 'react';

import { useEmbedding } from '@/components/providers/embed-provider';
import { projectCollectionUtils } from '@/features/projects';
const CUSTOM_PROPERTY_CONTAINER_ID = 'custom-property-container';

type CustomPropertyParams = {
  value: unknown;
  onChange: (value: unknown) => void;
  code: string;
  disabled: boolean;
  property: CustomPropertyType<boolean>;
};

const parseFunctionString = (code: string) => {
  return new Function(
    'params',
    `
    return (${code})(params);
  `,
  );
};
const CustomProperty = ({
  value,
  onChange,
  code,
  disabled,
  property,
}: CustomPropertyParams) => {
  const { project } = projectCollectionUtils.useCurrentProject();
  const { embedState } = useEmbedding();
  const id = useId();
  const containerId = CUSTOM_PROPERTY_CONTAINER_ID + '-' + id;
  useEffect(() => {
    try {
      const params = {
        containerId,
        value,
        onChange,
        isEmbedded: embedState.isEmbedded,
        projectId: project.id,
        disabled,
        property,
      };
      // Create function that takes a params object
      const fn = parseFunctionString(code);
      // Execute the function with args as the params object
      const cleanUpFunction = fn(params);
      if (cleanUpFunction && typeof cleanUpFunction === 'function') {
        return cleanUpFunction;
      }
    } catch (error) {
      console.error('Error executing custom code:', error);
    }
  }, []);
  return <div id={containerId}></div>;
};

CustomProperty.displayName = 'CustomProperty';
export default CustomProperty;