1- import { useEffect , useMemo , useRef , useState } from 'react'
1+ import { useMemo , useRef , useState } from 'react'
22import { Badge , Input } from '@/components/emcn'
33import { Label } from '@/components/ui/label'
44import { cn } from '@/lib/core/utils/cn'
@@ -7,39 +7,7 @@ import { TagDropdown } from '@/app/workspace/[workspaceId]/w/[workflowId]/compon
77import { useSubBlockInput } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-sub-block-input'
88import { useSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-sub-block-value'
99import { useAccessibleReferencePrefixes } from '@/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-accessible-reference-prefixes'
10-
11- /**
12- * Represents a field in the input format configuration
13- */
14- interface InputFormatField {
15- name : string
16- type ?: string
17- }
18-
19- /**
20- * Represents an input trigger block structure
21- */
22- interface InputTriggerBlock {
23- type : 'input_trigger' | 'start_trigger'
24- subBlocks ?: {
25- inputFormat ?: { value ?: InputFormatField [ ] }
26- }
27- }
28-
29- /**
30- * Represents a legacy starter block structure
31- */
32- interface StarterBlockLegacy {
33- type : 'starter'
34- subBlocks ?: {
35- inputFormat ?: { value ?: InputFormatField [ ] }
36- }
37- config ?: {
38- params ?: {
39- inputFormat ?: InputFormatField [ ]
40- }
41- }
42- }
10+ import { useWorkflowInputFields } from '@/hooks/queries/workflows'
4311
4412/**
4513 * Props for the InputMappingField component
@@ -70,73 +38,6 @@ interface InputMappingProps {
7038 disabled ?: boolean
7139}
7240
73- /**
74- * Type guard to check if a value is an InputTriggerBlock
75- * @param value - The value to check
76- * @returns True if the value is an InputTriggerBlock
77- */
78- function isInputTriggerBlock ( value : unknown ) : value is InputTriggerBlock {
79- const type = ( value as { type ?: unknown } ) . type
80- return (
81- ! ! value && typeof value === 'object' && ( type === 'input_trigger' || type === 'start_trigger' )
82- )
83- }
84-
85- /**
86- * Type guard to check if a value is a StarterBlockLegacy
87- * @param value - The value to check
88- * @returns True if the value is a StarterBlockLegacy
89- */
90- function isStarterBlock ( value : unknown ) : value is StarterBlockLegacy {
91- return ! ! value && typeof value === 'object' && ( value as { type ?: unknown } ) . type === 'starter'
92- }
93-
94- /**
95- * Type guard to check if a value is an InputFormatField
96- * @param value - The value to check
97- * @returns True if the value is an InputFormatField
98- */
99- function isInputFormatField ( value : unknown ) : value is InputFormatField {
100- if ( typeof value !== 'object' || value === null ) return false
101- if ( ! ( 'name' in value ) ) return false
102- const { name, type } = value as { name : unknown ; type ?: unknown }
103- if ( typeof name !== 'string' || name . trim ( ) === '' ) return false
104- if ( type !== undefined && typeof type !== 'string' ) return false
105- return true
106- }
107-
108- /**
109- * Extracts input format fields from workflow blocks
110- * @param blocks - The workflow blocks to extract from
111- * @returns Array of input format fields or null if not found
112- */
113- function extractInputFormatFields ( blocks : Record < string , unknown > ) : InputFormatField [ ] | null {
114- const triggerEntry = Object . entries ( blocks ) . find ( ( [ , b ] ) => isInputTriggerBlock ( b ) )
115- if ( triggerEntry && isInputTriggerBlock ( triggerEntry [ 1 ] ) ) {
116- const inputFormat = triggerEntry [ 1 ] . subBlocks ?. inputFormat ?. value
117- if ( Array . isArray ( inputFormat ) ) {
118- return ( inputFormat as unknown [ ] )
119- . filter ( isInputFormatField )
120- . map ( ( f ) => ( { name : f . name , type : f . type } ) )
121- }
122- }
123-
124- const starterEntry = Object . entries ( blocks ) . find ( ( [ , b ] ) => isStarterBlock ( b ) )
125- if ( starterEntry && isStarterBlock ( starterEntry [ 1 ] ) ) {
126- const starter = starterEntry [ 1 ]
127- const subBlockFormat = starter . subBlocks ?. inputFormat ?. value
128- const legacyParamsFormat = starter . config ?. params ?. inputFormat
129- const chosen = Array . isArray ( subBlockFormat ) ? subBlockFormat : legacyParamsFormat
130- if ( Array . isArray ( chosen ) ) {
131- return ( chosen as unknown [ ] )
132- . filter ( isInputFormatField )
133- . map ( ( f ) => ( { name : f . name , type : f . type } ) )
134- }
135- }
136-
137- return null
138- }
139-
14041/**
14142 * InputMapping component displays and manages input field mappings for workflow execution
14243 * @param props - The component props
@@ -168,62 +69,10 @@ export function InputMapping({
16869 const inputRefs = useRef < Map < string , HTMLInputElement > > ( new Map ( ) )
16970 const overlayRefs = useRef < Map < string , HTMLDivElement > > ( new Map ( ) )
17071
171- const [ childInputFields , setChildInputFields ] = useState < InputFormatField [ ] > ( [ ] )
172- const [ isLoading , setIsLoading ] = useState ( false )
72+ const workflowId = typeof selectedWorkflowId === 'string' ? selectedWorkflowId : undefined
73+ const { data : childInputFields = [ ] , isLoading } = useWorkflowInputFields ( workflowId )
17374 const [ collapsedFields , setCollapsedFields ] = useState < Record < string , boolean > > ( { } )
17475
175- useEffect ( ( ) => {
176- let isMounted = true
177- const controller = new AbortController ( )
178-
179- async function fetchChildSchema ( ) {
180- if ( ! selectedWorkflowId ) {
181- if ( isMounted ) {
182- setChildInputFields ( [ ] )
183- setIsLoading ( false )
184- }
185- return
186- }
187-
188- try {
189- if ( isMounted ) setIsLoading ( true )
190-
191- const res = await fetch ( `/api/workflows/${ selectedWorkflowId } ` , {
192- signal : controller . signal ,
193- } )
194-
195- if ( ! res . ok ) {
196- if ( isMounted ) {
197- setChildInputFields ( [ ] )
198- setIsLoading ( false )
199- }
200- return
201- }
202-
203- const { data } = await res . json ( )
204- const blocks = ( data ?. state ?. blocks as Record < string , unknown > ) || { }
205- const fields = extractInputFormatFields ( blocks )
206-
207- if ( isMounted ) {
208- setChildInputFields ( fields || [ ] )
209- setIsLoading ( false )
210- }
211- } catch ( error ) {
212- if ( isMounted ) {
213- setChildInputFields ( [ ] )
214- setIsLoading ( false )
215- }
216- }
217- }
218-
219- fetchChildSchema ( )
220-
221- return ( ) => {
222- isMounted = false
223- controller . abort ( )
224- }
225- } , [ selectedWorkflowId ] )
226-
22776 const valueObj : Record < string , string > = useMemo ( ( ) => {
22877 if ( isPreview && previewValue && typeof previewValue === 'object' ) {
22978 return previewValue as Record < string , string >
0 commit comments