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,14 +70,48 @@ 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 ref={vncRef}>
{/* Controls */}
<div>
<IconButton onClick={goBack}>
<ArrowBackIcon />
</IconButton>
{!document.fullscreenElement ? (
<IconButton onClick={goFullScreen}>
<FullscreenIcon />
</IconButton>
) : (
<IconButton onClick={exitFullScreen}>
<FullscreenExitIcon />
</IconButton>
)}
</div>
{/* Screen */}
<div <div
style={{ style={{
display: "flex", display: "flex",
@ -87,5 +128,6 @@ function VNCInner(p: { vm: VMInfo }): React.ReactElement {
}} }}
/> />
</div> </div>
</div>
); );
} }