Skip to content

Commit

Permalink
fix(comments): enable check (#5918)
Browse files Browse the repository at this point in the history
* feat(core): add `error` to `useFeatureEnabled`

* fix(comments): only enable comments if we are able to fetch project features
  • Loading branch information
hermanwikner authored and juice49 committed Mar 13, 2024
1 parent f74f8d5 commit 33b3ce7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
27 changes: 12 additions & 15 deletions packages/sanity/src/core/hooks/useFeatureEnabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import {useSource} from '../studio'
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../studioClient'
import {useClient} from './useClient'

const EMPTY_ARRAY: [] = []

interface Features {
isLoading: boolean
enabled: boolean
error: Error | null
features: string[]
isLoading: boolean
}

const INITIAL_LOADING_STATE: Features = {
isLoading: true,
enabled: true,
features: [],
error: null,
features: EMPTY_ARRAY,
isLoading: true,
}

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

if (!cachedFeatureRequest.get(projectId)) {
const features = fetchFeatures({versionedClient}).pipe(
shareReplay(),
catchError((error) => {
console.error(error)
// Return an empty list of features if the request fails
return of([])
}),
)
const features = fetchFeatures({versionedClient}).pipe(shareReplay())
cachedFeatureRequest.set(projectId, features)
}

const featureInfo = useMemoObservable(
() =>
(cachedFeatureRequest.get(projectId) || of([])).pipe(
(cachedFeatureRequest.get(projectId) || of(EMPTY_ARRAY)).pipe(
map((features = []) => ({
isLoading: false,
enabled: Boolean(features?.includes(featureKey)),
features,
error: null,
})),
startWith(INITIAL_LOADING_STATE),
catchError((err: Error) => {
console.error(err)
return of({isLoading: false, enabled: true, features: []})
catchError((error: Error) => {
return of({isLoading: false, enabled: false, features: EMPTY_ARRAY, error})
}),
),
[featureKey, projectId],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function useResolveCommentsEnabled(
documentType: string,
): ResolveCommentsEnabled {
// Check if the projects plan has the feature enabled
const {enabled: featureEnabled, isLoading} = useFeatureEnabled('studioComments')
const {enabled: featureEnabled, isLoading, error} = useFeatureEnabled('studioComments')

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

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

return {
enabled: true,
mode: featureEnabled ? 'default' : 'upsell',
}
}, [isLoading, enabledFromConfig, featureEnabled])
}, [isLoading, enabledFromConfig, error, featureEnabled])

return value
}

0 comments on commit 33b3ce7

Please sign in to comment.