Skip to content

Commit

Permalink
chore(core): rename commentSetup to addonDataset
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Feb 22, 2024
1 parent 033bf37 commit 5b65ebc
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 65 deletions.
1 change: 0 additions & 1 deletion packages/sanity/src/core/studio/addOnDataset/index.ts

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions packages/sanity/src/core/studio/addOnDataset/setup/index.ts

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {createContext} from 'react'

import {type AddonDatasetContextValue} from './types'

/**
* @beta
* @hidden
*/
export const AddonDatasetContext = createContext<AddonDatasetContextValue | null>(null)
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -54,7 +54,7 @@ export function CommentsSetupProvider(props: CommentsSetupProviderProps) {
[originalClient, projectId],
)

const handleRunSetup = useCallback(async (): Promise<SanityClient | null> => {
const handleCreateAddonDataset = useCallback(async (): Promise<SanityClient | null> => {
setIsRunningSetup(true)

// Before running the setup, we check if the addon dataset already exists.
Expand Down Expand Up @@ -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 <CommentsSetupContext.Provider value={ctxValue}>{children}</CommentsSetupContext.Provider>
return <AddonDatasetContext.Provider value={ctxValue}>{children}</AddonDatasetContext.Provider>
}
4 changes: 4 additions & 0 deletions packages/sanity/src/core/studio/addonDataset/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './AddonDatasetContext'
export * from './AddonDatasetProvider'
export * from './types'
export * from './useAddonDataset'
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -13,5 +13,5 @@ export interface CommentsSetupContextValue {
/**
* Function to create the addon dataset if it does not exist.
*/
runSetup: () => Promise<SanityClient | null>
createAddonDataset: () => Promise<SanityClient | null>
}
18 changes: 18 additions & 0 deletions packages/sanity/src/core/studio/addonDataset/useAddonDataset.ts
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion packages/sanity/src/core/studio/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './activeWorkspaceMatcher'
export * from './addOnDataset'
export * from './addonDataset'
export * from './colorScheme'
export * from './components'
export * from './renderStudio'
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -7,7 +7,7 @@ export function CommentsStudioLayout(props: LayoutProps) {
const {enabled, isLoading} = useFeatureEnabled('studioComments')

return (
<CommentsSetupProvider>
<AddonDatasetProvider>
<CommentsOnboardingProvider>
<ConditionalWrapper
condition={!enabled && !isLoading}
Expand All @@ -17,6 +17,6 @@ export function CommentsStudioLayout(props: LayoutProps) {
{props.renderDefault(props)}
</ConditionalWrapper>
</CommentsOnboardingProvider>
</CommentsSetupProvider>
</AddonDatasetProvider>
)
}
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -23,7 +23,7 @@ export default function CommentsProviderStory() {
const _mode = useSelect('_mode', MODES) || ('default' as keyof typeof MODES)

return (
<CommentsSetupProvider>
<AddonDatasetProvider>
<CommentsEnabledProvider documentType={_type} documentId={_id}>
<CommentsProvider documentType={_type} documentId={_id}>
<ConditionalWrapper
Expand All @@ -35,7 +35,7 @@ export default function CommentsProviderStory() {
</ConditionalWrapper>
</CommentsProvider>
</CommentsEnabledProvider>
</CommentsSetupProvider>
</AddonDatasetProvider>
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {orderBy} from 'lodash'
import {memo, type ReactNode, useCallback, useMemo, useState} from 'react'
import {
getPublishedId,
useCommentsSetup,
useAddonDataset,
useCurrentUser,
useEditState,
useSchema,
Expand Down Expand Up @@ -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<CommentStatus>('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)
Expand Down Expand Up @@ -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
Expand All @@ -226,7 +226,7 @@ export const CommentsProvider = memo(function CommentsProvider(props: CommentsPr
workspaceName,
getThreadLength,
getComment,
runSetup,
createAddonDataset,
handleOnCreate,
handleOnCreateError,
handleOnEdit,
Expand Down
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: () => Promise<SanityClient | null>
createAddonDataset: () => Promise<SanityClient | null>
workspace: string
}

Expand All @@ -44,7 +44,7 @@ export async function createOperation(props: CreateOperationProps): Promise<void
onCreate,
onCreateError,
projectId,
runSetup,
createAddonDataset,
workspace,
} = props

Expand Down Expand Up @@ -118,7 +118,7 @@ export async function createOperation(props: CreateOperationProps): Promise<void
// comment to create.
if (!client) {
try {
const newAddonClient = await runSetup()
const newAddonClient = await createAddonDataset()
if (!newAddonClient) {
throw new Error('Failed to create addon dataset client')
}
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: () => Promise<SanityClient | null>
createAddonDataset: () => Promise<SanityClient | null>
schemaType: SchemaType | undefined
workspace: string
}
Expand All @@ -60,7 +60,7 @@ export function useCommentOperations(
onRemove,
onUpdate,
projectId,
runSetup,
createAddonDataset,
workspace,
} = opts

Expand Down Expand Up @@ -102,7 +102,7 @@ export function useCommentOperations(
onCreate,
onCreateError,
projectId,
runSetup,
createAddonDataset,
workspace,
})
},
Expand All @@ -119,7 +119,7 @@ export function useCommentOperations(
onCreate,
onCreateError,
projectId,
runSetup,
createAddonDataset,
workspace,
],
)
Expand Down

0 comments on commit 5b65ebc

Please sign in to comment.