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

Function useWorkspaceBuildLogs

site/src/hooks/useWorkspaceBuildLogs.ts:6–45  ·  view source on GitHub ↗
(
	// buildId is optional because sometimes the build is not loaded yet
	buildId: string | undefined,
	enabled = true,
)

Source from the content-addressed store, hash-verified

4import type { ProvisionerJobLog } from "#/api/typesGenerated";
5
6export const useWorkspaceBuildLogs = (
7 // buildId is optional because sometimes the build is not loaded yet
8 buildId: string | undefined,
9 enabled = true,
10) => {
11 const [logs, setLogs] = useState<ProvisionerJobLog[]>();
12 const socket = useRef<WebSocket>(undefined);
13
14 useEffect(() => {
15 if (!buildId || !enabled) {
16 socket.current?.close();
17 return;
18 }
19
20 // Every time this hook is called reset the values
21 setLogs(undefined);
22
23 socket.current = watchBuildLogsByBuildId(buildId, {
24 // Retrieve all the logs
25 after: -1,
26 onMessage: (log) => {
27 setLogs((previousLogs) => {
28 if (!previousLogs) {
29 return [log];
30 }
31 return [...previousLogs, log];
32 });
33 },
34 onError: () => {
35 toast.error(`Error on getting "${buildId}" build logs.`);
36 },
37 });
38
39 return () => {
40 socket.current?.close();
41 };
42 }, [buildId, enabled]);
43
44 return logs;
45};

Callers 4

WorkspaceReadyPageFunction · 0.90
BuildingWorkspaceFunction · 0.90
WorkspaceBuildPageFunction · 0.90
WorkspaceBuildLogSectionFunction · 0.90

Calls 2

watchBuildLogsByBuildIdFunction · 0.90
closeMethod · 0.65

Tested by

no test coverage detected