@@ -35,11 +35,7 @@ import {
3535} from '@/app/workspace/[workspaceId]/w/[workflowId]/components/terminal/hooks'
3636import { getBlock } from '@/blocks'
3737import type { ConsoleEntry } from '@/stores/terminal'
38- import {
39- DEFAULT_TERMINAL_HEIGHT ,
40- useTerminalConsoleStore ,
41- useTerminalStore ,
42- } from '@/stores/terminal'
38+ import { useTerminalConsoleStore , useTerminalStore } from '@/stores/terminal'
4339import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
4440
4541/**
@@ -254,9 +250,11 @@ const isEventFromEditableElement = (e: KeyboardEvent): boolean => {
254250export function Terminal ( ) {
255251 const terminalRef = useRef < HTMLElement > ( null )
256252 const prevEntriesLengthRef = useRef ( 0 )
253+ const prevWorkflowEntriesLengthRef = useRef ( 0 )
257254 const {
258255 terminalHeight,
259256 setTerminalHeight,
257+ lastExpandedHeight,
260258 outputPanelWidth,
261259 setOutputPanelWidth,
262260 openOnRun,
@@ -301,6 +299,22 @@ export function Terminal() {
301299
302300 const isExpanded = terminalHeight > NEAR_MIN_THRESHOLD
303301
302+ /**
303+ * Expands the terminal to its last meaningful height, with safeguards:
304+ * - Never expands below {@link DEFAULT_EXPANDED_HEIGHT}.
305+ * - Never exceeds 70% of the viewport height.
306+ */
307+ const expandToLastHeight = useCallback ( ( ) => {
308+ setIsToggling ( true )
309+ const maxHeight = window . innerHeight * 0.7
310+ const desiredHeight = Math . max (
311+ lastExpandedHeight || DEFAULT_EXPANDED_HEIGHT ,
312+ DEFAULT_EXPANDED_HEIGHT
313+ )
314+ const targetHeight = Math . min ( desiredHeight , maxHeight )
315+ setTerminalHeight ( targetHeight )
316+ } , [ lastExpandedHeight , setTerminalHeight ] )
317+
304318 /**
305319 * Get all entries for current workflow (before filtering) for filter options
306320 */
@@ -404,6 +418,28 @@ export function Terminal() {
404418 return selectedEntry . output
405419 } , [ selectedEntry , showInput ] )
406420
421+ /**
422+ * Auto-open the terminal on new entries when "Open on run" is enabled.
423+ * This mirrors the header toggle behavior by using expandToLastHeight,
424+ * ensuring we always get the same smooth height transition.
425+ */
426+ useEffect ( ( ) => {
427+ if ( ! openOnRun ) {
428+ prevWorkflowEntriesLengthRef . current = allWorkflowEntries . length
429+ return
430+ }
431+
432+ const previousLength = prevWorkflowEntriesLengthRef . current
433+ const currentLength = allWorkflowEntries . length
434+
435+ // Only react when new entries are added for the active workflow
436+ if ( currentLength > previousLength && terminalHeight <= MIN_HEIGHT ) {
437+ expandToLastHeight ( )
438+ }
439+
440+ prevWorkflowEntriesLengthRef . current = currentLength
441+ } , [ allWorkflowEntries . length , expandToLastHeight , openOnRun , terminalHeight ] )
442+
407443 /**
408444 * Handle row click - toggle if clicking same entry
409445 * Disables auto-selection when user manually selects, re-enables when deselecting
@@ -421,14 +457,13 @@ export function Terminal() {
421457 * Handle header click - toggle between expanded and collapsed
422458 */
423459 const handleHeaderClick = useCallback ( ( ) => {
424- setIsToggling ( true )
425-
426460 if ( isExpanded ) {
461+ setIsToggling ( true )
427462 setTerminalHeight ( MIN_HEIGHT )
428463 } else {
429- setTerminalHeight ( DEFAULT_TERMINAL_HEIGHT )
464+ expandToLastHeight ( )
430465 }
431- } , [ isExpanded , setTerminalHeight ] )
466+ } , [ expandToLastHeight , isExpanded , setTerminalHeight ] )
432467
433468 /**
434469 * Handle transition end - reset toggling state
@@ -628,10 +663,7 @@ export function Terminal() {
628663 e . preventDefault ( )
629664
630665 if ( ! isExpanded ) {
631- setIsToggling ( true )
632- const maxHeight = window . innerHeight * 0.7
633- const targetHeight = Math . min ( DEFAULT_EXPANDED_HEIGHT , maxHeight )
634- setTerminalHeight ( targetHeight )
666+ expandToLastHeight ( )
635667 }
636668
637669 if ( e . key === 'ArrowLeft' ) {
@@ -647,7 +679,7 @@ export function Terminal() {
647679
648680 window . addEventListener ( 'keydown' , handleKeyDown )
649681 return ( ) => window . removeEventListener ( 'keydown' , handleKeyDown )
650- } , [ selectedEntry , showInput , hasInputData , isExpanded ] )
682+ } , [ expandToLastHeight , selectedEntry , showInput , hasInputData , isExpanded ] )
651683
652684 /**
653685 * Handle Escape to unselect and Enter to re-enable auto-selection
@@ -1188,10 +1220,7 @@ export function Terminal() {
11881220 onClick = { ( e ) => {
11891221 e . stopPropagation ( )
11901222 if ( ! isExpanded ) {
1191- setIsToggling ( true )
1192- const maxHeight = window . innerHeight * 0.7
1193- const targetHeight = Math . min ( DEFAULT_EXPANDED_HEIGHT , maxHeight )
1194- setTerminalHeight ( targetHeight )
1223+ expandToLastHeight ( )
11951224 }
11961225 if ( showInput ) setShowInput ( false )
11971226 } }
@@ -1209,10 +1238,7 @@ export function Terminal() {
12091238 onClick = { ( e ) => {
12101239 e . stopPropagation ( )
12111240 if ( ! isExpanded ) {
1212- setIsToggling ( true )
1213- const maxHeight = window . innerHeight * 0.7
1214- const targetHeight = Math . min ( DEFAULT_EXPANDED_HEIGHT , maxHeight )
1215- setTerminalHeight ( targetHeight )
1241+ expandToLastHeight ( )
12161242 }
12171243 setShowInput ( true )
12181244 } }
0 commit comments