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(sanity): avoid fetching in a loop #7152

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Changes from all commits
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
@@ -1,6 +1,6 @@
import {AddIcon, CheckmarkIcon} from '@sanity/icons'
import {useToast} from '@sanity/ui'
import {useCallback, useEffect, useState} from 'react'
import {useCallback, useEffect, useRef, useState} from 'react'
import {filter, firstValueFrom} from 'rxjs'
import {
DEFAULT_STUDIO_CLIENT_OPTIONS,
Expand Down Expand Up @@ -35,23 +35,27 @@ export function BundleActions(props: BundleActionsProps): JSX.Element {
const client = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS)
const toast = useToast()
const {newVersion} = useDocumentOperation(documentId, documentType)
const dummyFetch = useRef<Promise<void> | undefined>()

const fetchVersions = useCallback(async () => {
if (!loading) {
const response = await getAllVersionsOfDocument(bundles, client, documentId)
setDocumentVersions(response)
setIsInVersion(versionDocumentExists(documentVersions, name))
}
}, [loading, bundles, client, documentId, documentVersions, name])
}, [loading, bundles, client, documentId])

// DUMMY FETCH -- NEEDS TO BE REPLACED -- USING GROQ from utils
useEffect(() => {
const fetchVersionsInner = async () => {
fetchVersions()
if (!dummyFetch.current) {
dummyFetch.current = fetchVersions()
}
await dummyFetch.current
setIsInVersion(versionDocumentExists(documentVersions, name))
}

fetchVersionsInner()
}, [bundles, documentId, fetchVersions])
}, [bundles, documentId, fetchVersions, documentVersions, name])

const handleAddVersion = useCallback(async () => {
// only add to version if there isn't already a version in that bundle of this doc
Expand Down
Loading