Skip to content

Commit 75ed1f5

Browse files
authored
fix: prevent infinite loading issue on video browser (#399)
* fix: prevent infinite loading issue on video browser * fix: move assetDocuments array fallback to within useMemo
1 parent 60ff8f5 commit 75ed1f5

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/hooks/useAssets.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {useMemo, useState} from 'react'
2-
import {collate, createHookFromObservableFactory, DocumentStore, useDocumentStore} from 'sanity'
2+
import {useObservable} from 'react-rx'
3+
import {collate, DocumentStore, useDocumentStore} from 'sanity'
34

45
import {SANITY_API_VERSION} from '../hooks/useClient'
56
import {createSearchFilter} from '../util/createSearchFilter'
@@ -14,44 +15,49 @@ export const ASSET_SORT_OPTIONS = {
1415

1516
export type SortOption = keyof typeof ASSET_SORT_OPTIONS
1617

17-
const useAssetDocuments = createHookFromObservableFactory<
18-
VideoAssetDocument[],
19-
{
20-
documentStore: DocumentStore
21-
sort: SortOption
22-
searchQuery: string
23-
}
24-
>(({documentStore, sort, searchQuery}) => {
25-
const search = createSearchFilter(searchQuery)
26-
const filter = [`_type == "mux.videoAsset"`, ...search.filter].filter(Boolean).join(' && ')
18+
const useAssetDocuments = ({
19+
documentStore,
20+
sort,
21+
searchQuery,
22+
}: {
23+
documentStore: DocumentStore
24+
sort: SortOption
25+
searchQuery: string
26+
}): VideoAssetDocument[] | undefined => {
27+
const memoizedObservable = useMemo(() => {
28+
const search = createSearchFilter(searchQuery)
29+
const filter = [`_type == "mux.videoAsset"`, ...search.filter].filter(Boolean).join(' && ')
30+
const sortFragment = ASSET_SORT_OPTIONS[sort].groq
31+
return documentStore.listenQuery(
32+
/* groq */ `*[${filter}] | order(${sortFragment})`,
33+
search.params,
34+
{
35+
apiVersion: SANITY_API_VERSION,
36+
}
37+
)
38+
}, [documentStore, sort, searchQuery])
2739

28-
const sortFragment = ASSET_SORT_OPTIONS[sort].groq
29-
return documentStore.listenQuery(
30-
/* groq */ `*[${filter}] | order(${sortFragment})`,
31-
search.params,
32-
{
33-
apiVersion: SANITY_API_VERSION,
34-
}
35-
)
36-
})
40+
return useObservable(memoizedObservable, undefined)
41+
}
3742

3843
export default function useAssets() {
3944
const documentStore = useDocumentStore()
4045
const [sort, setSort] = useState<SortOption>('createdDesc')
4146
const [searchQuery, setSearchQuery] = useState('')
4247

43-
const [assetDocuments = [], isLoading] = useAssetDocuments({documentStore, sort, searchQuery})
48+
const assetDocumentsObservable = useAssetDocuments({documentStore, sort, searchQuery})
49+
const isLoading = assetDocumentsObservable === undefined
4450
const assets = useMemo(
4551
() =>
4652
// Avoid displaying both drafts & published assets by collating them together and giving preference to drafts
47-
collate<VideoAssetDocument>(assetDocuments).map(
53+
collate<VideoAssetDocument>(assetDocumentsObservable ?? []).map(
4854
(collated) =>
4955
({
5056
...(collated.draft || collated.published || {}),
5157
_id: collated.id,
5258
}) as VideoAssetDocument
5359
),
54-
[assetDocuments]
60+
[assetDocumentsObservable]
5561
)
5662

5763
return {

0 commit comments

Comments
 (0)