Skip to content

Commit 29e2625

Browse files
committed
use useQuery to get external agent credentials
1 parent 73049f3 commit 29e2625

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

site/src/api/queries/workspaces.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,13 @@ export const updateWorkspaceACL = (workspaceId: string) => {
430430
},
431431
};
432432
};
433+
434+
export const workspaceAgentCredentials = (
435+
workspaceId: string,
436+
agentName: string,
437+
) => {
438+
return {
439+
queryKey: ["workspaces", workspaceId, "agents", agentName, "credentials"],
440+
queryFn: () => API.getWorkspaceAgentCredentials(workspaceId, agentName),
441+
};
442+
};

site/src/modules/resources/AgentExternal.tsx

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { API } from "api/api";
2-
import { getErrorMessage } from "api/errors";
1+
import { workspaceAgentCredentials } from "api/queries/workspaces";
32
import type { Workspace, WorkspaceAgent } from "api/typesGenerated";
4-
import isChromatic from "chromatic/isChromatic";
3+
import { ErrorAlert } from "components/Alert/ErrorAlert";
54
import { CodeExample } from "components/CodeExample/CodeExample";
6-
import { displayError } from "components/GlobalSnackbar/utils";
7-
import { type FC, useEffect, useState } from "react";
5+
import { Loader } from "components/Loader/Loader";
6+
import type { FC } from "react";
7+
import { useQuery } from "react-query";
88

99
interface AgentExternalProps {
1010
isExternalAgent: boolean;
@@ -17,29 +17,24 @@ export const AgentExternal: FC<AgentExternalProps> = ({
1717
agent,
1818
workspace,
1919
}) => {
20-
const [externalAgentToken, setExternalAgentToken] = useState<string | null>(
21-
null,
22-
);
23-
const [command, setCommand] = useState<string | null>(null);
20+
if (!isExternalAgent) {
21+
return null;
22+
}
23+
24+
const {
25+
data: credentials,
26+
error,
27+
isLoading,
28+
isError,
29+
} = useQuery(workspaceAgentCredentials(workspace.id, agent.name));
30+
31+
if (isLoading) {
32+
return <Loader />;
33+
}
2434

25-
const origin = isChromatic() ? "https://example.com" : window.location.origin;
26-
useEffect(() => {
27-
if (
28-
isExternalAgent &&
29-
(agent.status === "timeout" || agent.status === "connecting")
30-
) {
31-
API.getWorkspaceAgentCredentials(workspace.id, agent.name)
32-
.then((res) => {
33-
setExternalAgentToken(res.agent_token);
34-
setCommand(res.command);
35-
})
36-
.catch((err) => {
37-
displayError(
38-
getErrorMessage(err, "Failed to get external agent credentials"),
39-
);
40-
});
41-
}
42-
}, [isExternalAgent, agent.status, workspace.id, agent.name]);
35+
if (isError) {
36+
return <ErrorAlert error={error} />;
37+
}
4338

4439
return (
4540
<section className="text-base text-muted-foreground pb-2 leading-relaxed">
@@ -48,7 +43,7 @@ export const AgentExternal: FC<AgentExternalProps> = ({
4843
{workspace.name} workspace:
4944
</p>
5045
<CodeExample
51-
code={command ?? ""}
46+
code={credentials?.command ?? ""}
5247
secret={false}
5348
redactPattern={/CODER_AGENT_TOKEN="([^"]+)"/g}
5449
redactReplacement={`CODER_AGENT_TOKEN="********"`}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy