diff --git a/packages/sanity/src/structure/comments/src/context/setup/CommentsSetupContext.ts b/packages/sanity/src/core/studio/commentsSetup/CommentsSetupContext.ts similarity index 100% rename from packages/sanity/src/structure/comments/src/context/setup/CommentsSetupContext.ts rename to packages/sanity/src/core/studio/commentsSetup/CommentsSetupContext.ts diff --git a/packages/sanity/src/structure/comments/src/context/setup/CommentsSetupProvider.tsx b/packages/sanity/src/core/studio/commentsSetup/CommentsSetupProvider.tsx similarity index 56% rename from packages/sanity/src/structure/comments/src/context/setup/CommentsSetupProvider.tsx rename to packages/sanity/src/core/studio/commentsSetup/CommentsSetupProvider.tsx index 86067b0a3d7d..c6182d14cfdb 100644 --- a/packages/sanity/src/structure/comments/src/context/setup/CommentsSetupProvider.tsx +++ b/packages/sanity/src/core/studio/commentsSetup/CommentsSetupProvider.tsx @@ -1,8 +1,9 @@ import {type SanityClient} from '@sanity/client' import {useCallback, useEffect, useMemo, useState} from 'react' -import {DEFAULT_STUDIO_CLIENT_OPTIONS, useClient, useWorkspace} from 'sanity' -import {type CommentPostPayload} from '../../types' +import {useClient} from '../../hooks' +import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../../studioClient' +import {useWorkspace} from '../workspace' import {CommentsSetupContext} from './CommentsSetupContext' import {type CommentsSetupContextValue} from './types' @@ -13,6 +14,8 @@ interface CommentsSetupProviderProps { } /** + * This providers 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. * @beta * @hidden */ @@ -51,60 +54,56 @@ export function CommentsSetupProvider(props: CommentsSetupProviderProps) { [originalClient, projectId], ) - const handleRunSetup = useCallback( - async (comment: CommentPostPayload) => { - setIsRunningSetup(true) - - // Before running the setup, we check if the addon dataset already exists. - // The addon dataset might already exist if another user has already run - // the setup, but the current user has not refreshed the page yet and - // therefore don't have a client for the addon dataset yet. - try { - const addonDatasetName = await getAddonDatasetName() - - if (addonDatasetName) { - const client = handleCreateClient(addonDatasetName) - setAddonDatasetClient(client) - await client.create(comment) - setIsRunningSetup(false) - return - } - } catch (_) { - // If the dataset does not exist we will get an error, but we can ignore - // it since we will create the dataset in the next step. - } + const handleRunSetup = useCallback(async (): Promise => { + setIsRunningSetup(true) + + // Before running the setup, we check if the addon dataset already exists. + // The addon dataset might already exist if another user has already run + // the setup, but the current user has not refreshed the page yet and + // therefore don't have a client for the addon dataset yet. + try { + const addonDatasetName = await getAddonDatasetName() - try { - // 1. Create the addon dataset - const res = await originalClient.withConfig({apiVersion: API_VERSION}).request({ - uri: `/comments/${dataset}/setup`, - method: 'POST', - }) - - const datasetName = res?.datasetName - - // 2. We can't continue if the addon dataset name is not returned - if (!datasetName) { - setIsRunningSetup(false) - return - } - - // 3. Create a client for the addon dataset and set it in the context value - // so that the consumers can use it to execute comment operations and set up - // the real time listener for the addon dataset. - const client = handleCreateClient(datasetName) + if (addonDatasetName) { + const client = handleCreateClient(addonDatasetName) setAddonDatasetClient(client) + setIsRunningSetup(false) + return client + } + } catch (_) { + // If the dataset does not exist we will get an error, but we can ignore + // it since we will create the dataset in the next step. + } + + try { + // 1. Create the addon dataset + const res = await originalClient.withConfig({apiVersion: API_VERSION}).request({ + uri: `/comments/${dataset}/setup`, + method: 'POST', + }) + + const datasetName = res?.datasetName - // 4. Create the comment - await client.create(comment) - } catch (err) { - throw err - } finally { + // 2. We can't continue if the addon dataset name is not returned + if (!datasetName) { setIsRunningSetup(false) + return null } - }, - [dataset, getAddonDatasetName, handleCreateClient, originalClient], - ) + + // 3. Create a client for the addon dataset and set it in the context value + // so that the consumers can use it to execute comment operations and set up + // the real time listener for the addon dataset. + const client = handleCreateClient(datasetName) + setAddonDatasetClient(client) + + // 4. Return the client so that the caller can use it to execute operations + return client + } catch (err) { + throw err + } finally { + setIsRunningSetup(false) + } + }, [dataset, getAddonDatasetName, handleCreateClient, originalClient]) useEffect(() => { // On mount, we check if the addon dataset already exists. If it does, we create diff --git a/packages/sanity/src/structure/comments/src/context/setup/index.ts b/packages/sanity/src/core/studio/commentsSetup/index.ts similarity index 74% rename from packages/sanity/src/structure/comments/src/context/setup/index.ts rename to packages/sanity/src/core/studio/commentsSetup/index.ts index 31dc8c78b076..e14ddd5a7e91 100644 --- a/packages/sanity/src/structure/comments/src/context/setup/index.ts +++ b/packages/sanity/src/core/studio/commentsSetup/index.ts @@ -1,3 +1,4 @@ export * from './CommentsSetupContext' export * from './CommentsSetupProvider' export * from './types' +export * from './useCommentsSetup' diff --git a/packages/sanity/src/core/studio/commentsSetup/types.ts b/packages/sanity/src/core/studio/commentsSetup/types.ts new file mode 100644 index 000000000000..fba5ab652029 --- /dev/null +++ b/packages/sanity/src/core/studio/commentsSetup/types.ts @@ -0,0 +1,17 @@ +import {type SanityClient} from '@sanity/client' + +/** + * @beta + * @hidden + */ +export interface CommentsSetupContextValue { + /** + * Addon dataset client, currently called `comments` dataset. + */ + client: SanityClient | null + isRunningSetup: boolean + /** + * Function to create the addon dataset if it does not exist. + */ + runSetup: () => Promise +} diff --git a/packages/sanity/src/structure/comments/src/hooks/useCommentsSetup.ts b/packages/sanity/src/core/studio/commentsSetup/useCommentsSetup.ts similarity index 67% rename from packages/sanity/src/structure/comments/src/hooks/useCommentsSetup.ts rename to packages/sanity/src/core/studio/commentsSetup/useCommentsSetup.ts index 895b69bc7810..c31e2b7aea96 100644 --- a/packages/sanity/src/structure/comments/src/hooks/useCommentsSetup.ts +++ b/packages/sanity/src/core/studio/commentsSetup/useCommentsSetup.ts @@ -1,6 +1,7 @@ import {useContext} from 'react' -import {CommentsSetupContext, type CommentsSetupContextValue} from '../context' +import {CommentsSetupContext} from './CommentsSetupContext' +import {type CommentsSetupContextValue} from './types' export function useCommentsSetup(): CommentsSetupContextValue { const ctx = useContext(CommentsSetupContext) diff --git a/packages/sanity/src/core/studio/index.ts b/packages/sanity/src/core/studio/index.ts index a9fd9d438f28..518dde556f71 100644 --- a/packages/sanity/src/core/studio/index.ts +++ b/packages/sanity/src/core/studio/index.ts @@ -1,5 +1,6 @@ export * from './activeWorkspaceMatcher' export * from './colorScheme' +export * from './commentsSetup' export * from './components' export * from './renderStudio' export * from './source' 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 8b521228cdd2..467be9c2b88f 100644 --- a/packages/sanity/src/structure/comments/plugin/studio-layout/CommentsStudioLayout.tsx +++ b/packages/sanity/src/structure/comments/plugin/studio-layout/CommentsStudioLayout.tsx @@ -1,7 +1,7 @@ -import {type LayoutProps, useFeatureEnabled} from 'sanity' +import {CommentsSetupProvider, type LayoutProps, useFeatureEnabled} from 'sanity' import {ConditionalWrapper} from '../../../../ui-components/conditionalWrapper' -import {CommentsOnboardingProvider, CommentsSetupProvider, CommentsUpsellProvider} from '../../src' +import {CommentsOnboardingProvider, CommentsUpsellProvider} from '../../src' export function CommentsStudioLayout(props: LayoutProps) { const {enabled, isLoading} = useFeatureEnabled('studioComments') diff --git a/packages/sanity/src/structure/comments/src/__workshop__/CommentsProviderStory.tsx b/packages/sanity/src/structure/comments/src/__workshop__/CommentsProviderStory.tsx index 3571ddecf086..07d8c6d87737 100644 --- a/packages/sanity/src/structure/comments/src/__workshop__/CommentsProviderStory.tsx +++ b/packages/sanity/src/structure/comments/src/__workshop__/CommentsProviderStory.tsx @@ -1,16 +1,11 @@ /* eslint-disable react/jsx-handler-names */ import {useSelect, useString} from '@sanity/ui-workshop' import {useMemo} from 'react' -import {useCurrentUser} from 'sanity' +import {CommentsSetupProvider, useCurrentUser} from 'sanity' import {ConditionalWrapper} from '../../../../ui-components/conditionalWrapper' import {CommentsList, CommentsUpsellPanel} from '../components' -import { - CommentsEnabledProvider, - CommentsProvider, - CommentsSetupProvider, - CommentsUpsellProvider, -} from '../context' +import {CommentsEnabledProvider, CommentsProvider, CommentsUpsellProvider} from '../context' import {useComments, useCommentsUpsell} from '../hooks' import {type CommentsUIMode} from '../types' 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 167df3c8b995..3bdda746f609 100644 --- a/packages/sanity/src/structure/comments/src/context/comments/CommentsProvider.tsx +++ b/packages/sanity/src/structure/comments/src/context/comments/CommentsProvider.tsx @@ -1,13 +1,19 @@ import {orderBy} from 'lodash' import {memo, type ReactNode, useCallback, useMemo, useState} from 'react' -import {getPublishedId, useCurrentUser, useEditState, useSchema, useWorkspace} from 'sanity' +import { + getPublishedId, + useCommentsSetup, + useCurrentUser, + useEditState, + useSchema, + useWorkspace, +} from 'sanity' import { type CommentOperationsHookOptions, type MentionHookOptions, useCommentOperations, useCommentsEnabled, - useCommentsSetup, useMentionOptions, } from '../../hooks' import {useCommentsStore} from '../../store' diff --git a/packages/sanity/src/structure/comments/src/context/index.ts b/packages/sanity/src/structure/comments/src/context/index.ts index e02af159d7b2..999141977686 100644 --- a/packages/sanity/src/structure/comments/src/context/index.ts +++ b/packages/sanity/src/structure/comments/src/context/index.ts @@ -3,5 +3,4 @@ export * from './enabled' export * from './intent' export * from './onboarding' export * from './selected-path' -export * from './setup' export * from './upsell' diff --git a/packages/sanity/src/structure/comments/src/context/setup/types.ts b/packages/sanity/src/structure/comments/src/context/setup/types.ts deleted file mode 100644 index 71435e6d719e..000000000000 --- a/packages/sanity/src/structure/comments/src/context/setup/types.ts +++ /dev/null @@ -1,13 +0,0 @@ -import {type SanityClient} from '@sanity/client' - -import {type CommentPostPayload} from '../../types' - -/** - * @beta - * @hidden - */ -export interface CommentsSetupContextValue { - client: SanityClient | null - isRunningSetup: boolean - runSetup: (comment: CommentPostPayload) => Promise -} diff --git a/packages/sanity/src/structure/comments/src/hooks/index.ts b/packages/sanity/src/structure/comments/src/hooks/index.ts index 3b3cf132d493..741f4f4d0873 100644 --- a/packages/sanity/src/structure/comments/src/hooks/index.ts +++ b/packages/sanity/src/structure/comments/src/hooks/index.ts @@ -1,10 +1,10 @@ +export * from '../../../../core/studio/commentsSetup/useCommentsSetup' export * from './use-comment-operations' export * from './useComments' export * from './useCommentsEnabled' export * from './useCommentsIntent' export * from './useCommentsOnboarding' export * from './useCommentsSelectedPath' -export * from './useCommentsSetup' export * from './useCommentsUpsell' export * from './useMentionOptions' export * from './useResolveCommentsEnabled' 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 56a51d6b307f..8db68772b6cb 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: (comment: CommentPostPayload) => Promise + runSetup: () => Promise workspace: string } @@ -118,7 +118,11 @@ export async function createOperation(props: CreateOperationProps): Promise void onUpdate?: (id: string, comment: Partial) => void projectId: string - runSetup: (comment: CommentPostPayload) => Promise + runSetup: () => Promise schemaType: SchemaType | undefined workspace: string }