|
| 1 | +import { FitAddon } from "@xterm/addon-fit"; |
| 2 | +import { Terminal } from "@xterm/xterm"; |
| 3 | +import "@xterm/xterm/css/xterm.css"; |
| 4 | +import * as React from "react"; |
| 5 | + |
| 6 | +import { base64ToArray, stringToBase64 } from "@/util/base64"; |
| 7 | + |
| 8 | +const TermWriteEventName = "tsunami:termwrite"; |
| 9 | + |
| 10 | +type TermSize = { |
| 11 | + rows: number; |
| 12 | + cols: number; |
| 13 | +}; |
| 14 | + |
| 15 | +type TermInputPayload = { |
| 16 | + id: string; |
| 17 | + termsize?: TermSize; |
| 18 | + data64?: string; |
| 19 | +}; |
| 20 | + |
| 21 | +type TermWritePayload = { |
| 22 | + id: string; |
| 23 | + data64: string; |
| 24 | +}; |
| 25 | + |
| 26 | +async function sendTermInput(payload: TermInputPayload) { |
| 27 | + const response = await fetch("/api/terminput", { |
| 28 | + method: "POST", |
| 29 | + headers: { |
| 30 | + "Content-Type": "application/json", |
| 31 | + }, |
| 32 | + body: JSON.stringify(payload), |
| 33 | + }); |
| 34 | + if (!response.ok) { |
| 35 | + throw new Error(`terminal input request failed: ${response.status} ${response.statusText}`); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +const TsunamiTerm = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(function TsunamiTerm( |
| 40 | + props, |
| 41 | + ref |
| 42 | +) { |
| 43 | + const { id, ...outerProps } = props; |
| 44 | + const outerRef = React.useRef<HTMLDivElement>(null); |
| 45 | + const termRef = React.useRef<HTMLDivElement>(null); |
| 46 | + const terminalRef = React.useRef<Terminal | null>(null); |
| 47 | + |
| 48 | + const setOuterRef = React.useCallback( |
| 49 | + (elem: HTMLDivElement) => { |
| 50 | + outerRef.current = elem; |
| 51 | + if (typeof ref === "function") { |
| 52 | + ref(elem); |
| 53 | + return; |
| 54 | + } |
| 55 | + if (ref != null) { |
| 56 | + ref.current = elem; |
| 57 | + } |
| 58 | + }, |
| 59 | + [ref] |
| 60 | + ); |
| 61 | + |
| 62 | + React.useEffect(() => { |
| 63 | + if (termRef.current == null) { |
| 64 | + return; |
| 65 | + } |
| 66 | + const terminal = new Terminal({ |
| 67 | + convertEol: false, |
| 68 | + }); |
| 69 | + const fitAddon = new FitAddon(); |
| 70 | + terminal.loadAddon(fitAddon); |
| 71 | + terminal.open(termRef.current); |
| 72 | + fitAddon.fit(); |
| 73 | + terminalRef.current = terminal; |
| 74 | + |
| 75 | + const onDataDisposable = terminal.onData((data) => { |
| 76 | + if (id == null || id === "") { |
| 77 | + return; |
| 78 | + } |
| 79 | + sendTermInput({ |
| 80 | + id, |
| 81 | + data64: stringToBase64(data), |
| 82 | + }).catch((error) => { |
| 83 | + console.error("Failed to send terminal input:", error); |
| 84 | + }); |
| 85 | + }); |
| 86 | + const onResizeDisposable = terminal.onResize((size) => { |
| 87 | + if (id == null || id === "") { |
| 88 | + return; |
| 89 | + } |
| 90 | + sendTermInput({ |
| 91 | + id, |
| 92 | + termsize: { |
| 93 | + rows: size.rows, |
| 94 | + cols: size.cols, |
| 95 | + }, |
| 96 | + }).catch((error) => { |
| 97 | + console.error("Failed to send terminal resize:", error); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + const resizeObserver = new ResizeObserver(() => { |
| 102 | + fitAddon.fit(); |
| 103 | + }); |
| 104 | + if (outerRef.current != null) { |
| 105 | + resizeObserver.observe(outerRef.current); |
| 106 | + } |
| 107 | + |
| 108 | + return () => { |
| 109 | + resizeObserver.disconnect(); |
| 110 | + onResizeDisposable.dispose(); |
| 111 | + onDataDisposable.dispose(); |
| 112 | + terminal.dispose(); |
| 113 | + terminalRef.current = null; |
| 114 | + }; |
| 115 | + }, [id]); |
| 116 | + |
| 117 | + React.useEffect(() => { |
| 118 | + const handleTermWrite = (event: Event) => { |
| 119 | + const detail = (event as CustomEvent<TermWritePayload>).detail; |
| 120 | + if (detail == null || detail.id !== id || detail.data64 == null || detail.data64 === "") { |
| 121 | + return; |
| 122 | + } |
| 123 | + try { |
| 124 | + terminalRef.current?.write(base64ToArray(detail.data64)); |
| 125 | + } catch (error) { |
| 126 | + console.error("Failed to process term write event:", error); |
| 127 | + } |
| 128 | + }; |
| 129 | + window.addEventListener(TermWriteEventName, handleTermWrite); |
| 130 | + return () => { |
| 131 | + window.removeEventListener(TermWriteEventName, handleTermWrite); |
| 132 | + }; |
| 133 | + }, [id]); |
| 134 | + |
| 135 | + return ( |
| 136 | + <div {...outerProps} id={id} ref={setOuterRef}> |
| 137 | + <div ref={termRef} className="w-full h-full" /> |
| 138 | + </div> |
| 139 | + ); |
| 140 | +}); |
| 141 | + |
| 142 | +export { TermWriteEventName, TsunamiTerm }; |
0 commit comments