@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.
Files changed (59) hide show
  1. package/README.md +36 -33
  2. package/dist/_chunks-es/context.js +1 -1
  3. package/dist/_chunks-es/context.js.map +1 -1
  4. package/dist/_chunks-es/useLogOut.js +16 -5
  5. package/dist/_chunks-es/useLogOut.js.map +1 -1
  6. package/dist/components.d.ts +39 -11
  7. package/dist/components.js +27 -10
  8. package/dist/components.js.map +1 -1
  9. package/dist/context.d.ts +2 -2
  10. package/dist/hooks.d.ts +475 -148
  11. package/dist/hooks.js +183 -54
  12. package/dist/hooks.js.map +1 -1
  13. package/dist/index.d.ts +2 -2
  14. package/dist/index.js +10 -2
  15. package/dist/index.js.map +1 -1
  16. package/package.json +4 -4
  17. package/src/_exports/hooks.ts +14 -4
  18. package/src/_exports/index.ts +1 -10
  19. package/src/components/Login/LoginLinks.test.tsx +1 -1
  20. package/src/components/SDKProvider.test.tsx +7 -7
  21. package/src/components/SDKProvider.tsx +20 -7
  22. package/src/components/SanityApp.test.tsx +104 -2
  23. package/src/components/SanityApp.tsx +66 -18
  24. package/src/components/auth/authTestHelpers.tsx +1 -1
  25. package/src/components/utils.ts +19 -0
  26. package/src/context/SanityProvider.test.tsx +1 -1
  27. package/src/context/SanityProvider.tsx +4 -4
  28. package/src/hooks/_synchronous-groq-js.mjs +4 -0
  29. package/src/hooks/client/useClient.ts +4 -1
  30. package/src/hooks/context/useSanityInstance.test.tsx +1 -1
  31. package/src/hooks/context/useSanityInstance.ts +19 -3
  32. package/src/hooks/datasets/useDatasets.ts +26 -1
  33. package/src/hooks/document/useDocument.test.ts +2 -2
  34. package/src/hooks/document/useDocument.ts +9 -7
  35. package/src/hooks/document/useDocumentEvent.test.ts +13 -3
  36. package/src/hooks/document/useDocumentEvent.ts +13 -3
  37. package/src/hooks/document/useDocumentSyncStatus.ts +3 -2
  38. package/src/hooks/document/useEditDocument.test.ts +19 -12
  39. package/src/hooks/document/useEditDocument.ts +13 -10
  40. package/src/hooks/document/usePermissions.ts +19 -2
  41. package/src/hooks/helpers/createStateSourceHook.tsx +11 -5
  42. package/src/hooks/infiniteList/useInfiniteList.test.tsx +152 -0
  43. package/src/hooks/infiniteList/useInfiniteList.ts +163 -0
  44. package/src/hooks/paginatedList/usePaginatedList.test.tsx +259 -0
  45. package/src/hooks/paginatedList/usePaginatedList.ts +278 -0
  46. package/src/hooks/projection/useProjection.test.tsx +218 -0
  47. package/src/hooks/projection/useProjection.ts +135 -0
  48. package/src/hooks/projects/useProject.ts +25 -1
  49. package/src/hooks/projects/useProjects.ts +33 -11
  50. package/src/hooks/query/useQuery.test.tsx +188 -0
  51. package/src/hooks/query/useQuery.ts +112 -0
  52. package/src/hooks/users/useUsers.ts +3 -3
  53. package/src/utils/getEnv.ts +21 -0
  54. package/src/version.ts +8 -0
  55. package/src/hooks/documentCollection/types.ts +0 -19
  56. package/src/hooks/documentCollection/useDocuments.test.ts +0 -130
  57. package/src/hooks/documentCollection/useDocuments.ts +0 -126
  58. package/src/hooks/documentCollection/useSearch.test.ts +0 -100
  59. 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
- }