-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(comments): use
document.components.layout
and rename components
- Loading branch information
1 parent
735732f
commit 89aad79
Showing
13 changed files
with
107 additions
and
31 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
packages/sanity/src/desk/comments/plugin/document-layout/CommentsDocumentLayout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
1 change: 1 addition & 0 deletions
1
packages/sanity/src/desk/comments/plugin/document-layout/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './CommentsDocumentLayout' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from './CommentField' | ||
export * from './CommentsField' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/sanity/src/desk/comments/plugin/studio-layout/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './CommentsStudioLayout' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/sanity/src/desk/comments/src/hooks/useResolveCommentsEnabled.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |