import type { TriggerDefinition } from "../types" import * as z from "zod/mini" import { FORM_FIELDS_SCHEMA, type FormField } from "../form-fields" export const DASHBOARD_RUN_CONFIG_SCHEMA = z.object({ description: z.optional(z.string().check(z.maxLength(2_000))), fields: z.prefault(FORM_FIELDS_SCHEMA, []), submitLabel: z.prefault( z.string().check(z.trim(), z.minLength(1), z.maxLength(100)), "Run", ), title: z.string().check(z.trim(), z.minLength(1), z.maxLength(255)), }) export const dashboardRunTriggerDefinition = { getDetails: ({ config }) => [ { label: "Run form", value: DASHBOARD_RUN_CONFIG_SCHEMA.parse(config).title, }, ], icon: "automation", name: "Dashboard run", type: "dashboard.run", } as const satisfies TriggerDefinition export interface DashboardRunConfig< TFields extends readonly FormField[] = readonly FormField[], > { /** Supporting text shown below the form title. */ description?: string /** Ordered fields. Omit to run immediately. */ fields?: TFields /** Submit button text. Defaults to "Run". */ submitLabel?: string /** Form heading. */ title: string } export interface DashboardRunEndpointOptions { appOrigin: string automationId: string hookSlot: number scopePath?: readonly number[] } /** * Returns the organization-authenticated submission endpoint for one dashboard * run. * * @param options - Public origin and trigger identity. */ export function getDashboardRunEndpoint(options: DashboardRunEndpointOptions) { return new URL( `/api/dashboard-runs/${getDashboardRunEndpointKey(options)}`, options.appOrigin, ).toString() } /** * Returns the durable endpoint identity for one dashboard run trigger. * * @param options - Trigger and owning automation identity. */ export function getDashboardRunEndpointKey( options: Omit, ) { return `${options.automationId}:${[ ...(options.scopePath ?? []), options.hookSlot, ].join(".")}` }