Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 144d34e

Browse files
committed
Break out getting agent name
To make it easier to move away from this service.
1 parent 21f8731 commit 144d34e

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

site/src/utils/workspace.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,20 @@ export const hasJobError = (workspace: TypesGen.Workspace) => {
286286
export const paramsUsedToCreateWorkspace = (
287287
param: TypesGen.TemplateVersionParameter,
288288
) => !param.ephemeral;
289+
290+
export const getMatchingAgentOrFirst = (
291+
workspace: TypesGen.Workspace,
292+
agentName: string | undefined,
293+
): TypesGen.WorkspaceAgent | undefined => {
294+
return workspace.latest_build.resources
295+
.map((resource) => {
296+
if (!resource.agents || resource.agents.length === 0) {
297+
return;
298+
}
299+
if (!agentName) {
300+
return resource.agents[0];
301+
}
302+
return resource.agents.find((agent) => agent.name === agentName);
303+
})
304+
.filter((a) => a)[0];
305+
};

site/src/xServices/terminal/terminalXService.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { assign, createMachine } from "xstate";
22
import * as API from "api/api";
33
import * as TypesGen from "api/typesGenerated";
4+
import { getMatchingAgentOrFirst } from "utils/workspace";
45

56
interface ReconnectingPTYRequest {
67
readonly data?: string;
@@ -196,20 +197,10 @@ export const terminalMachine =
196197
if (!context.workspace || !context.workspaceName) {
197198
throw new Error("workspace or workspace name is not set");
198199
}
199-
200-
const agent = context.workspace.latest_build.resources
201-
.map((resource) => {
202-
if (!resource.agents || resource.agents.length === 0) {
203-
return;
204-
}
205-
if (!context.agentName) {
206-
return resource.agents[0];
207-
}
208-
return resource.agents.find(
209-
(agent) => agent.name === context.agentName,
210-
);
211-
})
212-
.filter((a) => a)[0];
200+
const agent = getMatchingAgentOrFirst(
201+
context.workspace,
202+
context.agentName,
203+
);
213204
if (!agent) {
214205
throw new Error("no agent found with id");
215206
}

0 commit comments

Comments
 (0)