@@ -2,7 +2,7 @@ import {type SanityDocument} from '@sanity/client'
2
2
import { type CurrentUser , type SchemaType } from '@sanity/types'
3
3
4
4
import { isTextSelectionComment } from '../helpers'
5
- import { type CommentDocument , type CommentThreadItem } from '../types'
5
+ import { type CommentDocument , type CommentsType , type CommentThreadItem } from '../types'
6
6
import { buildCommentBreadcrumbs } from './buildCommentBreadcrumbs'
7
7
8
8
const EMPTY_ARRAY : [ ] = [ ]
@@ -12,6 +12,7 @@ interface BuildCommentThreadItemsProps {
12
12
currentUser : CurrentUser
13
13
documentValue : Partial < SanityDocument > | null
14
14
schemaType : SchemaType
15
+ type : CommentsType
15
16
}
16
17
17
18
/**
@@ -21,55 +22,85 @@ interface BuildCommentThreadItemsProps {
21
22
* returned array.
22
23
*/
23
24
export function buildCommentThreadItems ( props : BuildCommentThreadItemsProps ) : CommentThreadItem [ ] {
24
- const { comments, currentUser, documentValue, schemaType} = props
25
+ const { comments, currentUser, documentValue, schemaType, type } = props
25
26
const parentComments = comments ?. filter ( ( c ) => ! c . parentCommentId )
26
27
27
- const items = parentComments . map ( ( parentComment ) => {
28
- const crumbs = buildCommentBreadcrumbs ( {
29
- currentUser,
30
- documentValue,
31
- fieldPath : parentComment . target . path . field ,
32
- schemaType,
28
+ // If the comments are "task" comments, just group them together as thread items
29
+ // without any validation of the comments.
30
+ if ( type === 'task' ) {
31
+ const taskCommentItems = parentComments . map ( ( parentComment ) => {
32
+ const replies = comments ?. filter ( ( r ) => r . parentCommentId === parentComment . _id )
33
+ const commentsCount = [ parentComment , ...replies ] . length
34
+ const hasReferencedValue = false
35
+
36
+ const item : CommentThreadItem = {
37
+ commentsCount,
38
+ parentComment,
39
+ replies,
40
+ threadId : parentComment . threadId ,
41
+ hasReferencedValue,
42
+ breadcrumbs : EMPTY_ARRAY ,
43
+ fieldPath : '' ,
44
+ }
45
+
46
+ return item
33
47
} )
34
48
35
- // NOTE: Keep this code commented out for now as we might want to use it later.
36
- let hasTextSelection = false
37
-
38
- // If the comment is a text selection comment, we need to make sure that
39
- // we can successfully build a range decoration selection from it.
40
- if ( isTextSelectionComment ( parentComment ) ) {
41
- hasTextSelection = Boolean (
42
- parentComment . target . path . selection &&
43
- parentComment . target . path . selection . value . some ( ( v ) => v . text ) ,
44
- )
45
- }
46
-
47
- // Check if the comment has an invalid breadcrumb. The breadcrumbs can be invalid if:
48
- // - The field is hidden by conditional fields
49
- // - The field is not found in the schema type
50
- // - The field is not found in the document value (array items only)
51
- const hasInvalidBreadcrumb = crumbs . some ( ( bc ) => bc . invalid )
52
-
53
- // If the comment has an invalid breadcrumb or selection, we will omit it from the list.
54
- if ( hasInvalidBreadcrumb ) return undefined
55
-
56
- const replies = comments ?. filter ( ( r ) => r . parentCommentId === parentComment . _id )
57
- const commentsCount = [ parentComment , ...replies ] . length
58
- const hasReferencedValue = hasTextSelection
59
-
60
- const item : CommentThreadItem = {
61
- breadcrumbs : crumbs ,
62
- commentsCount,
63
- fieldPath : parentComment . target . path . field ,
64
- parentComment,
65
- replies,
66
- threadId : parentComment . threadId ,
67
- hasReferencedValue,
68
- }
69
-
70
- return item
71
- } )
72
-
73
- // We use the `Boolean` function to filter out any `undefined` items from the array.
74
- return items . filter ( Boolean ) as CommentThreadItem [ ]
49
+ return taskCommentItems
50
+ }
51
+
52
+ // If the comments are "field" comments, we want to validate them against
53
+ // the document value and schema type.
54
+ if ( type === 'field' ) {
55
+ const fieldCommentItems = parentComments . map ( ( parentComment ) => {
56
+ const crumbs = buildCommentBreadcrumbs ( {
57
+ currentUser,
58
+ documentValue,
59
+ fieldPath : parentComment . target . path ?. field || '' ,
60
+ schemaType,
61
+ } )
62
+
63
+ // NOTE: Keep this code commented out for now as we might want to use it later.
64
+ let hasTextSelection = false
65
+
66
+ // If the comment is a text selection comment, we need to make sure that
67
+ // we can successfully build a range decoration selection from it.
68
+ if ( isTextSelectionComment ( parentComment ) ) {
69
+ hasTextSelection = Boolean (
70
+ parentComment . target . path ?. selection &&
71
+ parentComment . target . path . selection . value . some ( ( v ) => v . text ) ,
72
+ )
73
+ }
74
+
75
+ // Check if the comment has an invalid breadcrumb. The breadcrumbs can be invalid if:
76
+ // - The field is hidden by conditional fields
77
+ // - The field is not found in the schema type
78
+ // - The field is not found in the document value (array items only)
79
+ const hasInvalidBreadcrumb = crumbs . some ( ( bc ) => bc . invalid )
80
+
81
+ // If the comment has an invalid breadcrumb or selection, we will omit it from the list.
82
+ if ( hasInvalidBreadcrumb ) return undefined
83
+
84
+ const replies = comments ?. filter ( ( r ) => r . parentCommentId === parentComment . _id )
85
+ const commentsCount = [ parentComment , ...replies ] . length
86
+ const hasReferencedValue = hasTextSelection
87
+
88
+ const item : CommentThreadItem = {
89
+ breadcrumbs : crumbs ,
90
+ commentsCount,
91
+ fieldPath : parentComment . target . path ?. field || '' ,
92
+ parentComment,
93
+ replies,
94
+ threadId : parentComment . threadId ,
95
+ hasReferencedValue,
96
+ }
97
+
98
+ return item
99
+ } )
100
+
101
+ // We use the `Boolean` function to filter out any `undefined` items from the array.
102
+ return fieldCommentItems . filter ( Boolean ) as CommentThreadItem [ ]
103
+ }
104
+
105
+ return EMPTY_ARRAY
75
106
}
0 commit comments