import { useState, useRef, useEffect, Dispatch, SetStateAction } from 'react'; import { RFB } from 'novnc-node'; import { Box, Menu, MenuItem, Typography } from '@material-ui/core'; import { makeStyles } from '@material-ui/core/styles'; import CloseIcon from '@material-ui/icons/Close'; import KeyboardIcon from '@material-ui/icons/Keyboard'; import IconButton from '@material-ui/core/IconButton'; import VncDisplay from './VncDisplay'; import { Panel } from '../Panels'; import { BLACK, RED, TEXT } from '../../lib/consts/DEFAULT_THEME'; import keyCombinations from './keyCombinations'; import putJSON from '../../lib/fetchers/putJSON'; import { HeaderText } from '../Text'; import Spinner from '../Spinner'; const useStyles = makeStyles(() => ({ displayBox: { paddingTop: '1em', paddingBottom: 0, width: '75%', height: '80%', }, vncBox: { width: '75%', height: '80%', }, closeButton: { borderRadius: 8, backgroundColor: RED, '&:hover': { backgroundColor: RED, }, }, keyboardButton: { borderRadius: 8, backgroundColor: TEXT, '&:hover': { backgroundColor: TEXT, }, }, closeBox: { paddingBottom: '1em', paddingLeft: '.7em', paddingRight: 0, }, buttonsBox: { paddingTop: 0, }, keysItem: { backgroundColor: TEXT, paddingRight: '3em', '&:hover': { backgroundColor: TEXT, }, }, })); interface PreviewProps { setMode: Dispatch>; uuid: string; serverName: string | string[] | undefined; } interface VncConnectionProps { protocol: string; forward_port: number; } const FullSize = ({ setMode, uuid, serverName }: PreviewProps): JSX.Element => { const [anchorEl, setAnchorEl] = useState(null); const rfb = useRef(undefined); const hostname = useRef(undefined); const [vncConnection, setVncConnection] = useState< VncConnectionProps | undefined >(undefined); const [displaySize] = useState<{ width: string; height: string; }>({ width: '75%', height: '80%' }); const classes = useStyles(); useEffect(() => { if (typeof window !== 'undefined') { hostname.current = window.location.hostname; } if (!vncConnection) (async () => { const res = await putJSON( `${process.env.NEXT_PUBLIC_API_URL}/manage_vnc_pipes`, { server_uuid: uuid, is_open: true, }, ); setVncConnection(await res.json()); })(); }, [uuid, vncConnection]); const handleClick = (event: React.MouseEvent): void => { setAnchorEl(event.currentTarget); }; const handleClickClose = async () => { await putJSON(`${process.env.NEXT_PUBLIC_API_URL}/manage_vnc_pipes`, { server_uuid: uuid, is_open: false, }); }; const handleSendKeys = (scans: string[]) => { if (rfb.current) { if (!scans.length) rfb.current.sendCtrlAltDel(); else { // Send pressing keys scans.forEach((scan) => { rfb.current.sendKey(scan, 1); }); // Send releasing keys in reverse order for (let i = scans.length - 1; i >= 0; i -= 1) { rfb.current.sendKey(scans[i], 0); } } setAnchorEl(null); } }; return ( {vncConnection ? ( ) : ( )} { handleClickClose(); setMode(true); }} > setAnchorEl(null)} > {keyCombinations.map(({ keys, scans }) => { return ( handleSendKeys(scans)} className={classes.keysItem} key={keys} > {keys} ); })} ); }; export default FullSize;