Skip to content

Commit

Permalink
feat(comments): use document.components.layout and rename components
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanwikner committed Dec 8, 2023
1 parent 735732f commit 89aad79
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, {useCallback} from 'react'
import {
CommentsEnabledProvider,
CommentsProvider,
CommentsSelectedPathProvider,
useCommentsEnabled,
} from '../../src'
import {useDocumentPane} from '../../..'
import {COMMENTS_INSPECTOR_NAME} from '../../../panes/document/constants'
import {DocumentLayoutProps} from 'sanity'

export function CommentsDocumentLayout(props: DocumentLayoutProps) {
const {documentId, documentType} = props

return (
<CommentsEnabledProvider documentId={documentId} documentType={documentType}>
<CommentsDocumentLayoutInner {...props} />
</CommentsEnabledProvider>
)
}

function CommentsDocumentLayoutInner(props: DocumentLayoutProps) {
const {documentId, documentType} = props
const commentsEnabled = useCommentsEnabled()
const {openInspector, inspector} = useDocumentPane()

const handleOpenCommentsInspector = useCallback(() => {
if (inspector?.name === COMMENTS_INSPECTOR_NAME) return

openInspector(COMMENTS_INSPECTOR_NAME)
}, [inspector?.name, openInspector])

// If comments are not enabled, render the default document layout
if (!commentsEnabled) {
return props.renderDefault(props)
}

return (
<CommentsEnabledProvider documentId={documentId} documentType={documentType}>
<CommentsProvider
documentId={documentId}
documentType={documentType}
isCommentsOpen={inspector?.name === COMMENTS_INSPECTOR_NAME}
onCommentsOpen={handleOpenCommentsInspector}
>
<CommentsSelectedPathProvider>{props.renderDefault(props)}</CommentsSelectedPathProvider>
</CommentsProvider>
</CommentsEnabledProvider>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CommentsDocumentLayout'
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
CommentCreatePayload,
useCommentsSelectedPath,
} from '../../src'
import {CommentFieldButton} from './CommentFieldButton'
import {CommentsFieldButton} from './CommentsFieldButton'
import {FieldProps, getSchemaTypeTitle, useCurrentUser} from 'sanity'

const HIGHLIGHT_BLOCK_VARIANTS: Variants = {
Expand All @@ -28,7 +28,7 @@ const HIGHLIGHT_BLOCK_VARIANTS: Variants = {
},
}

export function CommentField(props: FieldProps) {
export function CommentsField(props: FieldProps) {
const isEnabled = useCommentsEnabled()

if (isEnabled) return <CommentFieldInner {...props} />
Expand Down Expand Up @@ -233,7 +233,7 @@ function CommentFieldInner(props: FieldProps) {
const internalComments: FieldProps['__internal_comments'] = useMemo(
() => ({
button: currentUser && (
<CommentFieldButton
<CommentsFieldButton
count={Number(count)}
currentUser={currentUser}
fieldTitle={fieldTitle}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const ContentStack = styled(Stack)`
width: 320px;
`

interface CommentFieldButtonProps {
interface CommentsFieldButtonProps {
count: number
currentUser: CurrentUser
fieldTitle: string
Expand All @@ -48,7 +48,7 @@ interface CommentFieldButtonProps {
value: CommentMessage
}

export function CommentFieldButton(props: CommentFieldButtonProps) {
export function CommentsFieldButton(props: CommentsFieldButtonProps) {
const {
count,
currentUser,
Expand Down
2 changes: 1 addition & 1 deletion packages/sanity/src/desk/comments/plugin/field/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './CommentField'
export * from './CommentsField'
15 changes: 11 additions & 4 deletions packages/sanity/src/desk/comments/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import {commentsInspector} from './inspector'
import {CommentField} from './field'
import {CommentsLayout} from './layout'
import {CommentsField} from './field'
import {CommentsDocumentLayout} from './document-layout'
import {CommentsStudioLayout} from './studio-layout'
import {definePlugin} from 'sanity'

export const comments = definePlugin({
name: 'sanity/desk/comments',

document: {
inspectors: [commentsInspector],
components: {
layout: CommentsDocumentLayout,
},
},

form: {
components: {
field: CommentField,
field: CommentsField,
},
},

studio: {
components: {
layout: CommentsLayout,
layout: CommentsStudioLayout,
},
},
})
1 change: 0 additions & 1 deletion packages/sanity/src/desk/comments/plugin/layout/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import {CommentsOnboardingProvider, CommentsSetupProvider} from '../../src'
import {LayoutProps} from 'sanity'

export function CommentsLayout(props: LayoutProps) {
export function CommentsStudioLayout(props: LayoutProps) {
return (
<CommentsSetupProvider>
<CommentsOnboardingProvider>{props.renderDefault(props)}</CommentsOnboardingProvider>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CommentsStudioLayout'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useMemo} from 'react'
import React from 'react'
import {useResolveCommentsEnabled} from '../../hooks'
import {CommentsEnabledContext} from './CommentsEnabledContext'
import {useFeatureEnabled, useSource, getPublishedId} from 'sanity'

interface CommentsEnabledProviderProps {
children: React.ReactNode
Expand All @@ -13,21 +13,7 @@ export const CommentsEnabledProvider = React.memo(function CommentsEnabledProvid
) {
const {children, documentId, documentType} = props

// Check if the projects plan has the feature enabled
const {enabled: featureEnabled, isLoading} = useFeatureEnabled('studioComments')

const {enabled} = useSource().document.unstable_comments

// Check if the feature is enabled for the current document in the config
const enabledFromConfig = useMemo(
() => enabled({documentType, documentId: getPublishedId(documentId)}),
[documentId, documentType, enabled],
)

const isEnabled = useMemo((): boolean => {
if (isLoading || !featureEnabled || !enabledFromConfig) return false
return true
}, [enabledFromConfig, featureEnabled, isLoading])
const isEnabled = useResolveCommentsEnabled(documentId, documentType)

return (
<CommentsEnabledContext.Provider value={isEnabled}>{children}</CommentsEnabledContext.Provider>
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/desk/comments/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './useCommentsSetup'
export * from './useCommentsEnabled'
export * from './useCommentsOnboarding'
export * from './useCommentsSelectedPath'
export * from './useResolveCommentsEnabled'
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {CommentsEnabledContext} from '../context/enabled'
* if comments is enabled for the current document in the config API.
*/
export function useCommentsEnabled(): boolean {
const enabled = useContext(CommentsEnabledContext)
const ctx = useContext(CommentsEnabledContext)

return Boolean(enabled)
if (ctx === null) {
throw new Error('useCommentsEnabled: missing context value')
}

return ctx
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {useMemo} from 'react'
import {getPublishedId, useFeatureEnabled, useSource} from 'sanity'

/**
* @internal
* A hook that resolves if comments are enabled for the current document and document type
* and if the feature is enabled for the current project.
*/
export function useResolveCommentsEnabled(documentId: string, documentType: string): boolean {
// Check if the projects plan has the feature enabled
const {enabled: featureEnabled, isLoading} = useFeatureEnabled('studioComments')

const {enabled} = useSource().document.unstable_comments

// Check if the feature is enabled for the current document in the config
const enabledFromConfig = useMemo(
() => enabled({documentType, documentId: getPublishedId(documentId)}),
[documentId, documentType, enabled],
)

const isEnabled = useMemo((): boolean => {
if (isLoading || !featureEnabled || !enabledFromConfig) return false
return true
}, [enabledFromConfig, featureEnabled, isLoading])

return isEnabled
}

0 comments on commit 89aad79

Please sign in to comment.