-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tasks): update info passed to the tasks comments notification con…
…text (#6087)
- Loading branch information
1 parent
a8f12ba
commit 9f01651
Showing
3 changed files
with
118 additions
and
109 deletions.
There are no files selected for viewing
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
70 changes: 70 additions & 0 deletions
70
packages/sanity/src/tasks/src/tasks/hooks/useActivityLog.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,70 @@ | ||
import {useCallback, useEffect, useState} from 'react' | ||
import {getPublishedId, type TransactionLogEventWithEffects, useClient} from 'sanity' | ||
|
||
import {getJsonStream} from '../../../../core/store/_legacy/history/history/getJsonStream' | ||
import {type FieldChange, trackFieldChanges} from '../components/activity/helpers/parseTransactions' | ||
import {API_VERSION} from '../constants/API_VERSION' | ||
import {type TaskDocument} from '../types' | ||
|
||
export function useActivityLog(task: TaskDocument): { | ||
changes: FieldChange[] | ||
} { | ||
const [changes, setChanges] = useState<FieldChange[]>([]) | ||
const client = useClient({apiVersion: API_VERSION}) | ||
const {dataset, token} = client.config() | ||
|
||
const queryParams = `tag=sanity.studio.tasks.history&effectFormat=mendoza&excludeContent=true&includeIdentifiedDocumentsOnly=true&reverse=true` | ||
const transactionsUrl = client.getUrl( | ||
`/data/history/${dataset}/transactions/${getPublishedId(task._id)}?${queryParams}`, | ||
) | ||
|
||
const fetchAndParse = useCallback( | ||
async (newestTaskDocument: TaskDocument) => { | ||
try { | ||
const transactions: TransactionLogEventWithEffects[] = [] | ||
|
||
const stream = await getJsonStream(transactionsUrl, token) | ||
const reader = stream.getReader() | ||
let result | ||
for (;;) { | ||
result = await reader.read() | ||
if (result.done) { | ||
break | ||
} | ||
if ('error' in result.value) { | ||
throw new Error(result.value.error.description || result.value.error.type) | ||
} | ||
transactions.push(result.value) | ||
} | ||
|
||
const fieldsToTrack: (keyof Omit<TaskDocument, '_rev'>)[] = [ | ||
'createdByUser', | ||
'title', | ||
'description', | ||
'dueBy', | ||
'assignedTo', | ||
'status', | ||
'target', | ||
] | ||
|
||
const parsedChanges = await trackFieldChanges( | ||
newestTaskDocument, | ||
[...transactions], | ||
fieldsToTrack, | ||
) | ||
|
||
setChanges(parsedChanges) | ||
} catch (error) { | ||
console.error('Failed to fetch and parse activity log', error) | ||
} | ||
}, | ||
[transactionsUrl, token], | ||
) | ||
|
||
useEffect(() => { | ||
fetchAndParse(task) | ||
// Task is updated on every change, wait until the revision changes to update the activity log. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [fetchAndParse, task._rev]) | ||
return {changes} | ||
} |