Spaces:
Configuration error
Configuration error
File size: 4,336 Bytes
4dcc016 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | import { forwardRef, useEffect, useImperativeHandle, useMemo, useRef } from 'react'
import { useTerminalSession } from '../hooks/useTerminalSession'
function formatTime(timestamp) {
if (!timestamp) {
return 'Idle'
}
return new Date(timestamp * 1000).toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
}
function statusLabel(status) {
if (status === 'running') {
return 'Running'
}
if (status === 'failed') {
return 'Failed'
}
if (status === 'exited') {
return 'Completed'
}
return 'Starting'
}
const TerminalPane = forwardRef(function TerminalPane({ jobId, title, tone, onTelemetryChange }, ref) {
const { session, buffer, connectionState, error, lastOutputAt, restart, resize, sendInput, start, stop } =
useTerminalSession(jobId)
const viewportRef = useRef(null)
const scrollRef = useRef(null)
useImperativeHandle(
ref,
() => ({
submit: async (value) => {
await sendInput(value, true)
},
}),
[sendInput],
)
useEffect(() => {
onTelemetryChange?.({
jobId,
session,
connectionState,
error,
lastOutputAt,
})
}, [connectionState, error, jobId, lastOutputAt, onTelemetryChange, session])
useEffect(() => {
const container = scrollRef.current
if (container) {
container.scrollTop = container.scrollHeight
}
}, [buffer])
useEffect(() => {
const element = viewportRef.current
if (!element) {
return undefined
}
let frameId = 0
const measure = () => {
cancelAnimationFrame(frameId)
frameId = requestAnimationFrame(() => {
const style = getComputedStyle(element)
const fontSize = parseFloat(style.fontSize) || 15
const lineHeight = parseFloat(style.lineHeight) || 24
const cols = Math.max(48, Math.floor(element.clientWidth / (fontSize * 0.61)))
const rows = Math.max(14, Math.floor(element.clientHeight / lineHeight))
resize(cols, rows)
})
}
measure()
const observer = new ResizeObserver(measure)
observer.observe(element)
return () => {
cancelAnimationFrame(frameId)
observer.disconnect()
}
}, [resize])
const footerMeta = useMemo(
() => [
session?.status ? statusLabel(session.status) : 'Connecting',
session?.started_at ? `Started ${formatTime(session.started_at)}` : null,
session?.exit_code != null ? `Exit ${session.exit_code}` : null,
connectionState === 'connected' ? 'WS live' : connectionState,
].filter(Boolean),
[connectionState, session],
)
return (
<article className={`terminal-pane terminal-pane--${tone}`}>
<header className="terminal-pane__header">
<div className="terminal-pane__heading">
<div className="terminal-pane__title-row">
<span className="terminal-pane__dot" />
<h2>{title}</h2>
<span className={`status-chip status-chip--${session?.status || 'starting'}`}>
{statusLabel(session?.status)}
</span>
</div>
<p>{session?.command || 'Waiting for backend session...'}</p>
<small>{session?.cwd || 'No working directory available yet.'}</small>
</div>
<div className="terminal-pane__actions">
<button type="button" onClick={start}>
Attach
</button>
<button type="button" onClick={restart}>
Restart
</button>
<button type="button" onClick={stop}>
Stop
</button>
</div>
</header>
<div ref={viewportRef} className="terminal-pane__viewport">
<div ref={scrollRef} className="terminal-pane__scroll">
<pre className="terminal-pane__buffer">{buffer || 'Starting session...\n'}</pre>
{session?.status === 'running' ? <span className="terminal-pane__cursor" aria-hidden="true" /> : null}
</div>
</div>
<footer className="terminal-pane__footer">
<div className="terminal-pane__meta">
{footerMeta.map((item) => (
<span key={item}>{item}</span>
))}
{error ? <span className="terminal-pane__error">{error}</span> : null}
</div>
</footer>
</article>
)
})
export default TerminalPane
|