Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(comments): enable check #5918

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the error catch here so that we can catch it below instead.

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
}
Loading