|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { ChevronUp } from 'lucide-react' |
| 4 | +import SimpleCodeEditor from 'react-simple-code-editor' |
| 5 | +import { Code as CodeEditor, Combobox, getCodeEditorProps, Input, Label } from '@/components/emcn' |
| 6 | +import { TagDropdown } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel-new/components/editor/components/sub-block/components/tag-dropdown' |
| 7 | +import type { BlockState } from '@/stores/workflows/workflow/types' |
| 8 | +import type { ConnectedBlock } from '../../hooks/use-block-connections' |
| 9 | +import { useSubflowEditor } from '../../hooks/use-subflow-editor' |
| 10 | +import { ConnectionBlocks } from '../connection-blocks' |
| 11 | + |
| 12 | +interface SubflowEditorProps { |
| 13 | + currentBlock: BlockState |
| 14 | + currentBlockId: string |
| 15 | + subBlocksRef: React.RefObject<HTMLDivElement | null> |
| 16 | + connectionsHeight: number |
| 17 | + isResizing: boolean |
| 18 | + hasIncomingConnections: boolean |
| 19 | + incomingConnections: ConnectedBlock[] |
| 20 | + handleConnectionsResizeMouseDown: (e: React.MouseEvent) => void |
| 21 | + toggleConnectionsCollapsed: () => void |
| 22 | + userCanEdit: boolean |
| 23 | + isConnectionsAtMinHeight: boolean |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * SubflowEditor component for editing loop and parallel blocks |
| 28 | + * |
| 29 | + * @param props - Component props |
| 30 | + * @returns Rendered subflow editor |
| 31 | + */ |
| 32 | +export function SubflowEditor({ |
| 33 | + currentBlock, |
| 34 | + currentBlockId, |
| 35 | + subBlocksRef, |
| 36 | + connectionsHeight, |
| 37 | + isResizing, |
| 38 | + hasIncomingConnections, |
| 39 | + incomingConnections, |
| 40 | + handleConnectionsResizeMouseDown, |
| 41 | + toggleConnectionsCollapsed, |
| 42 | + userCanEdit, |
| 43 | + isConnectionsAtMinHeight, |
| 44 | +}: SubflowEditorProps) { |
| 45 | + const { |
| 46 | + subflowConfig, |
| 47 | + currentType, |
| 48 | + isCountMode, |
| 49 | + isConditionMode, |
| 50 | + inputValue, |
| 51 | + editorValue, |
| 52 | + typeOptions, |
| 53 | + showTagDropdown, |
| 54 | + cursorPosition, |
| 55 | + editorContainerRef, |
| 56 | + handleSubflowTypeChange, |
| 57 | + handleSubflowIterationsChange, |
| 58 | + handleSubflowIterationsSave, |
| 59 | + handleSubflowEditorChange, |
| 60 | + handleSubflowTagSelect, |
| 61 | + highlightWithReferences, |
| 62 | + setShowTagDropdown, |
| 63 | + } = useSubflowEditor(currentBlock, currentBlockId) |
| 64 | + |
| 65 | + if (!subflowConfig) return null |
| 66 | + |
| 67 | + return ( |
| 68 | + <div className='flex flex-1 flex-col overflow-hidden pt-[0px]'> |
| 69 | + {/* Subflow Editor Section */} |
| 70 | + <div ref={subBlocksRef} className='subblocks-section flex flex-1 flex-col overflow-hidden'> |
| 71 | + <div className='flex-1 overflow-y-auto overflow-x-hidden px-[8px] pt-[5px] pb-[8px]'> |
| 72 | + {/* Type Selection */} |
| 73 | + <div> |
| 74 | + <Label className='mb-[6.5px] block pl-[2px] font-medium text-[#E6E6E6] text-[13px] dark:text-[#E6E6E6]'> |
| 75 | + {currentBlock.type === 'loop' ? 'Loop Type' : 'Parallel Type'} |
| 76 | + </Label> |
| 77 | + <Combobox |
| 78 | + options={typeOptions} |
| 79 | + value={currentType || ''} |
| 80 | + onChange={handleSubflowTypeChange} |
| 81 | + disabled={!userCanEdit} |
| 82 | + placeholder='Select type...' |
| 83 | + /> |
| 84 | + </div> |
| 85 | + |
| 86 | + {/* Dashed Line Separator */} |
| 87 | + <div className='px-[2px] pt-[16px] pb-[10px]'> |
| 88 | + <div |
| 89 | + className='h-[1.25px]' |
| 90 | + style={{ |
| 91 | + backgroundImage: |
| 92 | + 'repeating-linear-gradient(to right, #2C2C2C 0px, #2C2C2C 6px, transparent 6px, transparent 12px)', |
| 93 | + }} |
| 94 | + /> |
| 95 | + </div> |
| 96 | + |
| 97 | + {/* Configuration */} |
| 98 | + <div> |
| 99 | + <Label className='mb-[6.5px] block pl-[2px] font-medium text-[#E6E6E6] text-[13px] dark:text-[#E6E6E6]'> |
| 100 | + {isCountMode |
| 101 | + ? `${currentBlock.type === 'loop' ? 'Loop' : 'Parallel'} Iterations` |
| 102 | + : isConditionMode |
| 103 | + ? 'While Condition' |
| 104 | + : `${currentBlock.type === 'loop' ? 'Collection' : 'Parallel'} Items`} |
| 105 | + </Label> |
| 106 | + |
| 107 | + {isCountMode ? ( |
| 108 | + <div> |
| 109 | + <Input |
| 110 | + type='text' |
| 111 | + value={inputValue} |
| 112 | + onChange={handleSubflowIterationsChange} |
| 113 | + onBlur={handleSubflowIterationsSave} |
| 114 | + onKeyDown={(e) => e.key === 'Enter' && handleSubflowIterationsSave()} |
| 115 | + disabled={!userCanEdit} |
| 116 | + className='mb-[4px]' |
| 117 | + /> |
| 118 | + <div className='text-[10px] text-muted-foreground'> |
| 119 | + Enter a number between 1 and {subflowConfig.maxIterations} |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + ) : ( |
| 123 | + <div ref={editorContainerRef} className='relative'> |
| 124 | + <CodeEditor.Container> |
| 125 | + <CodeEditor.Content> |
| 126 | + <CodeEditor.Placeholder gutterWidth={0} show={editorValue.length === 0}> |
| 127 | + {isConditionMode ? '<counter.value> < 10' : "['item1', 'item2', 'item3']"} |
| 128 | + </CodeEditor.Placeholder> |
| 129 | + |
| 130 | + <SimpleCodeEditor |
| 131 | + value={editorValue} |
| 132 | + onValueChange={handleSubflowEditorChange} |
| 133 | + highlight={highlightWithReferences} |
| 134 | + {...getCodeEditorProps({ |
| 135 | + isPreview: false, |
| 136 | + disabled: !userCanEdit, |
| 137 | + })} |
| 138 | + /> |
| 139 | + |
| 140 | + {showTagDropdown && ( |
| 141 | + <TagDropdown |
| 142 | + visible={showTagDropdown} |
| 143 | + onSelect={handleSubflowTagSelect} |
| 144 | + blockId={currentBlockId} |
| 145 | + activeSourceBlockId={null} |
| 146 | + inputValue={editorValue} |
| 147 | + cursorPosition={cursorPosition} |
| 148 | + onClose={() => setShowTagDropdown(false)} |
| 149 | + inputRef={{ |
| 150 | + current: editorContainerRef.current?.querySelector( |
| 151 | + 'textarea' |
| 152 | + ) as HTMLTextAreaElement, |
| 153 | + }} |
| 154 | + /> |
| 155 | + )} |
| 156 | + </CodeEditor.Content> |
| 157 | + </CodeEditor.Container> |
| 158 | + </div> |
| 159 | + )} |
| 160 | + </div> |
| 161 | + </div> |
| 162 | + </div> |
| 163 | + |
| 164 | + {/* Connections Section - Only show when there are connections */} |
| 165 | + {hasIncomingConnections && ( |
| 166 | + <div |
| 167 | + className={ |
| 168 | + 'connections-section flex flex-shrink-0 flex-col overflow-hidden border-[#2C2C2C] border-t dark:border-[#2C2C2C]' + |
| 169 | + (!isResizing ? ' transition-[height] duration-100 ease-out' : '') |
| 170 | + } |
| 171 | + style={{ height: `${connectionsHeight}px` }} |
| 172 | + > |
| 173 | + {/* Resize Handle */} |
| 174 | + <div className='relative'> |
| 175 | + <div |
| 176 | + className='absolute top-[-4px] right-0 left-0 z-30 h-[8px] cursor-ns-resize' |
| 177 | + onMouseDown={handleConnectionsResizeMouseDown} |
| 178 | + /> |
| 179 | + </div> |
| 180 | + |
| 181 | + {/* Connections Header with Chevron */} |
| 182 | + <div |
| 183 | + className='flex flex-shrink-0 cursor-pointer items-center gap-[8px] px-[10px] pt-[5px] pb-[5px]' |
| 184 | + onClick={toggleConnectionsCollapsed} |
| 185 | + onKeyDown={(e) => { |
| 186 | + if (e.key === 'Enter' || e.key === ' ') { |
| 187 | + e.preventDefault() |
| 188 | + toggleConnectionsCollapsed() |
| 189 | + } |
| 190 | + }} |
| 191 | + role='button' |
| 192 | + tabIndex={0} |
| 193 | + aria-label={isConnectionsAtMinHeight ? 'Expand connections' : 'Collapse connections'} |
| 194 | + > |
| 195 | + <ChevronUp |
| 196 | + className={ |
| 197 | + 'h-[14px] w-[14px] transition-transform' + |
| 198 | + (!isConnectionsAtMinHeight ? ' rotate-180' : '') |
| 199 | + } |
| 200 | + /> |
| 201 | + <div className='font-medium text-[#E6E6E6] text-[13px] dark:text-[#E6E6E6]'> |
| 202 | + Connections |
| 203 | + </div> |
| 204 | + </div> |
| 205 | + |
| 206 | + {/* Connections Content - Always visible */} |
| 207 | + <div className='flex-1 overflow-y-auto overflow-x-hidden px-[6px] pb-[8px]'> |
| 208 | + <ConnectionBlocks connections={incomingConnections} currentBlockId={currentBlock.id} /> |
| 209 | + </div> |
| 210 | + </div> |
| 211 | + )} |
| 212 | + </div> |
| 213 | + ) |
| 214 | +} |
0 commit comments