Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): update useReleaseHistory to fetch with the version documents #7463

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const ReleaseDetail = () => {
const {loading: documentsLoading, results} = useBundleDocuments(parsedBundleId)

const documentIds = results.map((result) => result.document?._id)
const history = useReleaseHistory(documentIds)
const history = useReleaseHistory(documentIds, parsedBundleId)

const bundle = data?.find((storeBundle) => storeBundle._id === parsedBundleId)
const bundleHasDocuments = !!results.length
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useCallback, useEffect, useMemo, useState} from 'react'
import {getPublishedId, type TransactionLogEventWithEffects, useClient} from 'sanity'
import {getVersionId, type TransactionLogEventWithEffects, useClient} from 'sanity'

import {getJsonStream} from '../../../../store/_legacy/history/history/getJsonStream'
import {API_VERSION} from '../../../../tasks/constants'
Expand All @@ -12,7 +12,10 @@ export type DocumentHistory = {
}

// TODO: Update this to contemplate the _revision change on any of the internal bundle documents, and fetch only the history of that document if changes.
export function useReleaseHistory(bundleDocumentsIds: string[]): {
export function useReleaseHistory(
bundleDocumentsIds: string[],
releaseId: string,
): {
documentsHistory: Record<string, DocumentHistory>
collaborators: string[]
loading: boolean
Expand All @@ -21,14 +24,14 @@ export function useReleaseHistory(bundleDocumentsIds: string[]): {
const {dataset, token} = client.config()
const [history, setHistory] = useState<TransactionLogEventWithEffects[]>([])
const queryParams = `tag=sanity.studio.tasks.history&effectFormat=mendoza&excludeContent=true&includeIdentifiedDocumentsOnly=true`

const publishedIds = bundleDocumentsIds.map((id) => getPublishedId(id)).join(',')
const publishedIds = bundleDocumentsIds.map((id) => getVersionId(id, releaseId)).join(',')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might need to rename the const to documentIds or maybe add a comment to clarify? Just bringing it up since we are technically bringing up a bunch of version ids (which are not publishedIds) 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree @RitaDias. Based on my understanding from glancing over the code, at least 🙂.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TRUE missed that rename! will push the update and merge

const transactionsUrl = client.getUrl(
`/data/history/${dataset}/transactions/${publishedIds}?${queryParams}`,
)

const fetchAndParseAll = useCallback(async () => {
if (!publishedIds) return
if (!releaseId) return
const transactions: TransactionLogEventWithEffects[] = []
const stream = await getJsonStream(transactionsUrl, token)
const reader = stream.getReader()
Expand All @@ -44,7 +47,7 @@ export function useReleaseHistory(bundleDocumentsIds: string[]): {
transactions.push(result.value)
}
setHistory(transactions)
}, [publishedIds, transactionsUrl, token])
}, [publishedIds, transactionsUrl, token, releaseId])

useEffect(() => {
fetchAndParseAll()
Expand Down
Loading