Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite5cce6a

Browse files
code-asherkylecarbs
authored andcommitted
feat: add terminal link component (#1538)
* Fix not being able to specify agent when connecting to terminalThe `workspace.agent` syntax was only used when fetching the agent andnot the workspace so it would try to fetch a workspace called`workspace.agent` instead of just `workspace`.* Add terminal link componentCurrently it does not show anywhere but we can drop it into theresources card later.
1 parented8811b commite5cce6a

File tree

6 files changed

+85
-13
lines changed

6 files changed

+85
-13
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import{Story}from"@storybook/react"
2+
importReactfrom"react"
3+
import{MockWorkspace}from"../../testHelpers/renderHelpers"
4+
import{TerminalLink,TerminalLinkProps}from"./TerminalLink"
5+
6+
exportdefault{
7+
title:"components/TerminalLink",
8+
component:TerminalLink,
9+
}
10+
11+
constTemplate:Story<TerminalLinkProps>=(args)=><TerminalLink{...args}/>
12+
13+
exportconstExample=Template.bind({})
14+
Example.args={
15+
workspaceName:MockWorkspace.name,
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
importLinkfrom"@material-ui/core/Link"
2+
importReactfrom"react"
3+
import*asTypesGenfrom"../../api/typesGenerated"
4+
5+
exportconstLanguage={
6+
linkText:"Open in terminal",
7+
}
8+
9+
exportinterfaceTerminalLinkProps{
10+
agentName?:TypesGen.WorkspaceAgent["name"]
11+
userName?:TypesGen.User["username"]
12+
workspaceName:TypesGen.Workspace["name"]
13+
}
14+
15+
/**
16+
* Generate a link to a terminal connected to the provided workspace agent. If
17+
* no agent is provided connect to the first agent.
18+
*
19+
* If no user name is provided "me" is used however it makes the link not
20+
* shareable.
21+
*/
22+
exportconstTerminalLink:React.FC<TerminalLinkProps>=({ agentName, userName="me", workspaceName})=>{
23+
return(
24+
<Linkhref={`/${userName}/${workspaceName}${agentName ?`.${agentName}` :""}/terminal`}target="_blank">
25+
{Language.linkText}
26+
</Link>
27+
)
28+
}

‎site/src/pages/TerminalPage/TerminalPage.test.tsx‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import React from "react"
66
import{Route,Routes}from"react-router-dom"
77
import{TextDecoder,TextEncoder}from"util"
88
import{ReconnectingPTYRequest}from"../../api/types"
9-
import{history,MockWorkspaceAgent,render}from"../../testHelpers/renderHelpers"
9+
import{history,MockWorkspace,MockWorkspaceAgent,render}from"../../testHelpers/renderHelpers"
1010
import{server}from"../../testHelpers/server"
1111
importTerminalPage,{Language}from"./TerminalPage"
1212

@@ -52,7 +52,7 @@ const expectTerminalText = (container: HTMLElement, text: string) => {
5252

5353
describe("TerminalPage",()=>{
5454
beforeEach(()=>{
55-
history.push("/some-user/my-workspace/terminal")
55+
history.push(`/some-user/${MockWorkspace.name}/terminal`)
5656
})
5757

5858
it("shows an error if fetching organizations fails",async()=>{
@@ -146,4 +146,20 @@ describe("TerminalPage", () => {
146146
expect(req.width).toBeGreaterThan(0)
147147
server.close()
148148
})
149+
150+
it("supports workspace.agent syntax",async()=>{
151+
// Given
152+
constserver=newWS("ws://localhost/api/v2/workspaceagents/"+MockWorkspaceAgent.id+"/pty")
153+
consttext="something to render"
154+
155+
// When
156+
history.push(`/some-user/${MockWorkspace.name}.${MockWorkspaceAgent.name}/terminal`)
157+
const{ container}=renderTerminal()
158+
159+
// Then
160+
awaitserver.connected
161+
server.send(text)
162+
awaitexpectTerminalText(container,text)
163+
server.close()
164+
})
149165
})

‎site/src/pages/TerminalPage/TerminalPage.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ const TerminalPage: React.FC<{
3434
constsearch=newURLSearchParams(location.search)
3535
returnsearch.get("reconnect")??uuidv4()
3636
})
37+
// The workspace name is in the format:
38+
// <workspace name>[.<agent name>]
39+
constworkspaceNameParts=workspace?.split(".")
3740
const[terminalState,sendEvent]=useMachine(terminalMachine,{
3841
context:{
42+
agentName:workspaceNameParts?.[1],
3943
reconnection:reconnectionToken,
40-
workspaceName:workspace,
44+
workspaceName:workspaceNameParts?.[0],
4145
username:username,
4246
},
4347
actions:{

‎site/src/testHelpers/handlers.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,16 @@ export const handlers = [
8080

8181
// workspaces
8282
rest.get("/api/v2/organizations/:organizationId/workspaces/:userName/:workspaceName",(req,res,ctx)=>{
83-
returnres(ctx.status(200),ctx.json(M.MockWorkspace))
83+
if(req.params.workspaceName!==M.MockWorkspace.name){
84+
returnres(
85+
ctx.status(404),
86+
ctx.json({
87+
message:"workspace not found",
88+
}),
89+
)
90+
}else{
91+
returnres(ctx.status(200),ctx.json(M.MockWorkspace))
92+
}
8493
}),
8594
rest.get("/api/v2/workspaces/:workspaceId",async(req,res,ctx)=>{
8695
returnres(ctx.status(200),ctx.json(M.MockWorkspace))

‎site/src/xServices/terminal/terminalXService.ts‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ export interface TerminalContext {
1414
websocketError?:Error|unknown
1515

1616
// Assigned by connecting!
17+
// The workspace agent is entirely optional. If the agent is omitted the
18+
// first agent will be used.
19+
agentName?:string
1720
username?:string
1821
workspaceName?:string
1922
reconnection?:string
2023
}
2124

2225
exporttypeTerminalEvent=
23-
|{type:"CONNECT";reconnection?:string;workspaceName?:string;username?:string}
26+
|{type:"CONNECT";agentName?:string;reconnection?:string;workspaceName?:string;username?:string}
2427
|{type:"WRITE";request:Types.ReconnectingPTYRequest}
2528
|{type:"READ";data:ArrayBuffer}
2629
|{type:"DISCONNECT"}
@@ -153,19 +156,14 @@ export const terminalMachine =
153156
getOrganizations:API.getOrganizations,
154157
getWorkspace:async(context)=>{
155158
if(!context.organizations||!context.workspaceName){
156-
thrownewError("organizations or workspace not set")
159+
thrownewError("organizations or workspacenamenot set")
157160
}
158161
returnAPI.getWorkspaceByOwnerAndName(context.organizations[0].id,context.username,context.workspaceName)
159162
},
160163
getWorkspaceAgent:async(context)=>{
161164
if(!context.workspace||!context.workspaceName){
162165
thrownewError("workspace or workspace name is not set")
163166
}
164-
// The workspace name is in the format:
165-
// <workspace name>[.<agent name>]
166-
// The workspace agent is entirely optional.
167-
constworkspaceNameParts=context.workspaceName.split(".")
168-
constagentName=workspaceNameParts[1]
169167

170168
constresources=awaitAPI.getWorkspaceResources(context.workspace.latest_build.id)
171169

@@ -174,10 +172,10 @@ export const terminalMachine =
174172
if(!resource.agents||resource.agents.length<1){
175173
return
176174
}
177-
if(!agentName){
175+
if(!context.agentName){
178176
returnresource.agents[0]
179177
}
180-
returnresource.agents.find((agent)=>agent.name===agentName)
178+
returnresource.agents.find((agent)=>agent.name===context.agentName)
181179
})
182180
.filter((a)=>a)[0]
183181
if(!agent){
@@ -218,6 +216,7 @@ export const terminalMachine =
218216
actions:{
219217
assignConnection:assign((context,event)=>({
220218
...context,
219+
agentName:event.agentName??context.agentName,
221220
reconnection:event.reconnection??context.reconnection,
222221
workspaceName:event.workspaceName??context.workspaceName,
223222
})),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp