@sanity/sdk-react 0.0.0-alpha.14 → 0.0.0-alpha.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -33
- package/dist/_chunks-es/context.js +1 -1
- package/dist/_chunks-es/context.js.map +1 -1
- package/dist/_chunks-es/useLogOut.js +16 -5
- package/dist/_chunks-es/useLogOut.js.map +1 -1
- package/dist/components.d.ts +39 -11
- package/dist/components.js +27 -10
- package/dist/components.js.map +1 -1
- package/dist/context.d.ts +2 -2
- package/dist/hooks.d.ts +475 -148
- package/dist/hooks.js +183 -54
- package/dist/hooks.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/_exports/hooks.ts +14 -4
- package/src/_exports/index.ts +1 -10
- package/src/components/Login/LoginLinks.test.tsx +1 -1
- package/src/components/SDKProvider.test.tsx +7 -7
- package/src/components/SDKProvider.tsx +20 -7
- package/src/components/SanityApp.test.tsx +104 -2
- package/src/components/SanityApp.tsx +66 -18
- package/src/components/auth/authTestHelpers.tsx +1 -1
- package/src/components/utils.ts +19 -0
- package/src/context/SanityProvider.test.tsx +1 -1
- package/src/context/SanityProvider.tsx +4 -4
- package/src/hooks/_synchronous-groq-js.mjs +4 -0
- package/src/hooks/client/useClient.ts +4 -1
- package/src/hooks/context/useSanityInstance.test.tsx +1 -1
- package/src/hooks/context/useSanityInstance.ts +19 -3
- package/src/hooks/datasets/useDatasets.ts +26 -1
- package/src/hooks/document/useDocument.test.ts +2 -2
- package/src/hooks/document/useDocument.ts +9 -7
- package/src/hooks/document/useDocumentEvent.test.ts +13 -3
- package/src/hooks/document/useDocumentEvent.ts +13 -3
- package/src/hooks/document/useDocumentSyncStatus.ts +3 -2
- package/src/hooks/document/useEditDocument.test.ts +19 -12
- package/src/hooks/document/useEditDocument.ts +13 -10
- package/src/hooks/document/usePermissions.ts +19 -2
- package/src/hooks/helpers/createStateSourceHook.tsx +11 -5
- package/src/hooks/infiniteList/useInfiniteList.test.tsx +152 -0
- package/src/hooks/infiniteList/useInfiniteList.ts +163 -0
- package/src/hooks/paginatedList/usePaginatedList.test.tsx +259 -0
- package/src/hooks/paginatedList/usePaginatedList.ts +278 -0
- package/src/hooks/projection/useProjection.test.tsx +218 -0
- package/src/hooks/projection/useProjection.ts +135 -0
- package/src/hooks/projects/useProject.ts +25 -1
- package/src/hooks/projects/useProjects.ts +33 -11
- package/src/hooks/query/useQuery.test.tsx +188 -0
- package/src/hooks/query/useQuery.ts +112 -0
- package/src/hooks/users/useUsers.ts +3 -3
- package/src/utils/getEnv.ts +21 -0
- package/src/version.ts +8 -0
- package/src/hooks/documentCollection/types.ts +0 -19
- package/src/hooks/documentCollection/useDocuments.test.ts +0 -130
- package/src/hooks/documentCollection/useDocuments.ts +0 -126
- package/src/hooks/documentCollection/useSearch.test.ts +0 -100
- package/src/hooks/documentCollection/useSearch.ts +0 -75
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import {type DocumentListOptions} from '@sanity/sdk'
|
|
2
|
-
import {renderHook} from '@testing-library/react'
|
|
3
|
-
import {beforeEach, describe, expect, it, vi} from 'vitest'
|
|
4
|
-
|
|
5
|
-
import {useDocuments} from './useDocuments'
|
|
6
|
-
import {useSearch} from './useSearch'
|
|
7
|
-
|
|
8
|
-
vi.mock('./useDocuments')
|
|
9
|
-
|
|
10
|
-
describe('useSearch', () => {
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
vi.clearAllMocks()
|
|
13
|
-
vi.mocked(useDocuments).mockReturnValue({
|
|
14
|
-
results: [],
|
|
15
|
-
isPending: false,
|
|
16
|
-
hasMore: false,
|
|
17
|
-
count: 0,
|
|
18
|
-
loadMore: vi.fn(),
|
|
19
|
-
})
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
it('should pass empty filter when no search parameters provided', () => {
|
|
23
|
-
renderHook(() => useSearch())
|
|
24
|
-
|
|
25
|
-
expect(useDocuments).toHaveBeenCalledWith({
|
|
26
|
-
filter: '',
|
|
27
|
-
})
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
it('should construct search filter with query', () => {
|
|
31
|
-
renderHook(() => useSearch({query: 'test query'}))
|
|
32
|
-
|
|
33
|
-
expect(useDocuments).toHaveBeenCalledWith({
|
|
34
|
-
filter: '[@] match text::query("test query")',
|
|
35
|
-
})
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
it('should trim whitespace from query', () => {
|
|
39
|
-
renderHook(() => useSearch({query: ' test query '}))
|
|
40
|
-
|
|
41
|
-
expect(useDocuments).toHaveBeenCalledWith({
|
|
42
|
-
filter: '[@] match text::query("test query")',
|
|
43
|
-
})
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
it('should not add search condition if query is empty', () => {
|
|
47
|
-
renderHook(() => useSearch({query: ' '}))
|
|
48
|
-
|
|
49
|
-
expect(useDocuments).toHaveBeenCalledWith({
|
|
50
|
-
filter: '',
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it('should combine search query with additional filter', () => {
|
|
55
|
-
renderHook(() =>
|
|
56
|
-
useSearch({
|
|
57
|
-
query: 'test query',
|
|
58
|
-
filter: '_type == "book"',
|
|
59
|
-
}),
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
expect(useDocuments).toHaveBeenCalledWith({
|
|
63
|
-
filter: '[@] match text::query("test query") && (_type == "book")',
|
|
64
|
-
})
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
it('should pass through other DocumentListOptions', () => {
|
|
68
|
-
const options: DocumentListOptions = {
|
|
69
|
-
sort: [{field: '_createdAt', direction: 'desc'}],
|
|
70
|
-
perspective: 'published',
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
renderHook(() =>
|
|
74
|
-
useSearch({
|
|
75
|
-
query: 'test query',
|
|
76
|
-
...options,
|
|
77
|
-
}),
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
expect(useDocuments).toHaveBeenCalledWith({
|
|
81
|
-
filter: '[@] match text::query("test query")',
|
|
82
|
-
...options,
|
|
83
|
-
})
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
it('should return useDocuments result unchanged', () => {
|
|
87
|
-
const mockResult = {
|
|
88
|
-
results: [{_id: 'doc1', _type: 'book'}],
|
|
89
|
-
isPending: false,
|
|
90
|
-
hasMore: true,
|
|
91
|
-
count: 1,
|
|
92
|
-
loadMore: vi.fn(),
|
|
93
|
-
}
|
|
94
|
-
vi.mocked(useDocuments).mockReturnValue(mockResult)
|
|
95
|
-
|
|
96
|
-
const {result} = renderHook(() => useSearch({query: 'test'}))
|
|
97
|
-
|
|
98
|
-
expect(result.current).toBe(mockResult)
|
|
99
|
-
})
|
|
100
|
-
})
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import {type DocumentListOptions} from '@sanity/sdk'
|
|
2
|
-
import {useMemo} from 'react'
|
|
3
|
-
|
|
4
|
-
import {type DocumentHandleCollection} from './types'
|
|
5
|
-
import {useDocuments} from './useDocuments'
|
|
6
|
-
|
|
7
|
-
interface SearchOptions extends DocumentListOptions {
|
|
8
|
-
query?: string
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @public
|
|
13
|
-
* Hook for searching documents using full-text search.
|
|
14
|
-
*
|
|
15
|
-
* @param options - The options for the search.
|
|
16
|
-
* @example
|
|
17
|
-
* ```tsx
|
|
18
|
-
* function SearchResults() {
|
|
19
|
-
* const [query, setQuery] = useState('')
|
|
20
|
-
* const {results, isPending} = useSearch({
|
|
21
|
-
* filter: '_type == "book"',
|
|
22
|
-
* query,
|
|
23
|
-
* sort: [{field: '_createdAt', direction: 'desc'}]
|
|
24
|
-
* })
|
|
25
|
-
*
|
|
26
|
-
* return (
|
|
27
|
-
* <div>
|
|
28
|
-
* <input
|
|
29
|
-
* type="search"
|
|
30
|
-
* value={query}
|
|
31
|
-
* onChange={(e) => setQuery(e.target.value)}
|
|
32
|
-
* placeholder="Search books..."
|
|
33
|
-
* />
|
|
34
|
-
* {isPending ? (
|
|
35
|
-
* <div>Searching...</div>
|
|
36
|
-
* ) : (
|
|
37
|
-
* <ul>
|
|
38
|
-
* {results.map((doc) => (
|
|
39
|
-
* <li key={doc._id}>{doc._id}</li>
|
|
40
|
-
* ))}
|
|
41
|
-
* </ul>
|
|
42
|
-
* )}
|
|
43
|
-
* </div>
|
|
44
|
-
* )
|
|
45
|
-
* }
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
export function useSearch({
|
|
49
|
-
query,
|
|
50
|
-
filter: additionalFilter,
|
|
51
|
-
...options
|
|
52
|
-
}: SearchOptions = {}): DocumentHandleCollection {
|
|
53
|
-
// Build the complete GROQ filter
|
|
54
|
-
const filter = useMemo(() => {
|
|
55
|
-
const conditions: string[] = []
|
|
56
|
-
|
|
57
|
-
// Add search query if specified
|
|
58
|
-
if (query?.trim()) {
|
|
59
|
-
conditions.push(`[@] match text::query("${query.trim()}")`)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Add additional filter if specified
|
|
63
|
-
if (additionalFilter) {
|
|
64
|
-
conditions.push(`(${additionalFilter})`)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return conditions.length ? conditions.join(' && ') : ''
|
|
68
|
-
}, [query, additionalFilter])
|
|
69
|
-
|
|
70
|
-
// Use the existing useDocuments hook with our constructed filter
|
|
71
|
-
return useDocuments({
|
|
72
|
-
...options,
|
|
73
|
-
filter,
|
|
74
|
-
})
|
|
75
|
-
}
|