Skip to content

Commit

Permalink
chore(core): single addon dataset context (#7432)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanl17 authored Aug 30, 2024
1 parent 58863af commit 9da634e
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand Down Expand Up @@ -134,3 +128,16 @@ export function AddonDatasetProvider(props: AddonDatasetSetupProviderProps) {

return <AddonDatasetContext.Provider value={ctxValue}>{children}</AddonDatasetContext.Provider>
}

/**
* 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 <AddonDatasetProviderInner {...props} />
}

0 comments on commit 9da634e

Please sign in to comment.