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.8 KB
Raw Blame
import { Property } from '@activepieces/pieces-framework';
import { todoistRestClient } from './client/rest-client';
import { todoistAuth } from '../..';

const buildEmptyList = ({ placeholder }: { placeholder: string }) => {
	return {
		disabled: true,
		options: [],
		placeholder,
	};
};

export const todoistProjectIdDropdown = (description: string) =>
	Property.Dropdown<string,false,typeof todoistAuth>({
		auth: todoistAuth,
		displayName: 'Project',
		refreshers: [],
		description,
		required: false,
		options: async ({ auth }) => {
			if (!auth) {
				return buildEmptyList({
					placeholder: 'Connect your Todoist account first',
				});
			}

			const token = auth.access_token;
			const projects = await todoistRestClient.projects.list({ token });
			if (projects.length === 0) {
				return buildEmptyList({
					placeholder: 'No projects found. Create one in Todoist first.',
				});
			}

			const options = projects.map((p) => ({
				label: p.name,
				value: p.id,
			}));

			return {
				disabled: false,
				options,
			};
		},
	});

export const todoistSectionIdDropdown = Property.Dropdown({
	auth: todoistAuth,
	displayName: 'Section',
	description: 'Leave empty to add the task outside any section.',
	refreshers: ['project_id'],
	required: false,
	options: async ({ auth, project_id }) => {
		if (!auth) {
			return buildEmptyList({
				placeholder: 'Connect your Todoist account first',
			});
		}

		const token = auth.access_token;
		const projectId = project_id as string | undefined;
		const sections = await todoistRestClient.sections.list({ token, project_id: projectId });

		if (sections.length === 0) {
			return buildEmptyList({
				placeholder: 'No sections found. Create one in Todoist first.',
			});
		}

		const options = sections.map((p) => ({
			label: p.name,
			value: p.id,
		}));

		return {
			disabled: false,
			options,
		};
	},
});