Improve VNC route

This commit is contained in:
Pierre HUBERT 2023-12-08 17:13:38 +01:00
parent 64eb90c0a4
commit 74b77be013

View File

@ -1,5 +1,9 @@
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import FullscreenIcon from "@mui/icons-material/Fullscreen";
import FullscreenExitIcon from "@mui/icons-material/FullscreenExit";
import { IconButton } from "@mui/material";
import React, { useEffect } from "react"; import React, { useEffect } from "react";
import { useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import { VncScreen } from "react-vnc"; import { VncScreen } from "react-vnc";
import { ServerApi } from "../api/ServerApi"; import { ServerApi } from "../api/ServerApi";
import { VMApi, VMInfo } from "../api/VMApi"; import { VMApi, VMInfo } from "../api/VMApi";
@ -33,10 +37,13 @@ export function VNCRoute(): React.ReactElement {
function VNCInner(p: { vm: VMInfo }): React.ReactElement { function VNCInner(p: { vm: VMInfo }): React.ReactElement {
const snackbar = useSnackbar(); const snackbar = useSnackbar();
const navigate = useNavigate();
const [token, setToken] = React.useState<VNCTokenInfo | undefined>(); const [token, setToken] = React.useState<VNCTokenInfo | undefined>();
const [counter, setCounter] = React.useState(1); const [counter, setCounter] = React.useState(1);
const vncRef = React.createRef<HTMLDivElement>();
const connect = async (force: boolean) => { const connect = async (force: boolean) => {
try { try {
if (force) setCounter(counter + 1); if (force) setCounter(counter + 1);
@ -63,29 +70,64 @@ function VNCInner(p: { vm: VMInfo }): React.ReactElement {
connect(true); connect(true);
}; };
const goBack = () => {
navigate(p.vm.ViewURL);
};
const goFullScreen = () => {
if (vncRef.current) vncRef.current.requestFullscreen();
};
const exitFullScreen = () => {
document.exitFullscreen();
};
useEffect(() => { useEffect(() => {
connect(false); connect(false);
if (vncRef.current) {
vncRef.current.onfullscreenchange = () => setCounter(counter + 1);
}
}); });
if (token === undefined) if (token === undefined)
return <p>Please wait, connecting to the machine...</p>; return <p>Please wait, connecting to the machine...</p>;
return ( return (
<div <div ref={vncRef}>
style={{ {/* Controls */}
display: "flex", <div>
alignItems: "center", <IconButton onClick={goBack}>
justifyContent: "center", <ArrowBackIcon />
height: "100%", </IconButton>
}} {!document.fullscreenElement ? (
> <IconButton onClick={goFullScreen}>
<VncScreen <FullscreenIcon />
url={token.url} </IconButton>
onDisconnect={() => { ) : (
console.info("VNC disconnected " + token?.url); <IconButton onClick={exitFullScreen}>
disconnected(); <FullscreenExitIcon />
</IconButton>
)}
</div>
{/* Screen */}
<div
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
height: "100%",
}} }}
/> >
<VncScreen
url={token.url}
onDisconnect={() => {
console.info("VNC disconnected " + token?.url);
disconnected();
}}
/>
</div>
</div> </div>
); );
} }