diff --git a/packages/sanity/src/core/studio/addOnDataset/index.ts b/packages/sanity/src/core/studio/addOnDataset/index.ts deleted file mode 100644 index 5a774a6b36e..00000000000 --- a/packages/sanity/src/core/studio/addOnDataset/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './setup' diff --git a/packages/sanity/src/core/studio/addOnDataset/setup/CommentsSetupContext.ts b/packages/sanity/src/core/studio/addOnDataset/setup/CommentsSetupContext.ts deleted file mode 100644 index 76d1ddd6d7c..00000000000 --- a/packages/sanity/src/core/studio/addOnDataset/setup/CommentsSetupContext.ts +++ /dev/null @@ -1,9 +0,0 @@ -import {createContext} from 'react' - -import {type CommentsSetupContextValue} from './types' - -/** - * @beta - * @hidden - */ -export const CommentsSetupContext = createContext(null) diff --git a/packages/sanity/src/core/studio/addOnDataset/setup/index.ts b/packages/sanity/src/core/studio/addOnDataset/setup/index.ts deleted file mode 100644 index e14ddd5a7e9..00000000000 --- a/packages/sanity/src/core/studio/addOnDataset/setup/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './CommentsSetupContext' -export * from './CommentsSetupProvider' -export * from './types' -export * from './useCommentsSetup' diff --git a/packages/sanity/src/core/studio/addOnDataset/setup/useCommentsSetup.ts b/packages/sanity/src/core/studio/addOnDataset/setup/useCommentsSetup.ts deleted file mode 100644 index 59c814cf52e..00000000000 --- a/packages/sanity/src/core/studio/addOnDataset/setup/useCommentsSetup.ts +++ /dev/null @@ -1,18 +0,0 @@ -import {useContext} from 'react' - -import {CommentsSetupContext} from './CommentsSetupContext' -import {type CommentsSetupContextValue} from './types' - -/** - * @beta - * @hidden - */ -export function useCommentsSetup(): CommentsSetupContextValue { - const ctx = useContext(CommentsSetupContext) - - if (!ctx) { - throw new Error('useCommentsSetup: missing context value') - } - - return ctx -} diff --git a/packages/sanity/src/core/studio/addonDataset/AddonDatasetContext.ts b/packages/sanity/src/core/studio/addonDataset/AddonDatasetContext.ts new file mode 100644 index 00000000000..0515483cfd9 --- /dev/null +++ b/packages/sanity/src/core/studio/addonDataset/AddonDatasetContext.ts @@ -0,0 +1,9 @@ +import {createContext} from 'react' + +import {type AddonDatasetContextValue} from './types' + +/** + * @beta + * @hidden + */ +export const AddonDatasetContext = createContext(null) diff --git a/packages/sanity/src/core/studio/addOnDataset/setup/CommentsSetupProvider.tsx b/packages/sanity/src/core/studio/addonDataset/AddonDatasetProvider.tsx similarity index 82% rename from packages/sanity/src/core/studio/addOnDataset/setup/CommentsSetupProvider.tsx rename to packages/sanity/src/core/studio/addonDataset/AddonDatasetProvider.tsx index 8150987ca97..7aa68fffafc 100644 --- a/packages/sanity/src/core/studio/addOnDataset/setup/CommentsSetupProvider.tsx +++ b/packages/sanity/src/core/studio/addonDataset/AddonDatasetProvider.tsx @@ -1,25 +1,25 @@ import {type SanityClient} from '@sanity/client' import {useCallback, useEffect, useMemo, useState} from 'react' -import {useClient} from '../../../hooks' -import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../../../studioClient' -import {useWorkspace} from '../../workspace' -import {CommentsSetupContext} from './CommentsSetupContext' -import {type CommentsSetupContextValue} from './types' +import {useClient} from '../../hooks' +import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../../studioClient' +import {useWorkspace} from '../workspace' +import {AddonDatasetContext} from './AddonDatasetContext' +import {type AddonDatasetContextValue} from './types' const API_VERSION = 'v2023-11-13' -interface CommentsSetupProviderProps { +interface AddonDatasetSetupProviderProps { children: React.ReactNode } /** * This provider sets the addon dataset client, currently called `comments` dataset. - * It also exposes a `runSetup` function that can be used to create the addon dataset if it does not exist. + * It also exposes a `createAddonDataset` function that can be used to create the addon dataset if it does not exist. * @beta * @hidden */ -export function CommentsSetupProvider(props: CommentsSetupProviderProps) { +export function AddonDatasetProvider(props: AddonDatasetSetupProviderProps) { const {children} = props const {dataset, projectId} = useWorkspace() const originalClient = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS) @@ -54,7 +54,7 @@ export function CommentsSetupProvider(props: CommentsSetupProviderProps) { [originalClient, projectId], ) - const handleRunSetup = useCallback(async (): Promise => { + const handleCreateAddonDataset = useCallback(async (): Promise => { setIsRunningSetup(true) // Before running the setup, we check if the addon dataset already exists. @@ -118,13 +118,13 @@ export function CommentsSetupProvider(props: CommentsSetupProviderProps) { }, [getAddonDatasetName, handleCreateClient]) const ctxValue = useMemo( - (): CommentsSetupContextValue => ({ + (): AddonDatasetContextValue => ({ client: addonDatasetClient, - runSetup: handleRunSetup, + createAddonDataset: handleCreateAddonDataset, isRunningSetup, }), - [addonDatasetClient, handleRunSetup, isRunningSetup], + [addonDatasetClient, handleCreateAddonDataset, isRunningSetup], ) - return {children} + return {children} } diff --git a/packages/sanity/src/core/studio/addonDataset/index.ts b/packages/sanity/src/core/studio/addonDataset/index.ts new file mode 100644 index 00000000000..17ef96d5a63 --- /dev/null +++ b/packages/sanity/src/core/studio/addonDataset/index.ts @@ -0,0 +1,4 @@ +export * from './AddonDatasetContext' +export * from './AddonDatasetProvider' +export * from './types' +export * from './useAddonDataset' diff --git a/packages/sanity/src/core/studio/addOnDataset/setup/types.ts b/packages/sanity/src/core/studio/addonDataset/types.ts similarity index 74% rename from packages/sanity/src/core/studio/addOnDataset/setup/types.ts rename to packages/sanity/src/core/studio/addonDataset/types.ts index fba5ab65202..df7aca812e7 100644 --- a/packages/sanity/src/core/studio/addOnDataset/setup/types.ts +++ b/packages/sanity/src/core/studio/addonDataset/types.ts @@ -4,7 +4,7 @@ import {type SanityClient} from '@sanity/client' * @beta * @hidden */ -export interface CommentsSetupContextValue { +export interface AddonDatasetContextValue { /** * Addon dataset client, currently called `comments` dataset. */ @@ -13,5 +13,5 @@ export interface CommentsSetupContextValue { /** * Function to create the addon dataset if it does not exist. */ - runSetup: () => Promise + createAddonDataset: () => Promise } diff --git a/packages/sanity/src/core/studio/addonDataset/useAddonDataset.ts b/packages/sanity/src/core/studio/addonDataset/useAddonDataset.ts new file mode 100644 index 00000000000..474bb8ee4eb --- /dev/null +++ b/packages/sanity/src/core/studio/addonDataset/useAddonDataset.ts @@ -0,0 +1,18 @@ +import {useContext} from 'react' + +import {AddonDatasetContext} from './AddonDatasetContext' +import {type AddonDatasetContextValue} from './types' + +/** + * @beta + * @hidden + */ +export function useAddonDataset(): AddonDatasetContextValue { + const ctx = useContext(AddonDatasetContext) + + if (!ctx) { + throw new Error('useAddonDataset: missing context value') + } + + return ctx +} diff --git a/packages/sanity/src/core/studio/index.ts b/packages/sanity/src/core/studio/index.ts index 98275cd5ec1..8b7fe85e746 100644 --- a/packages/sanity/src/core/studio/index.ts +++ b/packages/sanity/src/core/studio/index.ts @@ -1,5 +1,5 @@ export * from './activeWorkspaceMatcher' -export * from './addOnDataset' +export * from './addonDataset' export * from './colorScheme' export * from './components' export * from './renderStudio' diff --git a/packages/sanity/src/structure/comments/plugin/studio-layout/CommentsStudioLayout.tsx b/packages/sanity/src/structure/comments/plugin/studio-layout/CommentsStudioLayout.tsx index 467be9c2b88..554496eb42d 100644 --- a/packages/sanity/src/structure/comments/plugin/studio-layout/CommentsStudioLayout.tsx +++ b/packages/sanity/src/structure/comments/plugin/studio-layout/CommentsStudioLayout.tsx @@ -1,4 +1,4 @@ -import {CommentsSetupProvider, type LayoutProps, useFeatureEnabled} from 'sanity' +import {AddonDatasetProvider, type LayoutProps, useFeatureEnabled} from 'sanity' import {ConditionalWrapper} from '../../../../ui-components/conditionalWrapper' import {CommentsOnboardingProvider, CommentsUpsellProvider} from '../../src' @@ -7,7 +7,7 @@ export function CommentsStudioLayout(props: LayoutProps) { const {enabled, isLoading} = useFeatureEnabled('studioComments') return ( - + - + ) } diff --git a/packages/sanity/src/structure/comments/src/__workshop__/CommentsProviderStory.tsx b/packages/sanity/src/structure/comments/src/__workshop__/CommentsProviderStory.tsx index 07d8c6d8773..91f15f129fb 100644 --- a/packages/sanity/src/structure/comments/src/__workshop__/CommentsProviderStory.tsx +++ b/packages/sanity/src/structure/comments/src/__workshop__/CommentsProviderStory.tsx @@ -1,7 +1,7 @@ /* eslint-disable react/jsx-handler-names */ import {useSelect, useString} from '@sanity/ui-workshop' import {useMemo} from 'react' -import {CommentsSetupProvider, useCurrentUser} from 'sanity' +import {AddonDatasetProvider, useCurrentUser} from 'sanity' import {ConditionalWrapper} from '../../../../ui-components/conditionalWrapper' import {CommentsList, CommentsUpsellPanel} from '../components' @@ -23,7 +23,7 @@ export default function CommentsProviderStory() { const _mode = useSelect('_mode', MODES) || ('default' as keyof typeof MODES) return ( - + - + ) } diff --git a/packages/sanity/src/structure/comments/src/context/comments/CommentsProvider.tsx b/packages/sanity/src/structure/comments/src/context/comments/CommentsProvider.tsx index d4818d9fe88..a7091c41910 100644 --- a/packages/sanity/src/structure/comments/src/context/comments/CommentsProvider.tsx +++ b/packages/sanity/src/structure/comments/src/context/comments/CommentsProvider.tsx @@ -2,7 +2,7 @@ import {orderBy} from 'lodash' import {memo, type ReactNode, useCallback, useMemo, useState} from 'react' import { getPublishedId, - useCommentsSetup, + useAddonDataset, useCurrentUser, useEditState, useSchema, @@ -59,7 +59,7 @@ export const CommentsProvider = memo(function CommentsProvider(props: CommentsPr const {children, documentId, documentType, isCommentsOpen, onCommentsOpen} = props const commentsEnabled = useCommentsEnabled() const [status, setStatus] = useState('open') - const {client, runSetup, isRunningSetup} = useCommentsSetup() + const {client, createAddonDataset, isRunningSetup} = useAddonDataset() const publishedId = getPublishedId(documentId) const editState = useEditState(publishedId, documentType, 'low') const schemaType = useSchema().get(documentType) @@ -203,7 +203,7 @@ export const CommentsProvider = memo(function CommentsProvider(props: CommentsPr // This function runs when the first comment creation is executed. // It is used to create the addon dataset and configure a client for // the addon dataset. - runSetup, + createAddonDataset, // The following callbacks runs when the comment operation are executed. // They are used to update the local state of the comments immediately after // a comment operation has been executed. This is done to avoid waiting for @@ -226,7 +226,7 @@ export const CommentsProvider = memo(function CommentsProvider(props: CommentsPr workspaceName, getThreadLength, getComment, - runSetup, + createAddonDataset, handleOnCreate, handleOnCreateError, handleOnEdit, diff --git a/packages/sanity/src/structure/comments/src/hooks/use-comment-operations/createOperation.ts b/packages/sanity/src/structure/comments/src/hooks/use-comment-operations/createOperation.ts index 8db68772b6c..c8d91055bf6 100644 --- a/packages/sanity/src/structure/comments/src/hooks/use-comment-operations/createOperation.ts +++ b/packages/sanity/src/structure/comments/src/hooks/use-comment-operations/createOperation.ts @@ -25,7 +25,7 @@ interface CreateOperationProps { onCreate?: (comment: CommentPostPayload) => void onCreateError: (id: string, error: Error) => void projectId: string - runSetup: () => Promise + createAddonDataset: () => Promise workspace: string } @@ -44,7 +44,7 @@ export async function createOperation(props: CreateOperationProps): Promise void onUpdate?: (id: string, comment: Partial) => void projectId: string - runSetup: () => Promise + createAddonDataset: () => Promise schemaType: SchemaType | undefined workspace: string } @@ -60,7 +60,7 @@ export function useCommentOperations( onRemove, onUpdate, projectId, - runSetup, + createAddonDataset, workspace, } = opts @@ -102,7 +102,7 @@ export function useCommentOperations( onCreate, onCreateError, projectId, - runSetup, + createAddonDataset, workspace, }) }, @@ -119,7 +119,7 @@ export function useCommentOperations( onCreate, onCreateError, projectId, - runSetup, + createAddonDataset, workspace, ], )