1
1
import { watchBuildLogsByBuildId } from "api/api" ;
2
2
import { ProvisionerJobLog } from "api/typesGenerated" ;
3
3
import { displayError } from "components/GlobalSnackbar/utils" ;
4
- import { useState , useEffect } from "react" ;
4
+ import { useState , useEffect , useRef } from "react" ;
5
5
6
- // buildId is optional because sometimes the build is not loaded yet
7
- export const useWorkspaceBuildLogs = ( buildId ?: string ) => {
6
+ export const useWorkspaceBuildLogs = (
7
+ // buildId is optional because sometimes the build is not loaded yet
8
+ buildId : string | undefined ,
9
+ enabled : boolean = true ,
10
+ ) => {
8
11
const [ logs , setLogs ] = useState < ProvisionerJobLog [ ] > ( ) ;
12
+ const socket = useRef < WebSocket > ( ) ;
13
+
9
14
useEffect ( ( ) => {
10
- if ( ! buildId ) {
15
+ if ( ! buildId || ! enabled ) {
16
+ socket . current ?. close ( ) ;
11
17
return ;
12
18
}
13
19
14
20
// Every time this hook is called reset the values
15
21
setLogs ( undefined ) ;
16
22
17
- const socket = watchBuildLogsByBuildId ( buildId , {
23
+ socket . current = watchBuildLogsByBuildId ( buildId , {
18
24
// Retrieve all the logs
19
25
after : - 1 ,
20
26
onMessage : ( log ) => {
@@ -31,9 +37,9 @@ export const useWorkspaceBuildLogs = (buildId?: string) => {
31
37
} ) ;
32
38
33
39
return ( ) => {
34
- socket . close ( ) ;
40
+ socket . current ?. close ( ) ;
35
41
} ;
36
- } , [ buildId ] ) ;
42
+ } , [ buildId , enabled ] ) ;
37
43
38
44
return logs ;
39
45
} ;
0 commit comments