Skip to content

Commit

Permalink
chore(tasks): move CommentsSetupProvider to core
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Feb 22, 2024
1 parent f3fd972 commit 9bbca35
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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
*/
Expand Down Expand Up @@ -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<SanityClient | null> => {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './CommentsSetupContext'
export * from './CommentsSetupProvider'
export * from './types'
export * from './useCommentsSetup'
17 changes: 17 additions & 0 deletions packages/sanity/src/core/studio/commentsSetup/types.ts
Original file line number Diff line number Diff line change
@@ -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<SanityClient | null>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {useContext} from 'react'

import {CommentsSetupContext, type CommentsSetupContextValue} from '../context'
import {CommentsSetupContext} from './CommentsSetupContext'
import {type CommentsSetupContextValue} from './types'

/**
* @beta
* @hidden
*/
export function useCommentsSetup(): CommentsSetupContextValue {
const ctx = useContext(CommentsSetupContext)

Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/core/studio/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './activeWorkspaceMatcher'
export * from './colorScheme'
export * from './commentsSetup'
export * from './components'
export * from './renderStudio'
export * from './source'
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export * from './enabled'
export * from './intent'
export * from './onboarding'
export * from './selected-path'
export * from './setup'
export * from './upsell'
13 changes: 0 additions & 13 deletions packages/sanity/src/structure/comments/src/context/setup/types.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/sanity/src/structure/comments/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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 './useResolveCommentsEnabled'
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface CreateOperationProps {
onCreate?: (comment: CommentPostPayload) => void
onCreateError: (id: string, error: Error) => void
projectId: string
runSetup: (comment: CommentPostPayload) => Promise<void>
runSetup: () => Promise<SanityClient | null>
workspace: string
}

Expand Down Expand Up @@ -118,7 +118,11 @@ export async function createOperation(props: CreateOperationProps): Promise<void
// comment to create.
if (!client) {
try {
await runSetup(nextComment)
const newAddonClient = await runSetup()
if (!newAddonClient) {
throw new Error('Failed to create addon dataset client')
}
await newAddonClient.create(nextComment)
} catch (err) {
onCreateError?.(nextComment._id, err)
throw err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface CommentOperationsHookOptions {
onRemove?: (id: string) => void
onUpdate?: (id: string, comment: Partial<CommentCreatePayload>) => void
projectId: string
runSetup: (comment: CommentPostPayload) => Promise<void>
runSetup: () => Promise<SanityClient | null>
schemaType: SchemaType | undefined
workspace: string
}
Expand Down

0 comments on commit 9bbca35

Please sign in to comment.