@sanity/sdk-react 2.1.0 → 2.1.2-cors-check.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.
- package/dist/index.d.ts +89 -22
- package/dist/index.js +57 -17
- package/dist/index.js.map +1 -1
- package/package.json +27 -25
- package/src/_exports/sdk-react.ts +2 -0
- package/src/components/SanityApp.test.tsx +42 -0
- package/src/components/SanityApp.tsx +3 -2
- package/src/context/ResourceProvider.test.tsx +9 -5
- package/src/hooks/context/useSanityInstance.ts +2 -2
- package/src/hooks/dashboard/useManageFavorite.test.ts +2 -0
- package/src/hooks/dashboard/useNavigateToStudioDocument.test.ts +2 -0
- package/src/hooks/dashboard/useRecordDocumentHistoryEvent.test.ts +2 -0
- package/src/hooks/presence/usePresence.test.tsx +80 -0
- package/src/hooks/presence/usePresence.ts +23 -0
- package/src/hooks/preview/useDocumentPreview.tsx +1 -1
- package/src/hooks/projection/useDocumentProjection.ts +1 -1
- package/src/hooks/projects/useProjects.test.ts +38 -3
- package/src/hooks/projects/useProjects.ts +37 -26
- package/src/hooks/users/useUser.test.tsx +387 -0
- package/src/hooks/users/useUser.ts +109 -0
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
|
*
|
|
@@ -2097,30 +2107,38 @@ declare type UseProject = {
|
|
|
2097
2107
|
*/
|
|
2098
2108
|
export declare const useProject: UseProject
|
|
2099
2109
|
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
* ```tsx
|
|
2109
|
-
* const projects = useProjects()
|
|
2110
|
-
*
|
|
2111
|
-
* return (
|
|
2112
|
-
* <select>
|
|
2113
|
-
* {projects.map((project) => (
|
|
2114
|
-
* <option key={project.id}>{project.displayName}</option>
|
|
2115
|
-
* ))}
|
|
2116
|
-
* </select>
|
|
2117
|
-
* )
|
|
2118
|
-
* ```
|
|
2119
|
-
*/
|
|
2120
|
-
(): ProjectWithoutMembers[]
|
|
2121
|
-
}
|
|
2110
|
+
/**
|
|
2111
|
+
* @public
|
|
2112
|
+
* @category Types
|
|
2113
|
+
*/
|
|
2114
|
+
declare type UseProjects = <TIncludeMembers extends boolean = false>(options?: {
|
|
2115
|
+
organizationId?: string
|
|
2116
|
+
includeMembers?: TIncludeMembers
|
|
2117
|
+
}) => TIncludeMembers extends true ? SanityProject_2[] : ProjectWithoutMembers[]
|
|
2122
2118
|
|
|
2123
2119
|
/**
|
|
2120
|
+
* Returns metadata for each project you have access to.
|
|
2121
|
+
*
|
|
2122
|
+
* @category Projects
|
|
2123
|
+
* @param options - Configuration options
|
|
2124
|
+
* @returns An array of project metadata. If includeMembers is true, returns full SanityProject objects. Otherwise, returns ProjectWithoutMembers objects.
|
|
2125
|
+
* @example
|
|
2126
|
+
* ```tsx
|
|
2127
|
+
* const projects = useProjects()
|
|
2128
|
+
*
|
|
2129
|
+
* return (
|
|
2130
|
+
* <select>
|
|
2131
|
+
* {projects.map((project) => (
|
|
2132
|
+
* <option key={project.id}>{project.displayName}</option>
|
|
2133
|
+
* ))}
|
|
2134
|
+
* </select>
|
|
2135
|
+
* )
|
|
2136
|
+
* ```
|
|
2137
|
+
* @example
|
|
2138
|
+
* ```tsx
|
|
2139
|
+
* const projectsWithMembers = useProjects({ includeMembers: true })
|
|
2140
|
+
* const projectsWithoutMembers = useProjects({ includeMembers: false })
|
|
2141
|
+
* ```
|
|
2124
2142
|
* @public
|
|
2125
2143
|
* @function
|
|
2126
2144
|
*/
|
|
@@ -2291,6 +2309,21 @@ declare interface UseRecordDocumentHistoryEventProps extends DocumentHandle {
|
|
|
2291
2309
|
schemaName?: string
|
|
2292
2310
|
}
|
|
2293
2311
|
|
|
2312
|
+
/**
|
|
2313
|
+
* @public
|
|
2314
|
+
* @category Types
|
|
2315
|
+
*/
|
|
2316
|
+
export declare interface UserResult {
|
|
2317
|
+
/**
|
|
2318
|
+
* The user data fetched, or undefined if not found.
|
|
2319
|
+
*/
|
|
2320
|
+
data: SanityUser | undefined
|
|
2321
|
+
/**
|
|
2322
|
+
* Whether a user request is currently in progress
|
|
2323
|
+
*/
|
|
2324
|
+
isPending: boolean
|
|
2325
|
+
}
|
|
2326
|
+
|
|
2294
2327
|
/**
|
|
2295
2328
|
* @public
|
|
2296
2329
|
* @category Types
|
|
@@ -2406,6 +2439,40 @@ export declare const useSanityInstance: (config?: SanityConfig) => SanityInstanc
|
|
|
2406
2439
|
*/
|
|
2407
2440
|
export declare function useStudioWorkspacesByProjectIdDataset(): StudioWorkspacesResult
|
|
2408
2441
|
|
|
2442
|
+
/**
|
|
2443
|
+
*
|
|
2444
|
+
* @public
|
|
2445
|
+
*
|
|
2446
|
+
* Retrieves a single user by ID for a given resource (either a project or an organization).
|
|
2447
|
+
*
|
|
2448
|
+
* @category Users
|
|
2449
|
+
* @param options - The user ID, resource type, project ID, or organization ID
|
|
2450
|
+
* @returns The user data and loading state
|
|
2451
|
+
*
|
|
2452
|
+
* @example
|
|
2453
|
+
* ```
|
|
2454
|
+
* const { data, isPending } = useUser({
|
|
2455
|
+
* userId: 'gabc123',
|
|
2456
|
+
* resourceType: 'project',
|
|
2457
|
+
* projectId: 'my-project-id',
|
|
2458
|
+
* })
|
|
2459
|
+
*
|
|
2460
|
+
* return (
|
|
2461
|
+
* <div>
|
|
2462
|
+
* {isPending && <p>Loading...</p>}
|
|
2463
|
+
* {data && (
|
|
2464
|
+
* <figure>
|
|
2465
|
+
* <img src={data.profile.imageUrl} alt='' />
|
|
2466
|
+
* <figcaption>{data.profile.displayName}</figcaption>
|
|
2467
|
+
* <address>{data.profile.email}</address>
|
|
2468
|
+
* </figure>
|
|
2469
|
+
* )}
|
|
2470
|
+
* </div>
|
|
2471
|
+
* )
|
|
2472
|
+
* ```
|
|
2473
|
+
*/
|
|
2474
|
+
export declare function useUser(options: GetUserOptions): UserResult
|
|
2475
|
+
|
|
2409
2476
|
/**
|
|
2410
2477
|
*
|
|
2411
2478
|
* @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";
|
|
@@ -11,7 +11,7 @@ import { pick } from "lodash-es";
|
|
|
11
11
|
const SanityInstanceContext = createContext(null), useSanityInstance = (config) => {
|
|
12
12
|
const $ = c(3), instance = useContext(SanityInstanceContext);
|
|
13
13
|
if (!instance)
|
|
14
|
-
throw new Error(`SanityInstance context not found. ${config ? `Requested config: ${JSON.stringify(config, null, 2)}. ` : ""}Please ensure that your component is wrapped in a
|
|
14
|
+
throw new Error(`SanityInstance context not found. ${config ? `Requested config: ${JSON.stringify(config, null, 2)}. ` : ""}Please ensure that your component is wrapped in a ResourceProvider or a SanityApp component.`);
|
|
15
15
|
if (!config)
|
|
16
16
|
return instance;
|
|
17
17
|
let t0;
|
|
@@ -19,7 +19,7 @@ const SanityInstanceContext = createContext(null), useSanityInstance = (config)
|
|
|
19
19
|
const match = t0;
|
|
20
20
|
if (!match)
|
|
21
21
|
throw new Error(`Could not find a matching Sanity instance for the requested configuration: ${JSON.stringify(config, null, 2)}.
|
|
22
|
-
Please ensure there is a
|
|
22
|
+
Please ensure there is a ResourceProvider component with a matching configuration in the component hierarchy.`);
|
|
23
23
|
return match;
|
|
24
24
|
};
|
|
25
25
|
function createStateSourceHook(options) {
|
|
@@ -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(
|
|
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]
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
let
|
|
372
|
-
return
|
|
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;
|
|
@@ -999,7 +1017,7 @@ function useDocumentPreview(t0) {
|
|
|
999
1017
|
ref,
|
|
1000
1018
|
...docHandle
|
|
1001
1019
|
} = t0, $[0] = t0, $[1] = docHandle, $[2] = ref) : (docHandle = $[1], ref = $[2]);
|
|
1002
|
-
const instance = useSanityInstance();
|
|
1020
|
+
const instance = useSanityInstance(docHandle);
|
|
1003
1021
|
let t1;
|
|
1004
1022
|
$[3] !== docHandle || $[4] !== instance ? (t1 = getPreviewState(instance, docHandle), $[3] = docHandle, $[4] = instance, $[5] = t1) : t1 = $[5];
|
|
1005
1023
|
const stateSource = t1;
|
|
@@ -1040,7 +1058,7 @@ function useDocumentProjection(t0) {
|
|
|
1040
1058
|
projection,
|
|
1041
1059
|
...docHandle
|
|
1042
1060
|
} = t0, $[0] = t0, $[1] = docHandle, $[2] = projection, $[3] = ref) : (docHandle = $[1], projection = $[2], ref = $[3]);
|
|
1043
|
-
const instance = useSanityInstance();
|
|
1061
|
+
const instance = useSanityInstance(docHandle);
|
|
1044
1062
|
let stateSource, t1;
|
|
1045
1063
|
if ($[4] !== docHandle || $[5] !== instance || $[6] !== projection ? (stateSource = getProjectionState(instance, {
|
|
1046
1064
|
...docHandle,
|
|
@@ -1078,9 +1096,8 @@ const useProject = createStateSourceHook({
|
|
|
1078
1096
|
suspender: resolveProject,
|
|
1079
1097
|
getConfig: identity
|
|
1080
1098
|
}), useProjects = createStateSourceHook({
|
|
1081
|
-
// remove `undefined` since we're suspending when that is the case
|
|
1082
1099
|
getState: getProjectsState,
|
|
1083
|
-
shouldSuspend: (instance) => getProjectsState(instance).getCurrent() === void 0,
|
|
1100
|
+
shouldSuspend: (instance, options) => getProjectsState(instance, options).getCurrent() === void 0,
|
|
1084
1101
|
suspender: resolveProjects
|
|
1085
1102
|
}), useActiveReleases = createStateSourceHook({
|
|
1086
1103
|
getState: getActiveReleasesState,
|
|
@@ -1091,6 +1108,27 @@ const useProject = createStateSourceHook({
|
|
|
1091
1108
|
shouldSuspend: (instance, options) => getPerspectiveState(instance, options).getCurrent() === void 0,
|
|
1092
1109
|
suspender: (instance, _options) => firstValueFrom(getActiveReleasesState(instance).observable.pipe(filter(Boolean)))
|
|
1093
1110
|
});
|
|
1111
|
+
function useUser(options) {
|
|
1112
|
+
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());
|
|
1113
|
+
useEffect(() => {
|
|
1114
|
+
key !== deferredKey && startTransition(() => {
|
|
1115
|
+
ref.signal.aborted || (ref.abort(), setRef(new AbortController())), setDeferredKey(key);
|
|
1116
|
+
});
|
|
1117
|
+
}, [deferredKey, key, ref]);
|
|
1118
|
+
const {
|
|
1119
|
+
getCurrent,
|
|
1120
|
+
subscribe
|
|
1121
|
+
} = useMemo(() => getUsersState(instance, deferred), [instance, deferred]);
|
|
1122
|
+
if (getCurrent() === void 0)
|
|
1123
|
+
throw resolveUsers(instance, {
|
|
1124
|
+
...deferred,
|
|
1125
|
+
signal: ref.signal
|
|
1126
|
+
});
|
|
1127
|
+
return {
|
|
1128
|
+
data: useSyncExternalStore(subscribe, getCurrent)?.data[0],
|
|
1129
|
+
isPending
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1094
1132
|
function useUsers(options) {
|
|
1095
1133
|
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
1134
|
useEffect(() => {
|
|
@@ -1120,7 +1158,7 @@ function useUsers(options) {
|
|
|
1120
1158
|
loadMore
|
|
1121
1159
|
};
|
|
1122
1160
|
}
|
|
1123
|
-
var version = "2.1.0";
|
|
1161
|
+
var version = "2.1.2-cors-check.0";
|
|
1124
1162
|
function getEnv(key) {
|
|
1125
1163
|
if (typeof import.meta < "u" && import.meta.env)
|
|
1126
1164
|
return import.meta.env[key];
|
|
@@ -1162,12 +1200,14 @@ export {
|
|
|
1162
1200
|
useNavigateToStudioDocument,
|
|
1163
1201
|
usePaginatedDocuments,
|
|
1164
1202
|
usePerspective,
|
|
1203
|
+
usePresence,
|
|
1165
1204
|
useProject,
|
|
1166
1205
|
useProjects,
|
|
1167
1206
|
useQuery,
|
|
1168
1207
|
useRecordDocumentHistoryEvent,
|
|
1169
1208
|
useSanityInstance,
|
|
1170
1209
|
useStudioWorkspacesByProjectIdDataset,
|
|
1210
|
+
useUser,
|
|
1171
1211
|
useUsers,
|
|
1172
1212
|
useVerifyOrgProjects,
|
|
1173
1213
|
useWindowConnection
|