2024-05-08 23:52:08 -07:00
|
|
|
// Helper functions for terminal instances
|
|
|
|
|
|
|
|
import { createId } from "@paralleldrive/cuid2";
|
|
|
|
import { Terminal } from "@xterm/xterm";
|
|
|
|
import { Socket } from "socket.io-client";
|
|
|
|
|
|
|
|
export const createTerminal = ({
|
|
|
|
setTerminals,
|
|
|
|
setActiveTerminalId,
|
|
|
|
setCreatingTerminal,
|
2024-07-31 17:49:59 -07:00
|
|
|
command,
|
2024-05-08 23:52:08 -07:00
|
|
|
socket,
|
|
|
|
}: {
|
|
|
|
setTerminals: React.Dispatch<React.SetStateAction<{
|
|
|
|
id: string;
|
|
|
|
terminal: Terminal | null;
|
|
|
|
}[]>>;
|
|
|
|
setActiveTerminalId: React.Dispatch<React.SetStateAction<string>>;
|
|
|
|
setCreatingTerminal: React.Dispatch<React.SetStateAction<boolean>>;
|
2024-07-31 17:49:59 -07:00
|
|
|
command?: string;
|
2024-05-08 23:52:08 -07:00
|
|
|
socket: Socket;
|
|
|
|
|
|
|
|
}) => {
|
|
|
|
setCreatingTerminal(true);
|
|
|
|
const id = createId();
|
|
|
|
console.log("creating terminal, id:", id);
|
|
|
|
|
|
|
|
setTerminals((prev) => [...prev, { id, terminal: null }]);
|
|
|
|
setActiveTerminalId(id);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
socket.emit("createTerminal", id, () => {
|
|
|
|
setCreatingTerminal(false);
|
2024-07-31 17:49:59 -07:00
|
|
|
if (command) socket.emit("terminalData", id, command + "\n");
|
2024-05-08 23:52:08 -07:00
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const closeTerminal = ({
|
|
|
|
term,
|
|
|
|
terminals,
|
|
|
|
setTerminals,
|
|
|
|
setActiveTerminalId,
|
|
|
|
setClosingTerminal,
|
|
|
|
socket,
|
|
|
|
activeTerminalId,
|
|
|
|
} : {
|
|
|
|
term: {
|
|
|
|
id: string;
|
|
|
|
terminal: Terminal | null
|
|
|
|
}
|
|
|
|
terminals: {
|
|
|
|
id: string;
|
|
|
|
terminal: Terminal | null
|
|
|
|
}[]
|
|
|
|
setTerminals: React.Dispatch<React.SetStateAction<{
|
|
|
|
id: string;
|
|
|
|
terminal: Terminal | null
|
|
|
|
}[]>>
|
|
|
|
setActiveTerminalId: React.Dispatch<React.SetStateAction<string>>
|
|
|
|
setClosingTerminal: React.Dispatch<React.SetStateAction<string>>
|
|
|
|
socket: Socket
|
|
|
|
activeTerminalId: string
|
|
|
|
}) => {
|
|
|
|
const numTerminals = terminals.length;
|
|
|
|
const index = terminals.findIndex((t) => t.id === term.id);
|
|
|
|
if (index === -1) return;
|
|
|
|
|
|
|
|
setClosingTerminal(term.id);
|
|
|
|
|
|
|
|
socket.emit("closeTerminal", term.id, () => {
|
|
|
|
setClosingTerminal("");
|
|
|
|
|
|
|
|
const nextId =
|
|
|
|
activeTerminalId === term.id
|
|
|
|
? numTerminals === 1
|
|
|
|
? null
|
|
|
|
: index < numTerminals - 1
|
|
|
|
? terminals[index + 1].id
|
|
|
|
: terminals[index - 1].id
|
|
|
|
: activeTerminalId;
|
|
|
|
|
|
|
|
setTerminals((prev) => prev.filter((t) => t.id !== term.id));
|
|
|
|
|
|
|
|
if (!nextId) {
|
|
|
|
setActiveTerminalId("");
|
|
|
|
} else {
|
|
|
|
const nextTerminal = terminals.find((t) => t.id === nextId);
|
|
|
|
if (nextTerminal) {
|
|
|
|
setActiveTerminalId(nextTerminal.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|