Skip to content

Commit

Permalink
save terminal content on page unload/reload
Browse files Browse the repository at this point in the history
  • Loading branch information
srieger1 committed Jun 25, 2024
1 parent 4fb5e0d commit e6f64e4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion backend/src/websocket/ConsoleHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function (
username: string,
type: string,
): void {
console.log(environment, username);
console.log("ConsoleHandler: " + environment, username);
const envInstance = Environment.getActiveEnvironment(environment, username);
if (envInstance !== undefined) {
const envConsole = envInstance.getConsoleByAlias(type);
Expand Down
41 changes: 33 additions & 8 deletions frontend/src/components/Terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function XTerminal(props: TerminalProps): JSX.Element {
let resizeInterval: NodeJS.Timeout | undefined = undefined;

let unmounted = false;
let socketOpen = false;
//let socketOpen = false;
websocket.onopen = () => {
// close socket safely if component has already unmounted (strict mode)
if (unmounted) {
Expand All @@ -55,7 +55,7 @@ export default function XTerminal(props: TerminalProps): JSX.Element {
}

websocket.send(`auth ${localStorage.getItem("token")}`);
socketOpen = true;
//socketOpen = true;

// if the terminal is still available -> attach websocket
if (terminalRef) {
Expand All @@ -77,8 +77,18 @@ export default function XTerminal(props: TerminalProps): JSX.Element {
});

// restore terminal state
terminalRef.write(terminalState ?? "");
let state = undefined;
if (localStorage.getItem("terminalState-" + wsEndpoint)) {
state = localStorage.getItem("terminalState-" + wsEndpoint);
localStorage.removeItem("terminalState-" + wsEndpoint);
} else {
state = terminalState;
}
terminalRef.write(state ?? "");
// reset terminal colors
terminalRef.write("\x1B[0m");
// move cursor to the right
terminalRef.write("\x1B[1C");

// resize terminal
fitAddon.fit();
Expand All @@ -94,6 +104,18 @@ export default function XTerminal(props: TerminalProps): JSX.Element {
);
}

window.addEventListener("beforeunload", (event) => {
event.preventDefault();
console.log("Terminal: Saving state before unmount during reload");
const state = serAddon.serialize({ scrollback: 1000 });
localStorage.setItem("terminalState-" + wsEndpoint, state);
//onTerminalUnmount(wsEndpoint, state);
// if websocket is open or connecting, close it
if (websocket.readyState === 1) {
//websocket.close();
}
});

// componentWillUnmount
return () => {
unmounted = true;
Expand All @@ -102,12 +124,15 @@ export default function XTerminal(props: TerminalProps): JSX.Element {
if (resizeInterval) clearInterval(resizeInterval);

// serialize terminal state and close websocket
if (socketOpen) {
const state = serAddon.serialize({ scrollback: 1000 });
onTerminalUnmount(wsEndpoint, state);

websocket.close();
const state = serAddon.serialize({ scrollback: 1000 });
localStorage.setItem("terminalState-" + wsEndpoint, state);
onTerminalUnmount(wsEndpoint, state);
// if websocket is open or connecting, close it
if (websocket.readyState === 1) {
//websocket.close();
}

window.onbeforeunload = null;
};
});

Expand Down

0 comments on commit e6f64e4

Please sign in to comment.