1
1
import { watchBuildLogsByTemplateVersionId } from "api/api" ;
2
2
import type { ProvisionerJobLog , TemplateVersion } from "api/typesGenerated" ;
3
+ import { useEffectEvent } from "hooks/hookPolyfills" ;
3
4
import { useEffect , useState } from "react" ;
4
5
5
6
export const useWatchVersionLogs = (
6
7
templateVersion : TemplateVersion | undefined ,
7
8
options ?: { onDone : ( ) => Promise < unknown > } ,
8
9
) => {
9
- const [ logs , setLogs ] = useState < ProvisionerJobLog [ ] | undefined > ( ) ;
10
+ const [ logs , setLogs ] = useState < ProvisionerJobLog [ ] > ( ) ;
10
11
const templateVersionId = templateVersion ?. id ;
11
- const templateVersionStatus = templateVersion ?. job . status ;
12
-
13
- // biome-ignore lint/correctness/useExhaustiveDependencies: consider refactoring
14
- useEffect ( ( ) => {
12
+ const [ cachedVersionId , setCachedVersionId ] = useState ( templateVersionId ) ;
13
+ if ( cachedVersionId !== templateVersionId ) {
14
+ setCachedVersionId ( templateVersionId ) ;
15
15
setLogs ( undefined ) ;
16
- } , [ templateVersionId ] ) ;
16
+ }
17
17
18
+ const templateVersionStatus = templateVersion ?. job . status ;
19
+ const stableOnDone = useEffectEvent ( ( ) => options ?. onDone ( ) ) ;
18
20
useEffect ( ( ) => {
19
21
if ( ! templateVersionId || ! templateVersionStatus ) {
20
22
return ;
@@ -31,16 +33,14 @@ export const useWatchVersionLogs = (
31
33
onMessage : ( log ) => {
32
34
setLogs ( ( logs ) => ( logs ? [ ...logs , log ] : [ log ] ) ) ;
33
35
} ,
34
- onDone : options ?. onDone ,
36
+ onDone : stableOnDone ,
35
37
onError : ( error ) => {
36
38
console . error ( error ) ;
37
39
} ,
38
40
} ) ;
39
41
40
- return ( ) => {
41
- socket . close ( ) ;
42
- } ;
43
- } , [ options ?. onDone , templateVersionId , templateVersionStatus ] ) ;
42
+ return ( ) => socket . close ( ) ;
43
+ } , [ stableOnDone , templateVersionId , templateVersionStatus ] ) ;
44
44
45
45
return logs ;
46
46
} ;
0 commit comments