Skip to content

Commit 989f7fd

Browse files
committed
[grid] Improve LiveView password dialog
This was not working and leaving the user clueless when the password was wrong. Now, when the password is wrong we send back the user to the running sessions page and display a notification with the error.
1 parent 90ce3e2 commit 989f7fd

3 files changed

Lines changed: 56 additions & 29 deletions

File tree

javascript/grid-ui/src/components/LiveView/LiveView.tsx

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,24 @@
1818
import React, { useEffect, useState } from 'react'
1919
import RFB from '@novnc/novnc/core/rfb'
2020
import PasswordDialog from './PasswordDialog'
21+
import MuiAlert, { AlertProps } from '@mui/material/Alert'
22+
import Snackbar from '@mui/material/Snackbar'
23+
24+
const Alert = React.forwardRef<HTMLDivElement, AlertProps>(function Alert (
25+
props,
26+
ref,
27+
) {
28+
return <MuiAlert elevation={6} ref={ref} variant="filled" {...props} />
29+
})
2130

2231
function LiveView (props) {
23-
// let rfb: RFB = null
2432
let canvas: any = null
2533

2634
const [open, setOpen] = useState(false)
2735
const [message, setMessage] = useState('')
2836
const [rfb, setRfb] = useState<RFB>(null)
29-
// const [canvas, setCanvas] = useState(null)
37+
const [openErrorAlert, setOpenErrorAlert] = useState(false)
38+
const [openSuccessAlert, setOpenSuccessAlert] = useState(false)
3039

3140
const handlePasswordDialog = (state: boolean): void => {
3241
setOpen(state)
@@ -38,7 +47,6 @@ function LiveView (props) {
3847
}
3948
rfb.disconnect()
4049
setRfb(null)
41-
// rfb = null
4250
}
4351

4452
const connect = () => {
@@ -48,23 +56,16 @@ function LiveView (props) {
4856
return
4957
}
5058

51-
// rfb = new RFB(canvas, props.url, {})
52-
// rfb = new RFB(canvas, props.url, { credentials: { password: 'secret' } })
53-
// rfb.scaleViewport = props.scaleViewport
54-
// rfb.background = 'rgb(247,248,248)'
55-
// rfb.addEventListener('credentialsrequired', handleCredentials)
56-
// rfb.addEventListener('securityfailure', securityFailed)
5759
const newRfb = new RFB(canvas, props.url, {})
5860
newRfb.scaleViewport = props.scaleViewport
5961
newRfb.background = 'rgb(247,248,248)'
6062
newRfb.addEventListener('credentialsrequired', handleCredentials)
6163
newRfb.addEventListener('securityfailure', securityFailed)
62-
// newRfb.addEventListener('connect', connectedToServer)
64+
newRfb.addEventListener('connect', connectedToServer)
6365
setRfb(newRfb)
6466
}
6567

6668
const registerChild = ref => {
67-
// setCanvas(ref)
6869
canvas = ref
6970
}
7071

@@ -90,20 +91,20 @@ function LiveView (props) {
9091
errorMessage = 'New connection has been rejected'
9192
}
9293
setMessage(errorMessage)
93-
connect()
94+
setOpenErrorAlert(true)
9495
}
9596

9697
const handleCredentials = () => {
9798
handlePasswordDialog(true)
9899
}
99100

100-
// const connectedToServer = () => {
101-
// console.log('connectedToServer')
102-
// setOpen(false)
103-
// }
101+
const connectedToServer = () => {
102+
setOpenSuccessAlert(true)
103+
}
104104

105105
const handleCredentialsEntered = (password: string) => {
106106
rfb.sendCredentials({ username: '', password: password })
107+
setOpen(false)
107108
}
108109

109110
const handlePasswordDialogClose = () => {
@@ -124,6 +125,15 @@ function LiveView (props) {
124125
rfb.blur()
125126
}
126127

128+
const handleClose = (event?: React.SyntheticEvent | Event,
129+
reason?: string) => {
130+
if (reason === 'clickaway') {
131+
return
132+
}
133+
setOpenErrorAlert(false)
134+
props.onClose()
135+
}
136+
127137
return (
128138
<div
129139
style={
@@ -139,12 +149,30 @@ function LiveView (props) {
139149
<PasswordDialog
140150
title="LiveView (VNC) Password"
141151
open={open}
142-
setOpen={handlePasswordDialog}
152+
openDialog={handlePasswordDialog}
143153
onConfirm={handleCredentialsEntered}
144154
onCancel={handlePasswordDialogClose}
155+
/>
156+
<Snackbar
157+
open={openErrorAlert}
158+
anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
159+
autoHideDuration={6000}
160+
onClose={handleClose}
161+
>
162+
<Alert severity="error" sx={{ width: '100%' }}>
163+
{message}
164+
</Alert>
165+
</Snackbar>
166+
<Snackbar
167+
open={openSuccessAlert}
168+
anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
169+
autoHideDuration={4000}
170+
onClose={() => setOpenSuccessAlert(false)}
145171
>
146-
{message}
147-
</PasswordDialog>
172+
<Alert severity="success" sx={{ width: '100%' }}>
173+
Connected successfully!
174+
</Alert>
175+
</Snackbar>
148176
</div>
149177
)
150178
}

javascript/grid-ui/src/components/LiveView/PasswordDialog.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ interface State {
4040
}
4141

4242
const PasswordDialog = (props) => {
43-
const { title, children, open, setOpen, onConfirm, onCancel } = props
43+
const { title, children, open, openDialog, onConfirm, onCancel } = props
4444
const [values, setValues] = useState<State>({
4545
amount: '',
4646
password: '',
@@ -63,8 +63,8 @@ const PasswordDialog = (props) => {
6363
return (
6464
<Dialog
6565
open={open}
66-
onClose={() => setOpen(false)}
67-
aria-labelledby='password-dialog'
66+
onClose={() => openDialog(false)}
67+
aria-labelledby="password-dialog"
6868
>
6969
<DialogTitle id='password-dialog'>{title}</DialogTitle>
7070
<DialogContent>
@@ -105,22 +105,22 @@ const PasswordDialog = (props) => {
105105
</DialogContent>
106106
<DialogActions>
107107
<Button
108-
variant='contained'
108+
variant="contained"
109109
onClick={() => {
110-
setOpen(false)
110+
openDialog(false)
111111
onCancel()
112112
}}
113-
color='secondary'
113+
color="secondary"
114114
>
115115
Cancel
116116
</Button>
117117
<Button
118-
variant='contained'
118+
variant="contained"
119119
onClick={() => {
120-
setOpen(false)
120+
// setOpen(false)
121121
onConfirm(values.password)
122122
}}
123-
color='primary'
123+
color="primary"
124124
>
125125
Accept
126126
</Button>

javascript/grid-ui/src/models/session-data.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export function createSessionData (
5757
const vncUrl = new URL(vnc)
5858
url.pathname = vncUrl.pathname
5959
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'
60-
url.port = '4444'
6160
vnc = url.href
6261
} catch (error) {
6362
console.log(error)

0 commit comments

Comments
 (0)