1
1
import { Box , Flex , Stack , Text } from '@sanity/ui'
2
2
import { uuid } from '@sanity/uuid'
3
3
import { AnimatePresence , motion , type Variants } from 'framer-motion'
4
- import { useCallback , useEffect , useMemo , useState } from 'react'
4
+ import { useCallback , useMemo , useState } from 'react'
5
5
import {
6
6
type FormPatch ,
7
- getPublishedId ,
8
7
LoadingBlock ,
9
8
type PatchEvent ,
10
9
type Path ,
11
10
set ,
12
- type TransactionLogEventWithEffects ,
13
- useClient ,
14
11
useCurrentUser ,
15
12
useTranslation ,
16
13
useWorkspace ,
17
14
} from 'sanity'
18
15
import styled from 'styled-components'
19
16
20
- import { getJsonStream } from '../../../../../core/store/_legacy/history/history/getJsonStream'
21
17
import {
22
18
type CommentBaseCreatePayload ,
23
19
type CommentCreatePayload ,
@@ -29,78 +25,15 @@ import {
29
25
useComments ,
30
26
} from '../../../../../structure/comments'
31
27
import { tasksLocaleNamespace } from '../../../../i18n'
32
- import { API_VERSION } from '../../constants/API_VERSION'
33
28
import { type TaskDocument } from '../../types'
34
- import { CurrentWorkspaceProvider } from '../form/CurrentWorkspaceProvider'
35
29
import { getMentionedUsers } from '../form/utils'
36
- import { type FieldChange , trackFieldChanges } from './helpers/parseTransactions'
30
+ import { type FieldChange } from './helpers/parseTransactions'
37
31
import { EditedAt } from './TaskActivityEditedAt'
38
32
import { TasksActivityCommentInput } from './TasksActivityCommentInput'
39
33
import { TasksActivityCommentItem } from './TasksActivityCommentItem'
40
34
import { TasksActivityCreatedAt } from './TasksActivityCreatedAt'
41
35
import { TasksSubscribers } from './TasksSubscribers'
42
36
43
- function useActivityLog ( task : TaskDocument ) {
44
- const [ changes , setChanges ] = useState < FieldChange [ ] > ( [ ] )
45
- const client = useClient ( { apiVersion : API_VERSION } )
46
- const { dataset, token} = client . config ( )
47
-
48
- const queryParams = `tag=sanity.studio.tasks.history&effectFormat=mendoza&excludeContent=true&includeIdentifiedDocumentsOnly=true&reverse=true`
49
- const transactionsUrl = client . getUrl (
50
- `/data/history/${ dataset } /transactions/${ getPublishedId ( task . _id ) } ?${ queryParams } ` ,
51
- )
52
-
53
- const fetchAndParse = useCallback (
54
- async ( newestTaskDocument : TaskDocument ) => {
55
- try {
56
- const transactions : TransactionLogEventWithEffects [ ] = [ ]
57
-
58
- const stream = await getJsonStream ( transactionsUrl , token )
59
- const reader = stream . getReader ( )
60
- let result
61
- for ( ; ; ) {
62
- result = await reader . read ( )
63
- if ( result . done ) {
64
- break
65
- }
66
- if ( 'error' in result . value ) {
67
- throw new Error ( result . value . error . description || result . value . error . type )
68
- }
69
- transactions . push ( result . value )
70
- }
71
-
72
- const fieldsToTrack : ( keyof Omit < TaskDocument , '_rev' > ) [ ] = [
73
- 'createdByUser' ,
74
- 'title' ,
75
- 'description' ,
76
- 'dueBy' ,
77
- 'assignedTo' ,
78
- 'status' ,
79
- 'target' ,
80
- ]
81
-
82
- const parsedChanges = await trackFieldChanges (
83
- newestTaskDocument ,
84
- [ ...transactions ] ,
85
- fieldsToTrack ,
86
- )
87
-
88
- setChanges ( parsedChanges )
89
- } catch ( error ) {
90
- console . error ( 'Failed to fetch and parse activity log' , error )
91
- }
92
- } ,
93
- [ transactionsUrl , token ] ,
94
- )
95
-
96
- useEffect ( ( ) => {
97
- fetchAndParse ( task )
98
- // Task is updated on every change, wait until the revision changes to update the activity log.
99
- // eslint-disable-next-line react-hooks/exhaustive-deps
100
- } , [ fetchAndParse , task . _rev ] )
101
- return { changes}
102
- }
103
-
104
37
const EMPTY_ARRAY : [ ] = [ ]
105
38
106
39
const VARIANTS : Variants = {
@@ -114,6 +47,7 @@ interface TasksActivityLogProps {
114
47
onChange : ( patch : FormPatch | PatchEvent | FormPatch [ ] ) => void
115
48
path ?: Path
116
49
value : TaskDocument
50
+ activityData : FieldChange [ ]
117
51
}
118
52
119
53
type Activity =
@@ -129,21 +63,21 @@ type Activity =
129
63
}
130
64
131
65
export function TasksActivityLog ( props : TasksActivityLogProps ) {
132
- const { value, onChange, path} = props
66
+ const { value, onChange, path, activityData = [ ] } = props
133
67
const currentUser = useCurrentUser ( )
134
- const { title : workspaceTitle , basePath} = useWorkspace ( )
135
68
69
+ const { title : workspaceTitle , basePath} = useWorkspace ( )
136
70
const { comments, mentionOptions, operation, getComment} = useComments ( )
137
71
const [ commentToDeleteId , setCommentToDeleteId ] = useState < string | null > ( null )
138
72
const [ commentDeleteError , setCommentDeleteError ] = useState < Error | null > ( null )
139
73
const [ commentDeleteLoading , setCommentDeleteLoading ] = useState ( false )
140
74
141
75
const loading = comments . loading
142
76
const taskComments = comments . data . open
143
-
144
77
const handleGetNotificationValue = useCallback (
145
78
( message : CommentInputProps [ 'value' ] , commentId : string ) => {
146
- const studioUrl = new URL ( `${ window . location . origin } ${ basePath } /` )
79
+ const studioUrl = new URL ( `${ window . location . origin } ${ basePath ? `${ basePath } /` : '' } ` )
80
+
147
81
studioUrl . searchParams . set ( 'sidebar' , 'tasks' )
148
82
studioUrl . searchParams . set ( 'selectedTask' , value ?. _id )
149
83
studioUrl . searchParams . set ( 'viewMode' , 'edit' )
@@ -271,8 +205,6 @@ export function TasksActivityLog(props: TasksActivityLogProps) {
271
205
[ operation ] ,
272
206
)
273
207
274
- const activityData = useActivityLog ( value ) . changes
275
-
276
208
const activity : Activity [ ] = useMemo ( ( ) => {
277
209
const taskActivity : Activity [ ] = activityData . map ( ( item ) => ( {
278
210
_type : 'activity' as const ,
@@ -344,36 +276,34 @@ export function TasksActivityLog(props: TasksActivityLogProps) {
344
276
) }
345
277
346
278
{ currentUser && (
347
- < CurrentWorkspaceProvider >
348
- < Stack space = { 4 } marginTop = { 1 } >
349
- { activity . map ( ( item ) => {
350
- if ( item . _type === 'activity' ) {
351
- return < EditedAt key = { item . timestamp } activity = { item . payload } />
352
- }
353
-
354
- return (
355
- < TasksActivityCommentItem
356
- currentUser = { currentUser }
357
- key = { item . payload . parentComment . _id }
358
- mentionOptions = { mentionOptions }
359
- onCreateRetry = { handleCommentCreateRetry }
360
- onDelete = { handleDeleteCommentStart }
361
- onEdit = { handleCommentEdit }
362
- onReactionSelect = { handleCommentReact }
363
- onReply = { handleCommentReply }
364
- parentComment = { item . payload . parentComment }
365
- replies = { item . payload . replies }
366
- />
367
- )
368
- } ) }
369
-
370
- < TasksActivityCommentInput
371
- currentUser = { currentUser }
372
- mentionOptions = { mentionOptions }
373
- onSubmit = { handleCommentCreate }
374
- />
375
- </ Stack >
376
- </ CurrentWorkspaceProvider >
279
+ < Stack space = { 4 } marginTop = { 1 } >
280
+ { activity . map ( ( item ) => {
281
+ if ( item . _type === 'activity' ) {
282
+ return < EditedAt key = { item . timestamp } activity = { item . payload } />
283
+ }
284
+
285
+ return (
286
+ < TasksActivityCommentItem
287
+ currentUser = { currentUser }
288
+ key = { item . payload . parentComment . _id }
289
+ mentionOptions = { mentionOptions }
290
+ onCreateRetry = { handleCommentCreateRetry }
291
+ onDelete = { handleDeleteCommentStart }
292
+ onEdit = { handleCommentEdit }
293
+ onReactionSelect = { handleCommentReact }
294
+ onReply = { handleCommentReply }
295
+ parentComment = { item . payload . parentComment }
296
+ replies = { item . payload . replies }
297
+ />
298
+ )
299
+ } ) }
300
+
301
+ < TasksActivityCommentInput
302
+ currentUser = { currentUser }
303
+ mentionOptions = { mentionOptions }
304
+ onSubmit = { handleCommentCreate }
305
+ />
306
+ </ Stack >
377
307
) }
378
308
</ MotionStack >
379
309
) }
0 commit comments