MCPcopy Index your code
hub / github.com/coder/coder / useAgentContainers

Function useAgentContainers

site/src/modules/resources/useAgentContainers.ts:15–64  ·  view source on GitHub ↗
(
	agent: WorkspaceAgent,
)

Source from the content-addressed store, hash-verified

13} from "#/api/typesGenerated";
14
15export function useAgentContainers(
16 agent: WorkspaceAgent,
17): readonly WorkspaceAgentDevcontainer[] | undefined {
18 const queryClient = useQueryClient();
19 const queryKey = workspaceAgentContainersKey(agent.id);
20
21 const {
22 data: devcontainers,
23 error: queryError,
24 isLoading: queryIsLoading,
25 } = useQuery({
26 ...workspaceAgentContainers(agent),
27 select: (res) => res.devcontainers,
28 });
29
30 const updateDevcontainersCache = useEffectEvent(
31 async (data: WorkspaceAgentListContainersResponse) => {
32 queryClient.setQueryData(queryKey, data);
33 },
34 );
35
36 useEffect(() => {
37 if (agent.status !== "connected" || queryIsLoading || queryError) {
38 return;
39 }
40
41 const socket = watchAgentContainers(agent.id);
42
43 socket.addEventListener("message", (event) => {
44 if (event.parseError) {
45 toast.error("Failed to update containers.", {
46 description: "Please try refreshing the page.",
47 });
48 return;
49 }
50
51 updateDevcontainersCache(event.parsedMessage);
52 });
53
54 socket.addEventListener("error", () => {
55 toast.error("Failed to load containers.", {
56 description: "Please try refreshing the page.",
57 });
58 });
59
60 return () => socket.close();
61 }, [agent.id, agent.status, queryIsLoading, queryError]);
62
63 return devcontainers;
64}

Callers 2

AgentRowFunction · 0.90

Calls 5

workspaceAgentContainersFunction · 0.90
watchAgentContainersFunction · 0.90
addEventListenerMethod · 0.65
closeMethod · 0.65

Tested by

no test coverage detected