@@ -7,16 +7,20 @@ import {useSource} from '../studio'
7
7
import { DEFAULT_STUDIO_CLIENT_OPTIONS } from '../studioClient'
8
8
import { useClient } from './useClient'
9
9
10
+ const EMPTY_ARRAY : [ ] = [ ]
11
+
10
12
interface Features {
11
- isLoading : boolean
12
13
enabled : boolean
14
+ error : Error | null
13
15
features : string [ ]
16
+ isLoading : boolean
14
17
}
15
18
16
19
const INITIAL_LOADING_STATE : Features = {
17
- isLoading : true ,
18
20
enabled : true ,
19
- features : [ ] ,
21
+ error : null ,
22
+ features : EMPTY_ARRAY ,
23
+ isLoading : true ,
20
24
}
21
25
22
26
/**
@@ -37,29 +41,22 @@ export function useFeatureEnabled(featureKey: string): Features {
37
41
const { projectId} = useSource ( )
38
42
39
43
if ( ! cachedFeatureRequest . get ( projectId ) ) {
40
- const features = fetchFeatures ( { versionedClient} ) . pipe (
41
- shareReplay ( ) ,
42
- catchError ( ( error ) => {
43
- console . error ( error )
44
- // Return an empty list of features if the request fails
45
- return of ( [ ] )
46
- } ) ,
47
- )
44
+ const features = fetchFeatures ( { versionedClient} ) . pipe ( shareReplay ( ) )
48
45
cachedFeatureRequest . set ( projectId , features )
49
46
}
50
47
51
48
const featureInfo = useMemoObservable (
52
49
( ) =>
53
- ( cachedFeatureRequest . get ( projectId ) || of ( [ ] ) ) . pipe (
50
+ ( cachedFeatureRequest . get ( projectId ) || of ( EMPTY_ARRAY ) ) . pipe (
54
51
map ( ( features = [ ] ) => ( {
55
52
isLoading : false ,
56
53
enabled : Boolean ( features ?. includes ( featureKey ) ) ,
57
54
features,
55
+ error : null ,
58
56
} ) ) ,
59
57
startWith ( INITIAL_LOADING_STATE ) ,
60
- catchError ( ( err : Error ) => {
61
- console . error ( err )
62
- return of ( { isLoading : false , enabled : true , features : [ ] } )
58
+ catchError ( ( error : Error ) => {
59
+ return of ( { isLoading : false , enabled : false , features : EMPTY_ARRAY , error} )
63
60
} ) ,
64
61
) ,
65
62
[ featureKey , projectId ] ,
0 commit comments