From ec0c6ab4e259a38732a2a9275da5909a3955fbd6 Mon Sep 17 00:00:00 2001 From: Pedro Bonamin Date: Mon, 19 Feb 2024 12:36:52 +0100 Subject: [PATCH] fix(core): update featuresEnabled when switching projects --- packages/sanity/src/core/hooks/useFeatureEnabled.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/sanity/src/core/hooks/useFeatureEnabled.ts b/packages/sanity/src/core/hooks/useFeatureEnabled.ts index 5eff2e57850f..437b5d0765c7 100644 --- a/packages/sanity/src/core/hooks/useFeatureEnabled.ts +++ b/packages/sanity/src/core/hooks/useFeatureEnabled.ts @@ -3,6 +3,7 @@ import {useMemoObservable} from 'react-rx' import {type Observable, of} from 'rxjs' import {catchError, map, shareReplay, startWith} from 'rxjs/operators' +import {useSource} from '../studio' import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../studioClient' import {useClient} from './useClient' @@ -28,14 +29,15 @@ function fetchFeatures({versionedClient}: {versionedClient: SanityClient}): Obse }) } -let cachedFeatureRequest: Observable +const cachedFeatureRequest = new Map>() /** @internal */ export function useFeatureEnabled(featureKey: string): Features { const versionedClient = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS) + const {projectId} = useSource() - if (!cachedFeatureRequest) { - cachedFeatureRequest = fetchFeatures({versionedClient}).pipe( + if (!cachedFeatureRequest.get(projectId)) { + const features = fetchFeatures({versionedClient}).pipe( shareReplay(), catchError((error) => { console.error(error) @@ -43,11 +45,12 @@ export function useFeatureEnabled(featureKey: string): Features { return of([]) }), ) + cachedFeatureRequest.set(projectId, features) } const featureInfo = useMemoObservable( () => - cachedFeatureRequest.pipe( + (cachedFeatureRequest.get(projectId) || of([])).pipe( map((features = []) => ({ isLoading: false, enabled: Boolean(features?.includes(featureKey)), @@ -59,7 +62,7 @@ export function useFeatureEnabled(featureKey: string): Features { return of({isLoading: false, enabled: true, features: []}) }), ), - [featureKey], + [featureKey, projectId], INITIAL_LOADING_STATE, )