Skip to content

Commit c230bb5

Browse files
authoredMar 7, 2024
fix(comments): enable check (#5918)
* feat(core): add `error` to `useFeatureEnabled` * fix(comments): only enable comments if we are able to fetch project features
1 parent 3afe5a2 commit c230bb5

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed
 

‎packages/sanity/src/core/hooks/useFeatureEnabled.ts

+12-15
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ import {useSource} from '../studio'
77
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../studioClient'
88
import {useClient} from './useClient'
99

10+
const EMPTY_ARRAY: [] = []
11+
1012
interface Features {
11-
isLoading: boolean
1213
enabled: boolean
14+
error: Error | null
1315
features: string[]
16+
isLoading: boolean
1417
}
1518

1619
const INITIAL_LOADING_STATE: Features = {
17-
isLoading: true,
1820
enabled: true,
19-
features: [],
21+
error: null,
22+
features: EMPTY_ARRAY,
23+
isLoading: true,
2024
}
2125

2226
/**
@@ -37,29 +41,22 @@ export function useFeatureEnabled(featureKey: string): Features {
3741
const {projectId} = useSource()
3842

3943
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())
4845
cachedFeatureRequest.set(projectId, features)
4946
}
5047

5148
const featureInfo = useMemoObservable(
5249
() =>
53-
(cachedFeatureRequest.get(projectId) || of([])).pipe(
50+
(cachedFeatureRequest.get(projectId) || of(EMPTY_ARRAY)).pipe(
5451
map((features = []) => ({
5552
isLoading: false,
5653
enabled: Boolean(features?.includes(featureKey)),
5754
features,
55+
error: null,
5856
})),
5957
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})
6360
}),
6461
),
6562
[featureKey, projectId],

‎packages/sanity/src/structure/comments/src/hooks/useResolveCommentsEnabled.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function useResolveCommentsEnabled(
2323
documentType: string,
2424
): ResolveCommentsEnabled {
2525
// Check if the projects plan has the feature enabled
26-
const {enabled: featureEnabled, isLoading} = useFeatureEnabled('studioComments')
26+
const {enabled: featureEnabled, isLoading, error} = useFeatureEnabled('studioComments')
2727

2828
const {enabled} = useSource().document.unstable_comments
2929
// Check if the feature is enabled for the current document in the config
@@ -33,15 +33,19 @@ export function useResolveCommentsEnabled(
3333
)
3434

3535
const value: ResolveCommentsEnabled = useMemo(() => {
36-
if (isLoading || !enabledFromConfig) {
36+
// The feature is not enabled if:
37+
// - the feature is loading (`isLoading` is true)
38+
// - the feature is not enabled in the project (`enabledFromConfig` is false)
39+
// - there's an error when fetching the list of enabled features (`error` is set)
40+
if (isLoading || !enabledFromConfig || error) {
3741
return {enabled: false, mode: null}
3842
}
3943

4044
return {
4145
enabled: true,
4246
mode: featureEnabled ? 'default' : 'upsell',
4347
}
44-
}, [isLoading, enabledFromConfig, featureEnabled])
48+
}, [isLoading, enabledFromConfig, error, featureEnabled])
4549

4650
return value
4751
}

0 commit comments

Comments
 (0)
Please sign in to comment.