@sanity/sdk-react 2.1.0 → 2.1.1

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/dist/index.d.ts CHANGED
@@ -14,6 +14,7 @@ import {DocumentPermissionsResult} from '@sanity/sdk'
14
14
  import {FallbackProps} from 'react-error-boundary'
15
15
  import {FavoriteStatusResponse} from '@sanity/sdk'
16
16
  import {FrameMessage} from '@sanity/sdk'
17
+ import {GetUserOptions} from '@sanity/sdk'
17
18
  import {GetUsersOptions} from '@sanity/sdk'
18
19
  import {JsonMatch} from '@sanity/sdk'
19
20
  import {MediaResource} from '@sanity/message-protocol'
@@ -40,6 +41,7 @@ import {SanityQueryResult} from 'groq'
40
41
  import {SanityUser} from '@sanity/sdk'
41
42
  import {SortOrderingItem} from '@sanity/types'
42
43
  import {StudioResource} from '@sanity/message-protocol'
44
+ import {UserPresence} from '@sanity/sdk'
43
45
  import {ValidProjection} from '@sanity/sdk'
44
46
  import {WindowMessage} from '@sanity/sdk'
45
47
 
@@ -2067,6 +2069,14 @@ declare type UsePerspective = {
2067
2069
  */
2068
2070
  export declare const usePerspective: UsePerspective
2069
2071
 
2072
+ /**
2073
+ * A hook for subscribing to presence information for the current project.
2074
+ * @public
2075
+ */
2076
+ export declare function usePresence(): {
2077
+ locations: UserPresence[]
2078
+ }
2079
+
2070
2080
  declare type UseProject = {
2071
2081
  /**
2072
2082
  *
@@ -2291,6 +2301,21 @@ declare interface UseRecordDocumentHistoryEventProps extends DocumentHandle {
2291
2301
  schemaName?: string
2292
2302
  }
2293
2303
 
2304
+ /**
2305
+ * @public
2306
+ * @category Types
2307
+ */
2308
+ export declare interface UserResult {
2309
+ /**
2310
+ * The user data fetched, or undefined if not found.
2311
+ */
2312
+ data: SanityUser | undefined
2313
+ /**
2314
+ * Whether a user request is currently in progress
2315
+ */
2316
+ isPending: boolean
2317
+ }
2318
+
2294
2319
  /**
2295
2320
  * @public
2296
2321
  * @category Types
@@ -2406,6 +2431,40 @@ export declare const useSanityInstance: (config?: SanityConfig) => SanityInstanc
2406
2431
  */
2407
2432
  export declare function useStudioWorkspacesByProjectIdDataset(): StudioWorkspacesResult
2408
2433
 
2434
+ /**
2435
+ *
2436
+ * @public
2437
+ *
2438
+ * Retrieves a single user by ID for a given resource (either a project or an organization).
2439
+ *
2440
+ * @category Users
2441
+ * @param options - The user ID, resource type, project ID, or organization ID
2442
+ * @returns The user data and loading state
2443
+ *
2444
+ * @example
2445
+ * ```
2446
+ * const { data, isPending } = useUser({
2447
+ * userId: 'gabc123',
2448
+ * resourceType: 'project',
2449
+ * projectId: 'my-project-id',
2450
+ * })
2451
+ *
2452
+ * return (
2453
+ * <div>
2454
+ * {isPending && <p>Loading...</p>}
2455
+ * {data && (
2456
+ * <figure>
2457
+ * <img src={data.profile.imageUrl} alt='' />
2458
+ * <figcaption>{data.profile.displayName}</figcaption>
2459
+ * <address>{data.profile.email}</address>
2460
+ * </figure>
2461
+ * )}
2462
+ * </div>
2463
+ * )
2464
+ * ```
2465
+ */
2466
+ export declare function useUser(options: GetUserOptions): UserResult
2467
+
2409
2468
  /**
2410
2469
  *
2411
2470
  * @public
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
2
2
  import { c } from "react-compiler-runtime";
3
- import { getAuthState, getNodeState, getIsInDashboardState, setAuthToken, AuthStateType, getLoginUrlState, observeOrganizationVerificationState, handleAuthCallback, logout, createSanityInstance, getTokenState, getCurrentUserState, getDashboardOrganizationId, getClientState, getOrCreateController, getOrCreateChannel, releaseChannel, getFavoritesState, resolveFavoritesState, resolveDatasets, getDatasetsState, applyDocumentActions, resolveDocument, getDocumentState, subscribeDocumentEvents, getPermissionsState, getDocumentSyncStatus, editDocument, getQueryKey, parseQueryKey, getQueryState, resolveQuery, createGroqSearchFilter, getPreviewState, resolvePreview, getProjectionState, resolveProjection, resolveProject, getProjectState, resolveProjects, getProjectsState, getActiveReleasesState, getPerspectiveState, getUsersKey, parseUsersKey, getUsersState, resolveUsers, loadMoreUsers } from "@sanity/sdk";
3
+ import { getAuthState, getNodeState, getIsInDashboardState, setAuthToken, AuthStateType, getLoginUrlState, observeOrganizationVerificationState, handleAuthCallback, logout, createSanityInstance, getTokenState, getCurrentUserState, getDashboardOrganizationId, getClientState, getOrCreateController, getOrCreateChannel, releaseChannel, getFavoritesState, resolveFavoritesState, resolveDatasets, getDatasetsState, applyDocumentActions, resolveDocument, getDocumentState, subscribeDocumentEvents, getPermissionsState, getDocumentSyncStatus, editDocument, getQueryKey, parseQueryKey, getQueryState, resolveQuery, createGroqSearchFilter, getPresence, getPreviewState, resolvePreview, getProjectionState, resolveProjection, resolveProject, getProjectState, resolveProjects, getProjectsState, getActiveReleasesState, getPerspectiveState, getUsersKey, parseUsersKey, getUsersState, resolveUsers, loadMoreUsers } from "@sanity/sdk";
4
4
  export * from "@sanity/sdk";
5
5
  import { createContext, useContext, useSyncExternalStore, useRef, useEffect, useState, Suspense, useMemo, useCallback, useInsertionEffect, useTransition } from "react";
6
6
  import { ErrorBoundary } from "react-error-boundary";
@@ -351,7 +351,7 @@ function SDKProvider({
351
351
  }
352
352
  const REDIRECT_URL = "https://sanity.io/welcome";
353
353
  function SanityApp(t0) {
354
- const $ = c(13);
354
+ const $ = c(15);
355
355
  let children, fallback, props, t1;
356
356
  $[0] !== t0 ? ({
357
357
  children,
@@ -362,14 +362,14 @@ function SanityApp(t0) {
362
362
  let t2;
363
363
  $[5] !== t1 ? (t2 = t1 === void 0 ? [] : t1, $[5] = t1, $[6] = t2) : t2 = $[6];
364
364
  const config = t2;
365
- let t3;
366
- $[7] === Symbol.for("react.memo_cache_sentinel") ? (t3 = [], $[7] = t3) : t3 = $[7], useEffect(_temp2$1, t3);
367
- let t4;
368
- return $[8] !== children || $[9] !== config || $[10] !== fallback || $[11] !== props ? (t4 = /* @__PURE__ */ jsx(SDKProvider, { ...props, fallback, config, children }), $[8] = children, $[9] = config, $[10] = fallback, $[11] = props, $[12] = t4) : t4 = $[12], t4;
369
- }
370
- function _temp2$1() {
371
- let timeout;
372
- return !isInIframe() && !isLocalUrl(window) && (timeout = setTimeout(_temp$4, 1e3)), () => clearTimeout(timeout);
365
+ let t3, t4;
366
+ $[7] !== config ? (t3 = () => {
367
+ let timeout;
368
+ const primaryConfig = Array.isArray(config) ? config[0] : config;
369
+ return !isInIframe() && !isLocalUrl(window) && !primaryConfig?.studioMode?.enabled && (timeout = setTimeout(_temp$4, 1e3)), () => clearTimeout(timeout);
370
+ }, t4 = [config], $[7] = config, $[8] = t3, $[9] = t4) : (t3 = $[8], t4 = $[9]), useEffect(t3, t4);
371
+ let t5;
372
+ return $[10] !== children || $[11] !== config || $[12] !== fallback || $[13] !== props ? (t5 = /* @__PURE__ */ jsx(SDKProvider, { ...props, fallback, config, children }), $[10] = children, $[11] = config, $[12] = fallback, $[13] = props, $[14] = t5) : t5 = $[14], t5;
373
373
  }
374
374
  function _temp$4() {
375
375
  console.warn("Redirecting to core", REDIRECT_URL), window.location.replace(REDIRECT_URL);
@@ -992,6 +992,24 @@ function _temp2(str) {
992
992
  function _temp(i) {
993
993
  return typeof i == "string";
994
994
  }
995
+ function usePresence() {
996
+ const $ = c(11), sanityInstance = useSanityInstance();
997
+ let t0, t1;
998
+ $[0] !== sanityInstance ? (t1 = getPresence(sanityInstance), $[0] = sanityInstance, $[1] = t1) : t1 = $[1], t0 = t1;
999
+ const source = t0;
1000
+ let t2;
1001
+ $[2] !== source ? (t2 = (callback) => source.subscribe(callback), $[2] = source, $[3] = t2) : t2 = $[3];
1002
+ const subscribe = t2;
1003
+ let t3, t4;
1004
+ $[4] !== source ? (t3 = () => source.getCurrent(), t4 = () => source.getCurrent(), $[4] = source, $[5] = t3, $[6] = t4) : (t3 = $[5], t4 = $[6]);
1005
+ const locations = useSyncExternalStore(subscribe, t3, t4);
1006
+ let t5;
1007
+ $[7] !== locations ? (t5 = locations || [], $[7] = locations, $[8] = t5) : t5 = $[8];
1008
+ let t6;
1009
+ return $[9] !== t5 ? (t6 = {
1010
+ locations: t5
1011
+ }, $[9] = t5, $[10] = t6) : t6 = $[10], t6;
1012
+ }
995
1013
  function useDocumentPreview(t0) {
996
1014
  const $ = c(13);
997
1015
  let docHandle, ref;
@@ -1091,6 +1109,27 @@ const useProject = createStateSourceHook({
1091
1109
  shouldSuspend: (instance, options) => getPerspectiveState(instance, options).getCurrent() === void 0,
1092
1110
  suspender: (instance, _options) => firstValueFrom(getActiveReleasesState(instance).observable.pipe(filter(Boolean)))
1093
1111
  });
1112
+ function useUser(options) {
1113
+ const instance = useSanityInstance(options), [isPending, startTransition] = useTransition(), key = getUsersKey(instance, options), [deferredKey, setDeferredKey] = useState(key), deferred = useMemo(() => parseUsersKey(deferredKey), [deferredKey]), [ref, setRef] = useState(new AbortController());
1114
+ useEffect(() => {
1115
+ key !== deferredKey && startTransition(() => {
1116
+ ref.signal.aborted || (ref.abort(), setRef(new AbortController())), setDeferredKey(key);
1117
+ });
1118
+ }, [deferredKey, key, ref]);
1119
+ const {
1120
+ getCurrent,
1121
+ subscribe
1122
+ } = useMemo(() => getUsersState(instance, deferred), [instance, deferred]);
1123
+ if (getCurrent() === void 0)
1124
+ throw resolveUsers(instance, {
1125
+ ...deferred,
1126
+ signal: ref.signal
1127
+ });
1128
+ return {
1129
+ data: useSyncExternalStore(subscribe, getCurrent)?.data[0],
1130
+ isPending
1131
+ };
1132
+ }
1094
1133
  function useUsers(options) {
1095
1134
  const instance = useSanityInstance(options), [isPending, startTransition] = useTransition(), key = getUsersKey(instance, options), [deferredKey, setDeferredKey] = useState(key), deferred = useMemo(() => parseUsersKey(deferredKey), [deferredKey]), [ref, setRef] = useState(new AbortController());
1096
1135
  useEffect(() => {
@@ -1120,7 +1159,7 @@ function useUsers(options) {
1120
1159
  loadMore
1121
1160
  };
1122
1161
  }
1123
- var version = "2.1.0";
1162
+ var version = "2.1.1";
1124
1163
  function getEnv(key) {
1125
1164
  if (typeof import.meta < "u" && import.meta.env)
1126
1165
  return import.meta.env[key];
@@ -1162,12 +1201,14 @@ export {
1162
1201
  useNavigateToStudioDocument,
1163
1202
  usePaginatedDocuments,
1164
1203
  usePerspective,
1204
+ usePresence,
1165
1205
  useProject,
1166
1206
  useProjects,
1167
1207
  useQuery,
1168
1208
  useRecordDocumentHistoryEvent,
1169
1209
  useSanityInstance,
1170
1210
  useStudioWorkspacesByProjectIdDataset,
1211
+ useUser,
1171
1212
  useUsers,
1172
1213
  useVerifyOrgProjects,
1173
1214
  useWindowConnection