chore: format frontend code

This commit is contained in:
Akhilesh Rangani
2024-10-21 13:57:45 -06:00
parent 2897b908fd
commit 6fb1364d6f
64 changed files with 1421 additions and 1272 deletions

View File

@ -1,18 +1,17 @@
"use client";
"use client"
import { Button } from "@/components/ui/button";
import Tab from "@/components/ui/tab";
import { Terminal } from "@xterm/xterm";
import { Loader2, Plus, SquareTerminal, TerminalSquare } from "lucide-react";
import { toast } from "sonner";
import EditorTerminal from "./terminal";
import { useTerminal } from "@/context/TerminalContext";
import { useEffect } from "react";
import { Button } from "@/components/ui/button"
import Tab from "@/components/ui/tab"
import { useSocket } from "@/context/SocketContext"
import { useTerminal } from "@/context/TerminalContext"
import { Terminal } from "@xterm/xterm"
import { Loader2, Plus, SquareTerminal, TerminalSquare } from "lucide-react"
import { useEffect } from "react"
import { toast } from "sonner"
import EditorTerminal from "./terminal"
export default function Terminals() {
const { socket } = useSocket();
const { socket } = useSocket()
const {
terminals,
@ -22,24 +21,24 @@ export default function Terminals() {
activeTerminalId,
setActiveTerminalId,
creatingTerminal,
} = useTerminal();
} = useTerminal()
const activeTerminal = terminals.find((t) => t.id === activeTerminalId);
const activeTerminal = terminals.find((t) => t.id === activeTerminalId)
// Effect to set the active terminal when a new one is created
useEffect(() => {
if (terminals.length > 0 && !activeTerminalId) {
setActiveTerminalId(terminals[terminals.length - 1].id);
setActiveTerminalId(terminals[terminals.length - 1].id)
}
}, [terminals, activeTerminalId, setActiveTerminalId]);
}, [terminals, activeTerminalId, setActiveTerminalId])
const handleCreateTerminal = () => {
if (terminals.length >= 4) {
toast.error("You reached the maximum # of terminals.");
return;
toast.error("You reached the maximum # of terminals.")
return
}
createNewTerminal();
};
createNewTerminal()
}
return (
<>
@ -85,7 +84,7 @@ export default function Terminals() {
? { ...term, terminal: t }
: term
)
);
)
}}
visible={activeTerminalId === term.id}
/>
@ -98,5 +97,5 @@ export default function Terminals() {
</div>
)}
</>
);
}
)
}

View File

@ -1,12 +1,12 @@
"use client";
"use client"
import { Terminal } from "@xterm/xterm";
import { FitAddon } from "@xterm/addon-fit";
import "./xterm.css";
import { FitAddon } from "@xterm/addon-fit"
import { Terminal } from "@xterm/xterm"
import "./xterm.css"
import { useEffect, useRef, useState } from "react";
import { Socket } from "socket.io-client";
import { Loader2 } from "lucide-react";
import { Loader2 } from "lucide-react"
import { useEffect, useRef } from "react"
import { Socket } from "socket.io-client"
export default function EditorTerminal({
socket,
@ -15,16 +15,16 @@ export default function EditorTerminal({
setTerm,
visible,
}: {
socket: Socket;
id: string;
term: Terminal | null;
setTerm: (term: Terminal) => void;
visible: boolean;
socket: Socket
id: string
term: Terminal | null
setTerm: (term: Terminal) => void
visible: boolean
}) {
const terminalRef = useRef(null);
const terminalRef = useRef(null)
useEffect(() => {
if (!terminalRef.current) return;
if (!terminalRef.current) return
// console.log("new terminal", id, term ? "reusing" : "creating");
const terminal = new Terminal({
@ -36,56 +36,56 @@ export default function EditorTerminal({
fontSize: 14,
lineHeight: 1.5,
letterSpacing: 0,
});
})
setTerm(terminal);
setTerm(terminal)
return () => {
if (terminal) terminal.dispose();
};
}, []);
if (terminal) terminal.dispose()
}
}, [])
useEffect(() => {
if (!term) return;
if (!term) return
if (!terminalRef.current) return;
const fitAddon = new FitAddon();
term.loadAddon(fitAddon);
term.open(terminalRef.current);
fitAddon.fit();
if (!terminalRef.current) return
const fitAddon = new FitAddon()
term.loadAddon(fitAddon)
term.open(terminalRef.current)
fitAddon.fit()
const disposableOnData = term.onData((data) => {
socket.emit("terminalData", id, data);
});
socket.emit("terminalData", id, data)
})
const disposableOnResize = term.onResize((dimensions) => {
// const terminal_size = {
// width: dimensions.cols,
// height: dimensions.rows,
// };
fitAddon.fit();
socket.emit("terminalResize", dimensions);
});
fitAddon.fit()
socket.emit("terminalResize", dimensions)
})
return () => {
disposableOnData.dispose();
disposableOnResize.dispose();
};
}, [term, terminalRef.current]);
disposableOnData.dispose()
disposableOnResize.dispose()
}
}, [term, terminalRef.current])
useEffect(() => {
if (!term) return;
if (!term) return
const handleTerminalResponse = (response: { id: string; data: string }) => {
if (response.id === id) {
term.write(response.data);
term.write(response.data)
}
};
socket.on("terminalResponse", handleTerminalResponse);
}
socket.on("terminalResponse", handleTerminalResponse)
return () => {
socket.off("terminalResponse", handleTerminalResponse);
};
}, [term, id, socket]);
socket.off("terminalResponse", handleTerminalResponse)
}
}, [term, id, socket])
return (
<>
@ -102,5 +102,5 @@ export default function EditorTerminal({
) : null}
</div>
</>
);
)
}

View File

@ -35,7 +35,7 @@
* Default styles for xterm.js
*/
.xterm {
.xterm {
cursor: text;
position: relative;
user-select: none;
@ -80,7 +80,7 @@
.xterm .composition-view {
/* TODO: Composition position got messed up somewhere */
background: transparent;
color: #FFF;
color: #fff;
display: none;
position: absolute;
white-space: nowrap;
@ -154,12 +154,12 @@
}
.xterm .xterm-accessibility-tree:not(.debug) *::selection {
color: transparent;
color: transparent;
}
.xterm .xterm-accessibility-tree {
user-select: text;
white-space: pre;
user-select: text;
white-space: pre;
}
.xterm .live-region {
@ -176,33 +176,55 @@ white-space: pre;
opacity: 1 !important;
}
.xterm-underline-1 { text-decoration: underline; }
.xterm-underline-2 { text-decoration: double underline; }
.xterm-underline-3 { text-decoration: wavy underline; }
.xterm-underline-4 { text-decoration: dotted underline; }
.xterm-underline-5 { text-decoration: dashed underline; }
.xterm-underline-1 {
text-decoration: underline;
}
.xterm-underline-2 {
text-decoration: double underline;
}
.xterm-underline-3 {
text-decoration: wavy underline;
}
.xterm-underline-4 {
text-decoration: dotted underline;
}
.xterm-underline-5 {
text-decoration: dashed underline;
}
.xterm-overline {
text-decoration: overline;
}
.xterm-overline.xterm-underline-1 { text-decoration: overline underline; }
.xterm-overline.xterm-underline-2 { text-decoration: overline double underline; }
.xterm-overline.xterm-underline-3 { text-decoration: overline wavy underline; }
.xterm-overline.xterm-underline-4 { text-decoration: overline dotted underline; }
.xterm-overline.xterm-underline-5 { text-decoration: overline dashed underline; }
.xterm-overline.xterm-underline-1 {
text-decoration: overline underline;
}
.xterm-overline.xterm-underline-2 {
text-decoration: overline double underline;
}
.xterm-overline.xterm-underline-3 {
text-decoration: overline wavy underline;
}
.xterm-overline.xterm-underline-4 {
text-decoration: overline dotted underline;
}
.xterm-overline.xterm-underline-5 {
text-decoration: overline dashed underline;
}
.xterm-strikethrough {
text-decoration: line-through;
}
.xterm-screen .xterm-decoration-container .xterm-decoration {
z-index: 6;
position: absolute;
z-index: 6;
position: absolute;
}
.xterm-screen .xterm-decoration-container .xterm-decoration.xterm-decoration-top-layer {
z-index: 7;
.xterm-screen
.xterm-decoration-container
.xterm-decoration.xterm-decoration-top-layer {
z-index: 7;
}
.xterm-decoration-overview-ruler {
@ -216,4 +238,4 @@ z-index: 7;
.xterm-decoration-top {
z-index: 2;
position: relative;
}
}