Fix operator terminal SSR crash
This commit is contained in:
@@ -3,8 +3,6 @@
|
||||
import Link from 'next/link'
|
||||
import { useSearchParams } from 'next/navigation'
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { Terminal } from 'xterm'
|
||||
import { FitAddon } from 'xterm-addon-fit'
|
||||
import 'xterm/css/xterm.css'
|
||||
|
||||
interface ReadinessPodSummary {
|
||||
@@ -47,6 +45,11 @@ interface LiveTerminalSession {
|
||||
websocketUrl: string
|
||||
}
|
||||
|
||||
type XTermModule = typeof import('xterm')
|
||||
type FitAddonModule = typeof import('xterm-addon-fit')
|
||||
type XTermInstance = InstanceType<XTermModule['Terminal']>
|
||||
type FitAddonInstance = InstanceType<FitAddonModule['FitAddon']>
|
||||
|
||||
type LoadState = 'idle' | 'loading' | 'ready' | 'error'
|
||||
|
||||
const defaultAlias = (sandboxId: string | null) => `ssh openshell-${sandboxId || 'my-assistant'}`
|
||||
@@ -66,8 +69,8 @@ export default function OperatorTerminalPage() {
|
||||
const [liveSession, setLiveSession] = useState<LiveTerminalSession | null>(null)
|
||||
|
||||
const terminalContainerRef = useRef<HTMLDivElement | null>(null)
|
||||
const terminalRef = useRef<Terminal | null>(null)
|
||||
const fitAddonRef = useRef<FitAddon | null>(null)
|
||||
const terminalRef = useRef<XTermInstance | null>(null)
|
||||
const fitAddonRef = useRef<FitAddonInstance | null>(null)
|
||||
const socketRef = useRef<WebSocket | null>(null)
|
||||
|
||||
const refreshReadiness = useCallback(async () => {
|
||||
@@ -148,6 +151,19 @@ export default function OperatorTerminalPage() {
|
||||
return
|
||||
}
|
||||
|
||||
let disposed = false
|
||||
let cleanupResize: (() => void) | null = null
|
||||
|
||||
;(async () => {
|
||||
const [{ Terminal }, { FitAddon }] = await Promise.all([
|
||||
import('xterm'),
|
||||
import('xterm-addon-fit'),
|
||||
])
|
||||
|
||||
if (disposed || !terminalContainerRef.current) {
|
||||
return
|
||||
}
|
||||
|
||||
const term = new Terminal({
|
||||
cursorBlink: true,
|
||||
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
|
||||
@@ -180,11 +196,17 @@ export default function OperatorTerminalPage() {
|
||||
}
|
||||
|
||||
window.addEventListener('resize', handleResize)
|
||||
cleanupResize = () => window.removeEventListener('resize', handleResize)
|
||||
})().catch((error) => {
|
||||
setTerminalState('error')
|
||||
setTerminalStatus(error instanceof Error ? error.message : 'Failed to load terminal renderer.')
|
||||
})
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('resize', handleResize)
|
||||
disposed = true
|
||||
cleanupResize?.()
|
||||
socketRef.current?.close()
|
||||
term.dispose()
|
||||
terminalRef.current?.dispose()
|
||||
terminalRef.current = null
|
||||
fitAddonRef.current = null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user