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.7 KB
Raw Blame
import { HttpMethod } from '@activepieces/pieces-common';
import { Property } from '@activepieces/pieces-framework';
import { hedyAuth } from '../auth';
import { createClient } from './client';
import { PaginatedResponse, Topic } from './types';

function toTopicArray(result: unknown): Topic[] {
  if (Array.isArray(result)) {
    return result as Topic[];
  }

  if (result && typeof result === 'object' && 'data' in result) {
    const data = (result as PaginatedResponse<Topic>).data;
    if (Array.isArray(data)) {
      return data;
    }
  }

  return [];
}

export const topicDropdown = Property.Dropdown({
  auth: hedyAuth,
  displayName: 'Topic',
  description: 'Optionally filter results by a specific topic.',
  required: false,
  refreshers: [],
  options: async ({ auth }) => {
    if (!auth) {
      return {
        disabled: true,
        options: [],
        placeholder: 'Connect your Hedy account first.',
      };
    }

    const client = createClient(auth);
    try {
      const response = await client.request<Topic[]>({
        method: HttpMethod.GET,
        path: '/topics',
      });

      const topics = toTopicArray(response);

      if (topics.length === 0) {
        return {
          disabled: false,
          options: [],
          placeholder: 'No topics found in your Hedy workspace.',
        };
      }

      return {
        disabled: false,
        options: topics.map((topic) => ({
          label: topic.name,
          value: topic.id,
        })),
      };
    } catch (error) {
      return {
        disabled: true,
        options: [],
        placeholder:
          error instanceof Error ? error.message : 'Failed to load topics. Check your connection.',
      };
    }
  },
});