import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import React, { useEffect } from "react"; import { Terminal } from "@xterm/xterm"; import { FitAddon } from "xterm-addon-fit"; import "@xterm/xterm/css/xterm.css"; interface Props { id: string; containerId: string; } export const DockerLogsId: React.FC = ({ id, containerId }) => { const [term, setTerm] = React.useState(); const [lines, setLines] = React.useState(40); const createTerminal = (): Terminal => { const container = document.getElementById(id); if (container) { container.innerHTML = ""; } const termi = new Terminal({ cursorBlink: true, cols: 80, rows: 30, lineHeight: 1.4, fontWeight: 400, convertEol: true, theme: { cursor: "transparent", background: "rgba(0, 0, 0, 0)", }, }); const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; const wsUrl = `${protocol}//${window.location.host}/docker-container-logs?containerId=${containerId}&tail=${lines}`; const ws = new WebSocket(wsUrl); const fitAddon = new FitAddon(); termi.loadAddon(fitAddon); // @ts-ignore termi.open(container); fitAddon.fit(); termi.focus(); setTerm(termi); ws.onmessage = (e) => { termi.write(e.data); }; ws.onclose = (e) => { console.log(e.reason); termi.write(`Connection closed!\nReason: ${e.reason}\n`); }; return termi; }; useEffect(() => { createTerminal(); }, [lines, containerId]); useEffect(() => { term?.clear(); }, [lines, term]); return (
{ setLines(Number(e.target.value) || 1); }} />
); };