@@ -8,9 +8,11 @@ import {
8
8
LoadingBlock ,
9
9
type PatchEvent ,
10
10
type Path ,
11
+ set ,
11
12
type TransactionLogEventWithEffects ,
12
13
useClient ,
13
14
useCurrentUser ,
15
+ useWorkspace ,
14
16
} from 'sanity'
15
17
import styled from 'styled-components'
16
18
@@ -29,6 +31,7 @@ import {
29
31
import { API_VERSION } from '../../constants/API_VERSION'
30
32
import { type TaskDocument } from '../../types'
31
33
import { CurrentWorkspaceProvider } from '../form/CurrentWorkspaceProvider'
34
+ import { getMentionedUsers } from '../form/utils'
32
35
import { type FieldChange , trackFieldChanges } from './helpers/parseTransactions'
33
36
import { EditedAt } from './TaskActivityEditedAt'
34
37
import { TasksActivityCommentInput } from './TasksActivityCommentInput'
@@ -134,39 +137,80 @@ type Activity =
134
137
export function TasksActivityLog ( props : TasksActivityLogProps ) {
135
138
const { value, onChange, path} = props
136
139
const currentUser = useCurrentUser ( )
140
+ const { title : workspaceTitle , basePath} = useWorkspace ( )
137
141
138
142
const { comments, mentionOptions, operation, getComment} = useComments ( )
139
143
const loading = comments . loading
140
144
const taskComments = comments . data . open
141
145
146
+ const handleGetNotificationValue = useCallback (
147
+ ( message : CommentInputProps [ 'value' ] , commentId : string ) => {
148
+ const studioUrl = new URL ( `${ window . location . origin } ${ basePath } /` )
149
+ studioUrl . searchParams . set ( 'sidebar' , 'tasks' )
150
+ studioUrl . searchParams . set ( 'selectedTask' , value ?. _id )
151
+ studioUrl . searchParams . set ( 'viewMode' , 'edit' )
152
+ studioUrl . searchParams . set ( 'commentId' , commentId )
153
+
154
+ const mentionedUsers = getMentionedUsers ( message )
155
+ const subscribers = Array . from ( new Set ( [ ...( value . subscribers || [ ] ) , ...mentionedUsers ] ) )
156
+
157
+ return {
158
+ documentTitle : value . title || 'Sanity task' ,
159
+ url : studioUrl . toString ( ) ,
160
+ workspaceTitle : workspaceTitle ,
161
+ subscribers : subscribers ,
162
+ }
163
+ } ,
164
+ [ basePath , value ?. _id , value . title , workspaceTitle , value . subscribers ] ,
165
+ )
166
+
142
167
const handleCommentCreate = useCallback (
143
168
( message : CommentInputProps [ 'value' ] ) => {
169
+ const commentId = uuid ( )
170
+ const notification = handleGetNotificationValue ( message , commentId )
171
+
144
172
const nextComment : CommentCreatePayload = {
173
+ id : commentId ,
145
174
type : 'task' ,
146
175
message,
147
176
parentCommentId : undefined ,
148
177
reactions : EMPTY_ARRAY ,
149
178
status : 'open' ,
150
179
threadId : uuid ( ) ,
180
+ context : {
181
+ notification,
182
+ } ,
151
183
}
152
184
185
+ onChange ( set ( notification . subscribers , [ 'subscribers' ] ) )
186
+
153
187
operation . create ( nextComment )
154
188
} ,
155
- [ operation ] ,
189
+ [ operation , handleGetNotificationValue , onChange ] ,
156
190
)
157
191
158
192
const handleCommentReply = useCallback (
159
193
( nextComment : CommentBaseCreatePayload ) => {
194
+ const commentId = uuid ( )
195
+
196
+ const notification = handleGetNotificationValue ( nextComment . message , commentId )
197
+
198
+ onChange ( set ( notification . subscribers , [ 'subscribers' ] ) )
199
+
160
200
operation . create ( {
201
+ id : commentId ,
161
202
type : 'task' ,
162
203
message : nextComment . message ,
163
204
parentCommentId : nextComment . parentCommentId ,
164
205
reactions : EMPTY_ARRAY ,
165
206
status : 'open' ,
166
207
threadId : nextComment . threadId ,
208
+ context : {
209
+ notification,
210
+ } ,
167
211
} )
168
212
} ,
169
- [ operation ] ,
213
+ [ operation , handleGetNotificationValue , onChange ] ,
170
214
)
171
215
172
216
const handleCommentCreateRetry = useCallback (
@@ -176,6 +220,10 @@ export function TasksActivityLog(props: TasksActivityLogProps) {
176
220
const comment = getComment ( id )
177
221
if ( ! comment ) return
178
222
223
+ const notification = handleGetNotificationValue ( comment . message , comment . _id )
224
+
225
+ onChange ( set ( notification . subscribers , [ 'subscribers' ] ) )
226
+
179
227
operation . create ( {
180
228
type : 'task' ,
181
229
id : comment . _id ,
@@ -184,9 +232,12 @@ export function TasksActivityLog(props: TasksActivityLogProps) {
184
232
reactions : comment . reactions || EMPTY_ARRAY ,
185
233
status : comment . status ,
186
234
threadId : comment . threadId ,
235
+ context : {
236
+ notification,
237
+ } ,
187
238
} )
188
239
} ,
189
- [ getComment , operation ] ,
240
+ [ getComment , operation , handleGetNotificationValue , onChange ] ,
190
241
)
191
242
192
243
const handleCommentReact = useCallback (
0 commit comments