|
1 | 1 | 'use client' |
2 | 2 |
|
3 | | -import { type CommandHandle, type Sandbox } from 'e2b' |
4 | | -import { useCallback, useEffect, useRef, useState } from 'react' |
| 3 | +import type { CommandHandle, Sandbox } from 'e2b' |
| 4 | +import { useCallback, useEffect, useEffectEvent, useRef, useState } from 'react' |
5 | 5 | import type { SandboxManagementAuth } from '@/core/shared/sandbox-management-auth' |
6 | 6 | import { |
7 | 7 | DEFAULT_CWD, |
@@ -192,7 +192,7 @@ export default function DashboardTerminal({ |
192 | 192 |
|
193 | 193 | const resizePty = useCallback((size: { cols: number; rows: number }) => { |
194 | 194 | if (sandboxRef.current && pidRef.current) { |
195 | | - void sandboxRef.current.pty.resize(pidRef.current, size) |
| 195 | + void sandboxRef.current.pty.resize(pidRef.current, size).catch(() => {}) |
196 | 196 | } |
197 | 197 | }, []) |
198 | 198 |
|
@@ -378,7 +378,7 @@ export default function DashboardTerminal({ |
378 | 378 | }) |
379 | 379 | ptyRef.current = pty |
380 | 380 | pidRef.current = pty.pid |
381 | | - resizeTerminal() |
| 381 | + resizeTerminal({ force: true }) |
382 | 382 | setStatus('ready') |
383 | 383 | appendOutput(`PTY ${pty.pid} attached.\r\n`) |
384 | 384 | focusTerminal() |
@@ -555,38 +555,37 @@ export default function DashboardTerminal({ |
555 | 555 | } |
556 | 556 | }, [autoStart, launchTarget, queueTerminalCommand, status]) |
557 | 557 |
|
558 | | - useEffect(() => { |
559 | | - const handlePageHide = (event: PageTransitionEvent) => { |
560 | | - if (event.persisted) return |
| 558 | + const handlePageHide = useEffectEvent((event: PageTransitionEvent) => { |
| 559 | + if (event.persisted) return |
561 | 560 |
|
562 | | - abortCurrentStart() |
563 | | - void closeTerminal() |
564 | | - } |
| 561 | + abortCurrentStart() |
| 562 | + void closeTerminal() |
| 563 | + }) |
565 | 564 |
|
566 | | - const handlePageShow = (event: PageTransitionEvent) => { |
567 | | - if (!event.persisted || !ptyRef.current) return |
| 565 | + const handlePageShow = useEffectEvent((event: PageTransitionEvent) => { |
| 566 | + if (!event.persisted || !ptyRef.current) return |
568 | 567 |
|
569 | | - resizeTerminal() |
570 | | - focusTerminal() |
571 | | - } |
| 568 | + resizeTerminal({ force: true }) |
| 569 | + focusTerminal() |
| 570 | + }) |
| 571 | + |
| 572 | + const handleTerminalUnmount = useEffectEvent(() => { |
| 573 | + startGenerationRef.current += 1 |
| 574 | + isStartingRef.current = false |
| 575 | + clearPendingInput() |
| 576 | + void closeTerminal() |
| 577 | + }) |
572 | 578 |
|
| 579 | + useEffect(() => { |
573 | 580 | window.addEventListener('pagehide', handlePageHide) |
574 | 581 | window.addEventListener('pageshow', handlePageShow) |
575 | 582 |
|
576 | 583 | return () => { |
577 | 584 | window.removeEventListener('pagehide', handlePageHide) |
578 | 585 | window.removeEventListener('pageshow', handlePageShow) |
579 | | - abortCurrentStart() |
580 | | - clearPendingInput() |
581 | | - void closeTerminal() |
| 586 | + handleTerminalUnmount() |
582 | 587 | } |
583 | | - }, [ |
584 | | - abortCurrentStart, |
585 | | - clearPendingInput, |
586 | | - closeTerminal, |
587 | | - focusTerminal, |
588 | | - resizeTerminal, |
589 | | - ]) |
| 588 | + }, []) |
590 | 589 |
|
591 | 590 | return ( |
592 | 591 | <> |
|
0 commit comments