@sanity/sdk-react 2.8.0 → 2.9.0

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 (35) hide show
  1. package/dist/index.d.ts +144 -25
  2. package/dist/index.js +216 -122
  3. package/dist/index.js.map +1 -1
  4. package/package.json +7 -7
  5. package/src/_exports/sdk-react.ts +1 -0
  6. package/src/components/SanityApp.tsx +1 -0
  7. package/src/context/ResourceProvider.test.tsx +7 -1
  8. package/src/context/ResourceProvider.tsx +6 -0
  9. package/src/context/SDKStudioContext.ts +6 -0
  10. package/src/hooks/dashboard/useDispatchIntent.test.ts +2 -0
  11. package/src/hooks/dashboard/useWindowTitle.test.ts +213 -0
  12. package/src/hooks/dashboard/useWindowTitle.ts +112 -0
  13. package/src/hooks/document/useApplyDocumentActions.test.ts +113 -10
  14. package/src/hooks/document/useApplyDocumentActions.ts +99 -3
  15. package/src/hooks/document/useDocument.ts +22 -6
  16. package/src/hooks/document/useDocumentEvent.test.tsx +3 -3
  17. package/src/hooks/document/useDocumentEvent.ts +10 -3
  18. package/src/hooks/document/useDocumentPermissions.test.tsx +86 -2
  19. package/src/hooks/document/useDocumentPermissions.ts +22 -0
  20. package/src/hooks/document/useDocumentSyncStatus.test.ts +13 -2
  21. package/src/hooks/document/useDocumentSyncStatus.ts +14 -5
  22. package/src/hooks/document/useEditDocument.ts +34 -8
  23. package/src/hooks/documents/useDocuments.ts +2 -0
  24. package/src/hooks/helpers/useNormalizedSourceOptions.ts +50 -28
  25. package/src/hooks/helpers/useTrackHookUsage.ts +37 -0
  26. package/src/hooks/paginatedDocuments/usePaginatedDocuments.ts +2 -0
  27. package/src/hooks/presence/usePresence.ts +2 -0
  28. package/src/hooks/preview/useDocumentPreview.test.tsx +84 -193
  29. package/src/hooks/preview/useDocumentPreview.tsx +39 -55
  30. package/src/hooks/projection/useDocumentProjection.ts +2 -0
  31. package/src/hooks/query/useQuery.ts +2 -0
  32. package/src/hooks/releases/useActiveReleases.ts +32 -13
  33. package/src/hooks/releases/usePerspective.ts +26 -14
  34. package/src/hooks/users/useUser.ts +2 -0
  35. package/src/hooks/users/useUsers.ts +2 -0
@@ -1,19 +1,18 @@
1
1
  import {
2
+ type DocumentSource,
2
3
  getActiveReleasesState,
3
4
  type ReleaseDocument,
5
+ type SanityConfig,
4
6
  type SanityInstance,
5
7
  type StateSource,
6
8
  } from '@sanity/sdk'
7
9
  import {filter, firstValueFrom} from 'rxjs'
8
10
 
9
11
  import {createStateSourceHook} from '../helpers/createStateSourceHook'
10
-
11
- /**
12
- * @public
13
- */
14
- type UseActiveReleases = {
15
- (): ReleaseDocument[]
16
- }
12
+ import {
13
+ useNormalizedSourceOptions,
14
+ type WithSourceNameSupport,
15
+ } from '../helpers/useNormalizedSourceOptions'
17
16
 
18
17
  /**
19
18
  * @public
@@ -30,10 +29,30 @@ type UseActiveReleases = {
30
29
  * const activeReleases = useActiveReleases()
31
30
  * ```
32
31
  */
33
- export const useActiveReleases: UseActiveReleases = createStateSourceHook({
34
- getState: getActiveReleasesState as (instance: SanityInstance) => StateSource<ReleaseDocument[]>,
35
- shouldSuspend: (instance: SanityInstance) =>
36
- getActiveReleasesState(instance).getCurrent() === undefined,
37
- suspender: (instance: SanityInstance) =>
38
- firstValueFrom(getActiveReleasesState(instance).observable.pipe(filter(Boolean))),
32
+ type UseActiveReleases = {
33
+ (options?: WithSourceNameSupport<SanityConfig> | undefined): ReleaseDocument[]
34
+ }
35
+
36
+ const useActiveReleasesValue: UseActiveReleases = createStateSourceHook({
37
+ getState: getActiveReleasesState as (
38
+ instance: SanityInstance,
39
+ options?: {source?: DocumentSource},
40
+ ) => StateSource<ReleaseDocument[]>,
41
+ shouldSuspend: (instance: SanityInstance, options?: {source?: DocumentSource}) =>
42
+ getActiveReleasesState(instance, options ?? {}).getCurrent() === undefined,
43
+ suspender: (instance: SanityInstance, options?: {source?: DocumentSource}) =>
44
+ firstValueFrom(
45
+ getActiveReleasesState(instance, options ?? {}).observable.pipe(filter(Boolean)),
46
+ ),
39
47
  })
48
+
49
+ /**
50
+ * @public
51
+ * @function
52
+ */
53
+ export const useActiveReleases: UseActiveReleases = (
54
+ options: WithSourceNameSupport<{source?: DocumentSource}> | undefined,
55
+ ) => {
56
+ const normalizedOptions = useNormalizedSourceOptions(options ?? {})
57
+ return useActiveReleasesValue(normalizedOptions)
58
+ }
@@ -1,20 +1,17 @@
1
1
  import {
2
- getActiveReleasesState,
2
+ type DatasetHandle,
3
+ type DocumentSource,
3
4
  getPerspectiveState,
4
- type PerspectiveHandle,
5
5
  type SanityInstance,
6
6
  type StateSource,
7
7
  } from '@sanity/sdk'
8
8
  import {filter, firstValueFrom} from 'rxjs'
9
9
 
10
10
  import {createStateSourceHook} from '../helpers/createStateSourceHook'
11
-
12
- /**
13
- * @public
14
- */
15
- type UsePerspective = {
16
- (perspectiveHandle: PerspectiveHandle): string | string[]
17
- }
11
+ import {
12
+ useNormalizedSourceOptions,
13
+ type WithSourceNameSupport,
14
+ } from '../helpers/useNormalizedSourceOptions'
18
15
 
19
16
  /**
20
17
  * @public
@@ -38,13 +35,28 @@ type UsePerspective = {
38
35
  *
39
36
  * @returns The perspective for the given perspective handle.
40
37
  */
41
- export const usePerspective: UsePerspective = createStateSourceHook({
38
+ type UsePerspective = {
39
+ (perspectiveHandle: DatasetHandle): string | string[]
40
+ }
41
+
42
+ const usePerspectiveValue: UsePerspective = createStateSourceHook({
42
43
  getState: getPerspectiveState as (
43
44
  instance: SanityInstance,
44
- perspectiveHandle?: PerspectiveHandle,
45
+ perspectiveHandle?: {source?: DocumentSource},
45
46
  ) => StateSource<string | string[]>,
46
- shouldSuspend: (instance: SanityInstance, options: PerspectiveHandle): boolean =>
47
+ shouldSuspend: (instance: SanityInstance, options: {source?: DocumentSource}): boolean =>
47
48
  getPerspectiveState(instance, options).getCurrent() === undefined,
48
- suspender: (instance: SanityInstance, _options?: PerspectiveHandle) =>
49
- firstValueFrom(getActiveReleasesState(instance).observable.pipe(filter(Boolean))),
49
+ suspender: (instance: SanityInstance, _options?: {source?: DocumentSource}) =>
50
+ firstValueFrom(getPerspectiveState(instance, _options ?? {}).observable.pipe(filter(Boolean))),
50
51
  })
52
+
53
+ /**
54
+ * @public
55
+ * @function
56
+ */
57
+ export const usePerspective: UsePerspective = (
58
+ options: WithSourceNameSupport<DatasetHandle> | undefined,
59
+ ) => {
60
+ const normalizedOptions = useNormalizedSourceOptions(options ?? {})
61
+ return usePerspectiveValue(normalizedOptions)
62
+ }
@@ -9,6 +9,7 @@ import {
9
9
  import {useEffect, useMemo, useState, useSyncExternalStore, useTransition} from 'react'
10
10
 
11
11
  import {useSanityInstance} from '../context/useSanityInstance'
12
+ import {trackHookUsage} from '../helpers/useTrackHookUsage'
12
13
 
13
14
  /**
14
15
  * @public
@@ -59,6 +60,7 @@ export interface UserResult {
59
60
  */
60
61
  export function useUser(options: GetUserOptions): UserResult {
61
62
  const instance = useSanityInstance(options)
63
+ trackHookUsage(instance, 'useUser')
62
64
  // Use React's useTransition to avoid UI jank when user options change
63
65
  const [isPending, startTransition] = useTransition()
64
66
 
@@ -10,6 +10,7 @@ import {
10
10
  import {useCallback, useEffect, useMemo, useState, useSyncExternalStore, useTransition} from 'react'
11
11
 
12
12
  import {useSanityInstance} from '../context/useSanityInstance'
13
+ import {trackHookUsage} from '../helpers/useTrackHookUsage'
13
14
 
14
15
  /**
15
16
  * @public
@@ -69,6 +70,7 @@ export interface UsersResult {
69
70
  */
70
71
  export function useUsers(options?: GetUsersOptions): UsersResult {
71
72
  const instance = useSanityInstance(options)
73
+ trackHookUsage(instance, 'useUsers')
72
74
  // Use React's useTransition to avoid UI jank when user options change
73
75
  const [isPending, startTransition] = useTransition()
74
76