102 lines
2.3 KiB
TypeScript
Raw Normal View History

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