Skip to content

Commit cf666ec

Browse files
BrunoQuaresmapull[bot]
authored andcommitted
refactor(site): Add press any key option to reconnect on terminal screen (#5969)
1 parent 4affa6c commit cf666ec

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

site/src/pages/TerminalPage/TerminalPage.tsx

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { makeStyles } from "@material-ui/core/styles"
22
import { useMachine } from "@xstate/react"
3+
import { Stack } from "components/Stack/Stack"
34
import { FC, useEffect, useRef, useState } from "react"
45
import { Helmet } from "react-helmet-async"
56
import { useNavigate, useParams, useSearchParams } from "react-router-dom"
@@ -19,6 +20,34 @@ export const Language = {
1920
websocketErrorMessagePrefix: "WebSocket failed: ",
2021
}
2122

23+
const useReloading = (isDisconnected: boolean) => {
24+
const [status, setStatus] = useState<"reloading" | "notReloading">(
25+
"notReloading",
26+
)
27+
28+
// Retry connection on key press when it is disconnected
29+
useEffect(() => {
30+
if (!isDisconnected) {
31+
return
32+
}
33+
34+
const keyDownHandler = () => {
35+
setStatus("reloading")
36+
window.location.reload()
37+
}
38+
39+
document.addEventListener("keydown", keyDownHandler)
40+
41+
return () => {
42+
document.removeEventListener("keydown", keyDownHandler)
43+
}
44+
}, [isDisconnected])
45+
46+
return {
47+
status,
48+
}
49+
}
50+
2251
const TerminalPage: FC<
2352
React.PropsWithChildren<{
2453
readonly renderer?: XTerm.RendererType
@@ -67,6 +96,7 @@ const TerminalPage: FC<
6796
workspaceAgent,
6897
websocketError,
6998
} = terminalState.context
99+
const reloading = useReloading(isDisconnected)
70100

71101
// Create the terminal!
72102
useEffect(() => {
@@ -216,7 +246,16 @@ const TerminalPage: FC<
216246
{/* This overlay makes it more obvious that the terminal is disconnected. */}
217247
{/* It's nice for situations where Coder restarts, and they are temporarily disconnected. */}
218248
<div className={`${styles.overlay} ${isDisconnected ? "" : "connected"}`}>
219-
<span className={styles.overlayText}>Disconnected</span>
249+
{reloading.status === "reloading" ? (
250+
<span className={styles.overlayText}>Reloading...</span>
251+
) : (
252+
<Stack spacing={0.5} alignItems="center">
253+
<span className={styles.overlayText}>Disconnected</span>
254+
<span className={styles.overlaySubtext}>
255+
Press any key to retry
256+
</span>
257+
</Stack>
258+
)}
220259
</div>
221260
<div className={styles.terminal} ref={xtermRef} data-testid="terminal" />
222261
</>
@@ -225,7 +264,7 @@ const TerminalPage: FC<
225264

226265
export default TerminalPage
227266

228-
const useStyles = makeStyles(() => ({
267+
const useStyles = makeStyles((theme) => ({
229268
overlay: {
230269
position: "absolute",
231270
pointerEvents: "none",
@@ -238,17 +277,20 @@ const useStyles = makeStyles(() => ({
238277
justifyContent: "center",
239278
display: "flex",
240279
color: "white",
241-
fontFamily: MONOSPACE_FONT_FAMILY,
242-
fontSize: 18,
280+
fontSize: 16,
243281
backgroundColor: "rgba(0, 0, 0, 0.6)",
282+
backdropFilter: "blur(4px)",
244283
"&.connected": {
245284
opacity: 0,
246285
},
247286
},
248287
overlayText: {
249-
padding: 32,
250-
fontSize: 24,
251-
backgroundColor: "#000",
288+
fontSize: 16,
289+
fontWeight: 600,
290+
},
291+
overlaySubtext: {
292+
fontSize: 14,
293+
color: theme.palette.text.secondary,
252294
},
253295
terminal: {
254296
width: "100vw",

0 commit comments

Comments
 (0)