From 9da634e8c151174f687b8d5f893601c2ab2f7e37 Mon Sep 17 00:00:00 2001 From: Jordan Lawrence Date: Fri, 30 Aug 2024 11:12:42 +0100 Subject: [PATCH] chore(core): single addon dataset context (#7432) --- .../addonDataset/AddonDatasetProvider.tsx | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/sanity/src/core/studio/addonDataset/AddonDatasetProvider.tsx b/packages/sanity/src/core/studio/addonDataset/AddonDatasetProvider.tsx index eba4aa6c528..9b89eaae662 100644 --- a/packages/sanity/src/core/studio/addonDataset/AddonDatasetProvider.tsx +++ b/packages/sanity/src/core/studio/addonDataset/AddonDatasetProvider.tsx @@ -1,5 +1,5 @@ import {type SanityClient} from '@sanity/client' -import {useCallback, useEffect, useMemo, useState} from 'react' +import {useCallback, useContext, useEffect, useMemo, useState} from 'react' import {AddonDatasetContext} from 'sanity/_singletons' import {useClient} from '../../hooks' @@ -13,13 +13,7 @@ interface AddonDatasetSetupProviderProps { children: React.ReactNode } -/** - * This provider sets the addon dataset client, currently called `comments` dataset. - * It also exposes a `createAddonDataset` function that can be used to create the addon dataset if it does not exist. - * @beta - * @hidden - */ -export function AddonDatasetProvider(props: AddonDatasetSetupProviderProps) { +function AddonDatasetProviderInner(props: AddonDatasetSetupProviderProps) { const {children} = props const {dataset, projectId} = useWorkspace() const originalClient = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS) @@ -134,3 +128,16 @@ export function AddonDatasetProvider(props: AddonDatasetSetupProviderProps) { return {children} } + +/** + * This provider sets the addon dataset client, currently called `comments` dataset. + * It also exposes a `createAddonDataset` function that can be used to create the addon dataset if it does not exist. + * @beta + * @hidden + */ +export function AddonDatasetProvider(props: AddonDatasetSetupProviderProps) { + const context = useContext(AddonDatasetContext) + // Avoid mounting the provider if it's already provided by a parent + if (context) return props.children + return +}