- Notifications
You must be signed in to change notification settings - Fork929
feat: Add web terminal with reconnecting TTYs#1186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
31f27bc
feat: Add web terminal with reconnecting TTYs
kylecarbs15d843e
Add xstate service
kylecarbscb5ae98
Add the webpage for accessing a web terminal
kylecarbs229c7e4
Add terminal page tests
kylecarbs621aeb1
Merge branch 'main' into webterm
kylecarbs3e1a0a4
Use Ticker instead of Timer
kylecarbs4ef7106
Active Windows mode on Windows
kylecarbs19c7b54
Merge branch 'main' into webterm
kylecarbsFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Active Windows mode on Windows
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit4ef71062f9f6c8eaa99bb35c023979b46f59210c
There are no files selected for viewing
2 changes: 2 additions & 0 deletionsagent/agent.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletionssite/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionssite/src/api/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletionssite/src/pages/TerminalPage/TerminalPage.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
66 changes: 33 additions & 33 deletionssite/src/pages/TerminalPage/TerminalPage.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,6 +2,7 @@ import { makeStyles } from "@material-ui/core/styles" | ||
import { useMachine } from "@xstate/react" | ||
import React from "react" | ||
import { useLocation, useNavigate, useParams } from "react-router-dom" | ||
import { v4 as uuidv4 } from "uuid" | ||
import * as XTerm from "xterm" | ||
import { FitAddon } from "xterm-addon-fit" | ||
import { WebLinksAddon } from "xterm-addon-web-links" | ||
@@ -16,14 +17,6 @@ export const Language = { | ||
websocketErrorMessagePrefix: "WebSocket failed: ", | ||
} | ||
export const TerminalPage: React.FC<{ | ||
readonly renderer?: XTerm.RendererType | ||
}> = ({ renderer }) => { | ||
@@ -39,13 +32,14 @@ export const TerminalPage: React.FC<{ | ||
// a round-trip, and must be a UUIDv4. | ||
const [reconnectionToken] = React.useState<string>(() => { | ||
const search = new URLSearchParams(location.search) | ||
return search.get("reconnect") ?? uuidv4() | ||
}) | ||
const [terminalState, sendEvent] = useMachine(terminalMachine, { | ||
context: { | ||
reconnection: reconnectionToken, | ||
workspaceName: workspace, | ||
username: username, | ||
}, | ||
actions: { | ||
readMessage: (_, event) => { | ||
if (typeof event.data === "string") { | ||
@@ -59,6 +53,8 @@ export const TerminalPage: React.FC<{ | ||
}, | ||
}) | ||
const isConnected = terminalState.matches("connected") | ||
const { organizationsError, workspaceError, workspaceAgentError, workspaceAgent, websocketError } = | ||
terminalState.context | ||
// Create the terminal! | ||
React.useEffect(() => { | ||
@@ -125,13 +121,7 @@ export const TerminalPage: React.FC<{ | ||
replace: true, | ||
}, | ||
) | ||
}, [location.search, navigate, reconnectionToken]) | ||
// Apply terminal options based on connection state. | ||
React.useEffect(() => { | ||
@@ -150,17 +140,17 @@ export const TerminalPage: React.FC<{ | ||
terminal.options = { | ||
disableStdin: true, | ||
} | ||
if (organizationsError instanceof Error) { | ||
terminal.writeln(Language.organizationsErrorMessagePrefix + organizationsError.message) | ||
} | ||
if (workspaceError instanceof Error) { | ||
terminal.writeln(Language.workspaceErrorMessagePrefix + workspaceError.message) | ||
} | ||
if (workspaceAgentError instanceof Error) { | ||
terminal.writeln(Language.workspaceAgentErrorMessagePrefix + workspaceAgentError.message) | ||
} | ||
if (websocketError instanceof Error) { | ||
terminal.writeln(Language.websocketErrorMessagePrefix + websocketError.message) | ||
} | ||
kylecarbs marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return | ||
} | ||
@@ -174,6 +164,7 @@ export const TerminalPage: React.FC<{ | ||
terminal.focus() | ||
terminal.options = { | ||
disableStdin: false, | ||
windowsMode: workspaceAgent?.operating_system === "windows", | ||
} | ||
// Update the terminal size post-fit. | ||
@@ -185,10 +176,11 @@ export const TerminalPage: React.FC<{ | ||
}, | ||
}) | ||
}, [ | ||
workspaceError, | ||
organizationsError, | ||
workspaceAgentError, | ||
websocketError, | ||
workspaceAgent, | ||
terminal, | ||
fitAddon, | ||
isConnected, | ||
@@ -199,7 +191,9 @@ export const TerminalPage: React.FC<{ | ||
<> | ||
{/* This overlay makes it more obvious that the terminal is disconnected. */} | ||
{/* It's nice for situations where Coder restarts, and they are temporarily disconnected. */} | ||
<div className={`${styles.overlay} ${isConnected ? "connected" : ""}`}> | ||
<span>Disconnected</span> | ||
</div> | ||
<div className={styles.terminal} ref={xtermRef} data-testid="terminal" /> | ||
</> | ||
) | ||
@@ -214,6 +208,12 @@ const useStyles = makeStyles(() => ({ | ||
bottom: 0, | ||
right: 0, | ||
zIndex: 1, | ||
alignItems: "center", | ||
justifyContent: "center", | ||
display: "flex", | ||
color: "white", | ||
fontFamily: MONOSPACE_FONT_FAMILY, | ||
fontSize: 18, | ||
backgroundColor: "rgba(0, 0, 0, 0.5)", | ||
"&.connected": { | ||
opacity: 0, | ||
1 change: 1 addition & 0 deletionssite/src/testHelpers/entities.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
50 changes: 19 additions & 31 deletionssite/src/xServices/terminal/terminalXService.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletionssite/yarn.lock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.