Fix operator terminal SSR crash
This commit is contained in:
@@ -3,8 +3,6 @@
|
|||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { useSearchParams } from 'next/navigation'
|
import { useSearchParams } from 'next/navigation'
|
||||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||||
import { Terminal } from 'xterm'
|
|
||||||
import { FitAddon } from 'xterm-addon-fit'
|
|
||||||
import 'xterm/css/xterm.css'
|
import 'xterm/css/xterm.css'
|
||||||
|
|
||||||
interface ReadinessPodSummary {
|
interface ReadinessPodSummary {
|
||||||
@@ -47,6 +45,11 @@ interface LiveTerminalSession {
|
|||||||
websocketUrl: string
|
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'
|
type LoadState = 'idle' | 'loading' | 'ready' | 'error'
|
||||||
|
|
||||||
const defaultAlias = (sandboxId: string | null) => `ssh openshell-${sandboxId || 'my-assistant'}`
|
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 [liveSession, setLiveSession] = useState<LiveTerminalSession | null>(null)
|
||||||
|
|
||||||
const terminalContainerRef = useRef<HTMLDivElement | null>(null)
|
const terminalContainerRef = useRef<HTMLDivElement | null>(null)
|
||||||
const terminalRef = useRef<Terminal | null>(null)
|
const terminalRef = useRef<XTermInstance | null>(null)
|
||||||
const fitAddonRef = useRef<FitAddon | null>(null)
|
const fitAddonRef = useRef<FitAddonInstance | null>(null)
|
||||||
const socketRef = useRef<WebSocket | null>(null)
|
const socketRef = useRef<WebSocket | null>(null)
|
||||||
|
|
||||||
const refreshReadiness = useCallback(async () => {
|
const refreshReadiness = useCallback(async () => {
|
||||||
@@ -148,43 +151,62 @@ export default function OperatorTerminalPage() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const term = new Terminal({
|
let disposed = false
|
||||||
cursorBlink: true,
|
let cleanupResize: (() => void) | null = null
|
||||||
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
|
|
||||||
fontSize: 13,
|
|
||||||
theme: {
|
|
||||||
background: '#000000',
|
|
||||||
foreground: '#f5f5f5',
|
|
||||||
cursor: '#76b900',
|
|
||||||
},
|
|
||||||
scrollback: 5000,
|
|
||||||
allowTransparency: false,
|
|
||||||
})
|
|
||||||
const fitAddon = new FitAddon()
|
|
||||||
term.loadAddon(fitAddon)
|
|
||||||
term.open(terminalContainerRef.current)
|
|
||||||
fitAddon.fit()
|
|
||||||
|
|
||||||
terminalRef.current = term
|
;(async () => {
|
||||||
fitAddonRef.current = fitAddon
|
const [{ Terminal }, { FitAddon }] = await Promise.all([
|
||||||
|
import('xterm'),
|
||||||
|
import('xterm-addon-fit'),
|
||||||
|
])
|
||||||
|
|
||||||
const handleResize = () => {
|
if (disposed || !terminalContainerRef.current) {
|
||||||
fitAddon.fit()
|
return
|
||||||
if (socketRef.current?.readyState === WebSocket.OPEN) {
|
|
||||||
socketRef.current.send(JSON.stringify({
|
|
||||||
type: 'resize',
|
|
||||||
cols: term.cols,
|
|
||||||
rows: term.rows,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
window.addEventListener('resize', handleResize)
|
const term = new Terminal({
|
||||||
|
cursorBlink: true,
|
||||||
|
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
|
||||||
|
fontSize: 13,
|
||||||
|
theme: {
|
||||||
|
background: '#000000',
|
||||||
|
foreground: '#f5f5f5',
|
||||||
|
cursor: '#76b900',
|
||||||
|
},
|
||||||
|
scrollback: 5000,
|
||||||
|
allowTransparency: false,
|
||||||
|
})
|
||||||
|
const fitAddon = new FitAddon()
|
||||||
|
term.loadAddon(fitAddon)
|
||||||
|
term.open(terminalContainerRef.current)
|
||||||
|
fitAddon.fit()
|
||||||
|
|
||||||
|
terminalRef.current = term
|
||||||
|
fitAddonRef.current = fitAddon
|
||||||
|
|
||||||
|
const handleResize = () => {
|
||||||
|
fitAddon.fit()
|
||||||
|
if (socketRef.current?.readyState === WebSocket.OPEN) {
|
||||||
|
socketRef.current.send(JSON.stringify({
|
||||||
|
type: 'resize',
|
||||||
|
cols: term.cols,
|
||||||
|
rows: term.rows,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 () => {
|
return () => {
|
||||||
window.removeEventListener('resize', handleResize)
|
disposed = true
|
||||||
|
cleanupResize?.()
|
||||||
socketRef.current?.close()
|
socketRef.current?.close()
|
||||||
term.dispose()
|
terminalRef.current?.dispose()
|
||||||
terminalRef.current = null
|
terminalRef.current = null
|
||||||
fitAddonRef.current = null
|
fitAddonRef.current = null
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user