@sanity/sdk-react 0.0.0-alpha.14 → 0.0.0-alpha.15
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 +15 -4
- package/dist/_chunks-es/useLogOut.js.map +1 -1
- package/dist/components.d.ts +4 -4
- package/dist/components.js +27 -9
- package/dist/components.js.map +1 -1
- package/dist/context.d.ts +2 -2
- package/dist/hooks.d.ts +86 -8
- package/dist/hooks.js +56 -12
- package/dist/hooks.js.map +1 -1
- package/package.json +4 -4
- package/src/_exports/hooks.ts +1 -0
- package/src/components/Login/LoginLinks.test.tsx +1 -1
- package/src/components/SDKProvider.test.tsx +7 -7
- package/src/components/SDKProvider.tsx +6 -4
- package/src/components/SanityApp.test.tsx +103 -1
- package/src/components/SanityApp.tsx +32 -11
- 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/context/useSanityInstance.test.tsx +1 -1
- package/src/hooks/context/useSanityInstance.ts +19 -3
- package/src/hooks/document/useDocument.test.ts +2 -2
- package/src/hooks/document/useDocument.ts +8 -6
- package/src/hooks/document/useDocumentEvent.test.ts +13 -3
- package/src/hooks/document/useDocumentEvent.ts +11 -3
- package/src/hooks/document/useDocumentSyncStatus.ts +1 -1
- package/src/hooks/document/useEditDocument.test.ts +19 -12
- package/src/hooks/document/useEditDocument.ts +11 -9
- package/src/hooks/document/usePermissions.ts +19 -2
- package/src/hooks/documentCollection/useDocuments.ts +1 -1
- package/src/hooks/helpers/createStateSourceHook.tsx +8 -2
- package/src/hooks/projection/useProjection.test.tsx +218 -0
- package/src/hooks/projection/useProjection.ts +135 -0
- package/src/hooks/users/useUsers.ts +1 -1
package/README.md
CHANGED
|
@@ -7,32 +7,49 @@
|
|
|
7
7
|
|
|
8
8
|
React hooks for creating Sanity applications.
|
|
9
9
|
|
|
10
|
-
## Installation
|
|
10
|
+
## 💻 Installation
|
|
11
11
|
|
|
12
12
|
```bash
|
|
13
13
|
npm i @sanity/sdk-react @sanity/sdk
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
> 💡 Looking to build a Sanity application? Check out the [Quick Start](#quick-start) section.
|
|
17
|
+
|
|
18
|
+
## 📚 SDK Documentation
|
|
17
19
|
|
|
18
20
|
See the [SDK Documentation](https://sdk-docs.sanity.dev) for more information.
|
|
19
21
|
|
|
20
|
-
## Quick Start
|
|
22
|
+
## 🚀 Quick Start
|
|
21
23
|
|
|
22
24
|
Here's how to implement your Sanity application:
|
|
23
25
|
|
|
26
|
+
1. Create a new React TypeScript project using the Sanity template
|
|
27
|
+
|
|
24
28
|
```bash
|
|
25
|
-
|
|
26
|
-
npm create vite@latest my-content-os-app -- --template react-ts -y
|
|
29
|
+
pnpx sanity@latest init --template core-app
|
|
27
30
|
cd my-content-os-app
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
2. Install dependencies
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm i
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
3. Run the app
|
|
40
|
+
|
|
41
|
+
```bash
|
|
31
42
|
npm run dev
|
|
32
|
-
# In another terminal, run the Sanity CoreUI
|
|
33
|
-
npx @sanity/os-cli run --url=http://localhost:5173/
|
|
34
43
|
```
|
|
35
44
|
|
|
45
|
+
4. Open the App in Sanity Dashboard with your organization ID
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
https://core.sanity.io/<your-organization-id>?dev=localhost:5173
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
5. Overwrite the `src/App.tsx` file with the following code:
|
|
52
|
+
|
|
36
53
|
```tsx
|
|
37
54
|
// src/App.tsx
|
|
38
55
|
import {SanityConfig} from '@sanity/sdk'
|
|
@@ -41,19 +58,16 @@ import {useCurrentUser, useLogOut} from '@sanity/sdk-react/hooks'
|
|
|
41
58
|
|
|
42
59
|
import './App.css'
|
|
43
60
|
|
|
44
|
-
const sanityConfig:
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
// ...
|
|
51
|
-
// },
|
|
52
|
-
}
|
|
61
|
+
const sanityConfig: SanityConfigs = [
|
|
62
|
+
{
|
|
63
|
+
projectId: '<your-project-id>',
|
|
64
|
+
dataset: '<your-dataset>',
|
|
65
|
+
},
|
|
66
|
+
]
|
|
53
67
|
|
|
54
68
|
export function App(): JSX.Element {
|
|
55
69
|
return (
|
|
56
|
-
<SanityApp
|
|
70
|
+
<SanityApp sanityConfigs={sanityConfigs}>
|
|
57
71
|
<MyApp />
|
|
58
72
|
</SanityApp>
|
|
59
73
|
)
|
|
@@ -74,6 +88,8 @@ function MyApp() {
|
|
|
74
88
|
export default App
|
|
75
89
|
```
|
|
76
90
|
|
|
91
|
+
6. Overwrite the `src/App.css` file with the following code:
|
|
92
|
+
|
|
77
93
|
```css
|
|
78
94
|
/* src/App.css */
|
|
79
95
|
#root {
|
|
@@ -135,19 +151,6 @@ export default App
|
|
|
135
151
|
}
|
|
136
152
|
```
|
|
137
153
|
|
|
138
|
-
## TypeScript Support
|
|
139
|
-
|
|
140
|
-
This package includes TypeScript definitions. You can import types like:
|
|
141
|
-
|
|
142
|
-
```tsx
|
|
143
|
-
import type {
|
|
144
|
-
SanityProviderProps,
|
|
145
|
-
AuthBoundaryProps,
|
|
146
|
-
LoginLayoutProps,
|
|
147
|
-
LoginErrorProps,
|
|
148
|
-
} from '@sanity/react'
|
|
149
|
-
```
|
|
150
|
-
|
|
151
154
|
## License
|
|
152
155
|
|
|
153
156
|
MIT © Sanity.io
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import { createContext } from "react";
|
|
3
|
-
const SanityInstanceContext = createContext(null), SanityProvider = ({ children,
|
|
3
|
+
const SanityInstanceContext = createContext(null), SanityProvider = ({ children, sanityInstances }) => /* @__PURE__ */ jsx(SanityInstanceContext.Provider, { value: sanityInstances, children });
|
|
4
4
|
export {
|
|
5
5
|
SanityInstanceContext,
|
|
6
6
|
SanityProvider
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sources":["../../src/context/SanityProvider.tsx"],"sourcesContent":["import {type SanityInstance} from '@sanity/sdk'\nimport {createContext, type ReactElement} from 'react'\n\n/**\n * @internal\n */\nexport interface SanityProviderProps {\n children: React.ReactNode\n
|
|
1
|
+
{"version":3,"file":"context.js","sources":["../../src/context/SanityProvider.tsx"],"sourcesContent":["import {type SanityInstance} from '@sanity/sdk'\nimport {createContext, type ReactElement} from 'react'\n\n/**\n * @internal\n */\nexport interface SanityProviderProps {\n children: React.ReactNode\n sanityInstances: SanityInstance[]\n}\n\nexport const SanityInstanceContext = createContext<SanityInstance[] | null>(null)\n\n/**\n * @internal\n *\n * Top-level context provider that provides access to the Sanity configuration instance.\n * This must wrap any components making use of the Sanity SDK React hooks.\n *\n * @remarks In most cases, SanityApp should be used rather than SanityProvider directly; SanityApp bundles both SanityProvider and an authentication layer.\n * @param props - Sanity project and dataset configuration\n * @returns Rendered component\n * @example\n * ```tsx\n * import {createSanityInstance} from '@sanity/sdk'\n * import {SanityProvider} from '@sanity/sdk-react'\n *\n * import MyAppRoot from './Root'\n *\n * const sanityInstance = createSanityInstance({\n * projectId: 'your-project-id',\n * dataset: 'production',\n * })\n *\n * export default function MyApp() {\n * return (\n * <SanityProvider sanityInstance={sanityInstance}>\n * <MyAppRoot />\n * </SanityProvider>\n * )\n * }\n * ```\n */\nexport const SanityProvider = ({children, sanityInstances}: SanityProviderProps): ReactElement => {\n return (\n <SanityInstanceContext.Provider value={sanityInstances}>\n {children}\n </SanityInstanceContext.Provider>\n )\n}\n"],"names":[],"mappings":";;AAWO,MAAM,wBAAwB,cAAuC,IAAI,GAgCnE,iBAAiB,CAAC,EAAC,UAAU,gBAAe,0BAEpD,sBAAsB,UAAtB,EAA+B,OAAO,iBACpC,SACH,CAAA;"}
|
|
@@ -1,16 +1,27 @@
|
|
|
1
1
|
import { getAuthState, handleCallback, getLoginUrlsState, fetchLoginUrls, logout } from "@sanity/sdk";
|
|
2
2
|
import { useContext, useMemo, useSyncExternalStore, useCallback } from "react";
|
|
3
3
|
import { SanityInstanceContext } from "./context.js";
|
|
4
|
-
const useSanityInstance = () => {
|
|
4
|
+
const useSanityInstance = (resourceId) => {
|
|
5
5
|
const sanityInstance = useContext(SanityInstanceContext);
|
|
6
6
|
if (!sanityInstance)
|
|
7
7
|
throw new Error("useSanityInstance must be called from within the SanityProvider");
|
|
8
|
-
|
|
8
|
+
if (sanityInstance.length === 0)
|
|
9
|
+
throw new Error("No Sanity instances found");
|
|
10
|
+
if (sanityInstance.length === 1 || !resourceId)
|
|
11
|
+
return sanityInstance[0];
|
|
12
|
+
if (!resourceId)
|
|
13
|
+
throw new Error("resourceId is required when there are multiple Sanity instances");
|
|
14
|
+
const instance = sanityInstance.find((inst) => inst.identity.resourceId === resourceId);
|
|
15
|
+
if (!instance)
|
|
16
|
+
throw new Error(`Sanity instance with resourceId ${resourceId} not found`);
|
|
17
|
+
return instance;
|
|
9
18
|
};
|
|
10
19
|
function createStateSourceHook(options) {
|
|
11
|
-
const getState = typeof options == "function" ? options : options.getState, suspense = "shouldSuspend" in options && "suspender" in options ? options : void 0;
|
|
20
|
+
const getState = typeof options == "function" ? options : options.getState, getResourceId = "getResourceId" in options ? options.getResourceId : void 0, suspense = "shouldSuspend" in options && "suspender" in options ? options : void 0;
|
|
12
21
|
function useHook(...params) {
|
|
13
|
-
|
|
22
|
+
let resourceId;
|
|
23
|
+
getResourceId && (resourceId = getResourceId(...params));
|
|
24
|
+
const instance = useSanityInstance(resourceId);
|
|
14
25
|
if (suspense?.shouldSuspend(instance, ...params))
|
|
15
26
|
throw suspense.suspender(instance, ...params);
|
|
16
27
|
const state = useMemo(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLogOut.js","sources":["../../src/hooks/context/useSanityInstance.ts","../../src/hooks/helpers/createStateSourceHook.tsx","../../src/hooks/auth/useAuthState.tsx","../../src/hooks/helpers/createCallbackHook.tsx","../../src/hooks/auth/useHandleCallback.tsx","../../src/hooks/auth/useLoginUrls.tsx","../../src/hooks/auth/useLogOut.tsx"],"sourcesContent":["import {type SanityInstance} from '@sanity/sdk'\nimport {useContext} from 'react'\n\nimport {SanityInstanceContext} from '../../context/SanityProvider'\n\n/**\n * `useSanityInstance` returns the current Sanity instance from the application context.\n * This must be called from within a `SanityProvider` component.\n * @internal\n * @returns The current Sanity instance\n * @example\n * ```tsx\n * const instance = useSanityInstance()\n * ```\n */\nexport const useSanityInstance = (): SanityInstance => {\n const sanityInstance = useContext(SanityInstanceContext)\n if (!sanityInstance) {\n throw new Error('useSanityInstance must be called from within the SanityProvider')\n }\n\n return sanityInstance\n}\n","import {type SanityInstance, type StateSource} from '@sanity/sdk'\nimport {useMemo, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\ntype StateSourceFactory<TParams extends unknown[], TState> = (\n instance: SanityInstance,\n ...params: TParams\n) => StateSource<TState>\n\ninterface CreateStateSourceHookOptions<TParams extends unknown[], TState> {\n getState: StateSourceFactory<TParams, TState>\n shouldSuspend: (instance: SanityInstance, ...params: TParams) => boolean\n suspender: (instance: SanityInstance, ...params: TParams) => Promise<unknown>\n}\n\nexport function createStateSourceHook<TParams extends unknown[], TState>(\n options: StateSourceFactory<TParams, TState> | CreateStateSourceHookOptions<TParams, TState>,\n): (...params: TParams) => TState {\n const getState = typeof options === 'function' ? options : options.getState\n const suspense = 'shouldSuspend' in options && 'suspender' in options ? options : undefined\n\n function useHook(...params: TParams) {\n const instance = useSanityInstance()\n if (suspense?.shouldSuspend(instance, ...params)) {\n throw suspense.suspender(instance, ...params)\n }\n\n const state = useMemo(\n () => getState(instance, ...params),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [instance, ...params],\n )\n return useSyncExternalStore(state.subscribe, state.getCurrent)\n }\n\n return useHook\n}\n","import {type AuthState, getAuthState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * @internal\n * A React hook that subscribes to authentication state changes.\n *\n * This hook provides access to the current authentication state type from the Sanity auth store.\n * It automatically re-renders when the authentication state changes.\n *\n * @remarks\n * The hook uses `useSyncExternalStore` to safely subscribe to auth state changes\n * and ensure consistency between server and client rendering.\n *\n * @returns The current authentication state type\n *\n * @example\n * ```tsx\n * function AuthStatus() {\n * const authState = useAuthState()\n * return <div>Current auth state: {authState}</div>\n * }\n * ```\n */\nexport const useAuthState: () => AuthState = createStateSourceHook(getAuthState)\n","import {type SanityInstance} from '@sanity/sdk'\nimport {useCallback} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\nexport function createCallbackHook<TParams extends unknown[], TReturn>(\n callback: (instance: SanityInstance, ...params: TParams) => TReturn,\n): () => (...params: TParams) => TReturn {\n function useHook() {\n const instance = useSanityInstance()\n return useCallback((...params: TParams) => callback(instance, ...params), [instance])\n }\n\n return useHook\n}\n","import {handleCallback} from '@sanity/sdk'\n\nimport {createCallbackHook} from '../helpers/createCallbackHook'\n\n/**\n * @internal\n * A React hook that returns a function for handling authentication callbacks.\n *\n * @remarks\n * This hook provides access to the authentication store's callback handler,\n * which processes auth redirects by extracting the session ID and fetching the\n * authentication token. If fetching the long-lived token is successful,\n * `handleCallback` will return a Promise that resolves a new location that\n * removes the short-lived token from the URL. Use this in combination with\n * `history.replaceState` or your own router's `replace` function to update the\n * current location without triggering a reload.\n *\n * @example\n * ```tsx\n * function AuthCallback() {\n * const handleCallback = useHandleCallback()\n * const router = useRouter() // Example router\n *\n * useEffect(() => {\n * async function processCallback() {\n * // Handle the callback and get the cleaned URL\n * const newUrl = await handleCallback(window.location.href)\n *\n * if (newUrl) {\n * // Replace URL without triggering navigation\n * router.replace(newUrl, {shallow: true})\n * }\n * }\n *\n * processCallback().catch(console.error)\n * }, [handleCallback, router])\n *\n * return <div>Completing login...</div>\n * }\n * ```\n *\n * @returns A callback handler function that processes OAuth redirects\n * @public\n */\nexport const useHandleCallback = createCallbackHook(handleCallback)\n","import {type AuthProvider, fetchLoginUrls, getLoginUrlsState} from '@sanity/sdk'\nimport {useMemo, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n * A React hook that retrieves the available authentication provider URLs for login.\n *\n * @remarks\n * This hook fetches the login URLs from the Sanity auth store when the component mounts.\n * Each provider object contains information about an authentication method, including its URL.\n * The hook will suspend if the login URLs have not yet loaded.\n *\n * @example\n * ```tsx\n * // LoginProviders component that uses the hook\n * function LoginProviders() {\n * const providers = useLoginUrls()\n *\n * return (\n * <div>\n * {providers.map((provider) => (\n * <a key={provider.name} href={provider.url}>\n * Login with {provider.title}\n * </a>\n * ))}\n * </div>\n * )\n * }\n *\n * // Parent component with Suspense boundary\n * function LoginPage() {\n * return (\n * <Suspense fallback={<div>Loading authentication providers...</div>}>\n * <LoginProviders />\n * </Suspense>\n * )\n * }\n * ```\n *\n * @returns An array of {@link AuthProvider} objects containing login URLs and provider information\n * @public\n */\nexport function useLoginUrls(): AuthProvider[] {\n const instance = useSanityInstance()\n const {subscribe, getCurrent} = useMemo(() => getLoginUrlsState(instance), [instance])\n\n if (!getCurrent()) throw fetchLoginUrls(instance)\n\n return useSyncExternalStore(subscribe, getCurrent as () => AuthProvider[])\n}\n","import {logout} from '@sanity/sdk'\n\nimport {createCallbackHook} from '../helpers/createCallbackHook'\n\n/**\n * Hook to log out of the current session\n * @internal\n * @returns A function to log out of the current session\n */\nexport const useLogOut = createCallbackHook(logout)\n"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"useLogOut.js","sources":["../../src/hooks/context/useSanityInstance.ts","../../src/hooks/helpers/createStateSourceHook.tsx","../../src/hooks/auth/useAuthState.tsx","../../src/hooks/helpers/createCallbackHook.tsx","../../src/hooks/auth/useHandleCallback.tsx","../../src/hooks/auth/useLoginUrls.tsx","../../src/hooks/auth/useLogOut.tsx"],"sourcesContent":["import {type SanityInstance} from '@sanity/sdk'\nimport {useContext} from 'react'\n\nimport {SanityInstanceContext} from '../../context/SanityProvider'\n\n/**\n * `useSanityInstance` returns the current Sanity instance from the application context.\n * This must be called from within a `SanityProvider` component.\n * @internal\n *\n * @param resourceId - The resourceId of the Sanity instance to return (optional)\n * @returns The current Sanity instance\n * @example\n * ```tsx\n * const instance = useSanityInstance('abc123:production')\n * ```\n */\nexport const useSanityInstance = (resourceId?: string): SanityInstance => {\n const sanityInstance = useContext(SanityInstanceContext)\n if (!sanityInstance) {\n throw new Error('useSanityInstance must be called from within the SanityProvider')\n }\n if (sanityInstance.length === 0) {\n throw new Error('No Sanity instances found')\n }\n if (sanityInstance.length === 1 || !resourceId) {\n return sanityInstance[0]\n }\n\n if (!resourceId) {\n throw new Error('resourceId is required when there are multiple Sanity instances')\n }\n\n const instance = sanityInstance.find((inst) => inst.identity.resourceId === resourceId)\n if (!instance) {\n throw new Error(`Sanity instance with resourceId ${resourceId} not found`)\n }\n return instance\n}\n","import {type ResourceId, type SanityInstance, type StateSource} from '@sanity/sdk'\nimport {useMemo, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\ntype StateSourceFactory<TParams extends unknown[], TState> = (\n instance: SanityInstance,\n ...params: TParams\n) => StateSource<TState>\n\ninterface CreateStateSourceHookOptions<TParams extends unknown[], TState> {\n getState: StateSourceFactory<TParams, TState>\n shouldSuspend: (instance: SanityInstance, ...params: TParams) => boolean\n suspender: (instance: SanityInstance, ...params: TParams) => Promise<unknown>\n getResourceId?: (...params: TParams) => ResourceId\n}\n\nexport function createStateSourceHook<TParams extends unknown[], TState>(\n options: StateSourceFactory<TParams, TState> | CreateStateSourceHookOptions<TParams, TState>,\n): (...params: TParams) => TState {\n const getState = typeof options === 'function' ? options : options.getState\n const getResourceId = 'getResourceId' in options ? options.getResourceId : undefined\n const suspense = 'shouldSuspend' in options && 'suspender' in options ? options : undefined\n\n function useHook(...params: TParams) {\n let resourceId: ResourceId | undefined\n if (getResourceId) {\n resourceId = getResourceId(...params)\n }\n const instance = useSanityInstance(resourceId)\n if (suspense?.shouldSuspend(instance, ...params)) {\n throw suspense.suspender(instance, ...params)\n }\n\n const state = useMemo(\n () => getState(instance, ...params),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [instance, ...params],\n )\n return useSyncExternalStore(state.subscribe, state.getCurrent)\n }\n\n return useHook\n}\n","import {type AuthState, getAuthState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * @internal\n * A React hook that subscribes to authentication state changes.\n *\n * This hook provides access to the current authentication state type from the Sanity auth store.\n * It automatically re-renders when the authentication state changes.\n *\n * @remarks\n * The hook uses `useSyncExternalStore` to safely subscribe to auth state changes\n * and ensure consistency between server and client rendering.\n *\n * @returns The current authentication state type\n *\n * @example\n * ```tsx\n * function AuthStatus() {\n * const authState = useAuthState()\n * return <div>Current auth state: {authState}</div>\n * }\n * ```\n */\nexport const useAuthState: () => AuthState = createStateSourceHook(getAuthState)\n","import {type SanityInstance} from '@sanity/sdk'\nimport {useCallback} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\nexport function createCallbackHook<TParams extends unknown[], TReturn>(\n callback: (instance: SanityInstance, ...params: TParams) => TReturn,\n): () => (...params: TParams) => TReturn {\n function useHook() {\n const instance = useSanityInstance()\n return useCallback((...params: TParams) => callback(instance, ...params), [instance])\n }\n\n return useHook\n}\n","import {handleCallback} from '@sanity/sdk'\n\nimport {createCallbackHook} from '../helpers/createCallbackHook'\n\n/**\n * @internal\n * A React hook that returns a function for handling authentication callbacks.\n *\n * @remarks\n * This hook provides access to the authentication store's callback handler,\n * which processes auth redirects by extracting the session ID and fetching the\n * authentication token. If fetching the long-lived token is successful,\n * `handleCallback` will return a Promise that resolves a new location that\n * removes the short-lived token from the URL. Use this in combination with\n * `history.replaceState` or your own router's `replace` function to update the\n * current location without triggering a reload.\n *\n * @example\n * ```tsx\n * function AuthCallback() {\n * const handleCallback = useHandleCallback()\n * const router = useRouter() // Example router\n *\n * useEffect(() => {\n * async function processCallback() {\n * // Handle the callback and get the cleaned URL\n * const newUrl = await handleCallback(window.location.href)\n *\n * if (newUrl) {\n * // Replace URL without triggering navigation\n * router.replace(newUrl, {shallow: true})\n * }\n * }\n *\n * processCallback().catch(console.error)\n * }, [handleCallback, router])\n *\n * return <div>Completing login...</div>\n * }\n * ```\n *\n * @returns A callback handler function that processes OAuth redirects\n * @public\n */\nexport const useHandleCallback = createCallbackHook(handleCallback)\n","import {type AuthProvider, fetchLoginUrls, getLoginUrlsState} from '@sanity/sdk'\nimport {useMemo, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n * A React hook that retrieves the available authentication provider URLs for login.\n *\n * @remarks\n * This hook fetches the login URLs from the Sanity auth store when the component mounts.\n * Each provider object contains information about an authentication method, including its URL.\n * The hook will suspend if the login URLs have not yet loaded.\n *\n * @example\n * ```tsx\n * // LoginProviders component that uses the hook\n * function LoginProviders() {\n * const providers = useLoginUrls()\n *\n * return (\n * <div>\n * {providers.map((provider) => (\n * <a key={provider.name} href={provider.url}>\n * Login with {provider.title}\n * </a>\n * ))}\n * </div>\n * )\n * }\n *\n * // Parent component with Suspense boundary\n * function LoginPage() {\n * return (\n * <Suspense fallback={<div>Loading authentication providers...</div>}>\n * <LoginProviders />\n * </Suspense>\n * )\n * }\n * ```\n *\n * @returns An array of {@link AuthProvider} objects containing login URLs and provider information\n * @public\n */\nexport function useLoginUrls(): AuthProvider[] {\n const instance = useSanityInstance()\n const {subscribe, getCurrent} = useMemo(() => getLoginUrlsState(instance), [instance])\n\n if (!getCurrent()) throw fetchLoginUrls(instance)\n\n return useSyncExternalStore(subscribe, getCurrent as () => AuthProvider[])\n}\n","import {logout} from '@sanity/sdk'\n\nimport {createCallbackHook} from '../helpers/createCallbackHook'\n\n/**\n * Hook to log out of the current session\n * @internal\n * @returns A function to log out of the current session\n */\nexport const useLogOut = createCallbackHook(logout)\n"],"names":[],"mappings":";;;AAiBa,MAAA,oBAAoB,CAAC,eAAwC;AAClE,QAAA,iBAAiB,WAAW,qBAAqB;AACvD,MAAI,CAAC;AACG,UAAA,IAAI,MAAM,iEAAiE;AAEnF,MAAI,eAAe,WAAW;AACtB,UAAA,IAAI,MAAM,2BAA2B;AAEzC,MAAA,eAAe,WAAW,KAAK,CAAC;AAClC,WAAO,eAAe,CAAC;AAGzB,MAAI,CAAC;AACG,UAAA,IAAI,MAAM,iEAAiE;AAG7E,QAAA,WAAW,eAAe,KAAK,CAAC,SAAS,KAAK,SAAS,eAAe,UAAU;AACtF,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,mCAAmC,UAAU,YAAY;AAEpE,SAAA;AACT;ACrBO,SAAS,sBACd,SACgC;AAChC,QAAM,WAAW,OAAO,WAAY,aAAa,UAAU,QAAQ,UAC7D,gBAAgB,mBAAmB,UAAU,QAAQ,gBAAgB,QACrE,WAAW,mBAAmB,WAAW,eAAe,UAAU,UAAU;AAElF,WAAS,WAAW,QAAiB;AAC/B,QAAA;AACA,sBACF,aAAa,cAAc,GAAG,MAAM;AAEhC,UAAA,WAAW,kBAAkB,UAAU;AAC7C,QAAI,UAAU,cAAc,UAAU,GAAG,MAAM;AAC7C,YAAM,SAAS,UAAU,UAAU,GAAG,MAAM;AAG9C,UAAM,QAAQ;AAAA,MACZ,MAAM,SAAS,UAAU,GAAG,MAAM;AAAA;AAAA,MAElC,CAAC,UAAU,GAAG,MAAM;AAAA,IACtB;AACA,WAAO,qBAAqB,MAAM,WAAW,MAAM,UAAU;AAAA,EAAA;AAGxD,SAAA;AACT;AClBa,MAAA,eAAgC,sBAAsB,YAAY;ACpBxE,SAAS,mBACd,UACuC;AACvC,WAAS,UAAU;AACjB,UAAM,WAAW,kBAAkB;AAC5B,WAAA,YAAY,IAAI,WAAoB,SAAS,UAAU,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC;AAAA,EAAA;AAG/E,SAAA;AACT;AC8Ba,MAAA,oBAAoB,mBAAmB,cAAc;ACA3D,SAAS,eAA+B;AAC7C,QAAM,WAAW,qBACX,EAAC,WAAW,WAAU,IAAI,QAAQ,MAAM,kBAAkB,QAAQ,GAAG,CAAC,QAAQ,CAAC;AAErF,MAAI,CAAC,WAAA,EAAc,OAAM,eAAe,QAAQ;AAEzC,SAAA,qBAAqB,WAAW,UAAkC;AAC3E;AC1Ca,MAAA,YAAY,mBAAmB,MAAM;"}
|
package/dist/components.d.ts
CHANGED
|
@@ -98,13 +98,13 @@ declare interface LoginLayoutProps {
|
|
|
98
98
|
* }
|
|
99
99
|
* ```
|
|
100
100
|
*/
|
|
101
|
-
export declare function SanityApp({
|
|
101
|
+
export declare function SanityApp({sanityConfigs, children}: SanityAppProps): ReactElement
|
|
102
102
|
|
|
103
103
|
/**
|
|
104
104
|
* @public
|
|
105
105
|
*/
|
|
106
106
|
export declare interface SanityAppProps {
|
|
107
|
-
|
|
107
|
+
sanityConfigs: SanityConfig[]
|
|
108
108
|
children: React.ReactNode
|
|
109
109
|
}
|
|
110
110
|
|
|
@@ -113,14 +113,14 @@ export declare interface SanityAppProps {
|
|
|
113
113
|
*
|
|
114
114
|
* Top-level context provider that provides access to the Sanity SDK.
|
|
115
115
|
*/
|
|
116
|
-
export declare function SDKProvider({children,
|
|
116
|
+
export declare function SDKProvider({children, sanityConfigs}: SDKProviderProps): ReactElement
|
|
117
117
|
|
|
118
118
|
/**
|
|
119
119
|
* @internal
|
|
120
120
|
*/
|
|
121
121
|
export declare interface SDKProviderProps {
|
|
122
122
|
children: ReactNode
|
|
123
|
-
|
|
123
|
+
sanityConfigs: SanityConfig[]
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
export {}
|
package/dist/components.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsxs, jsx } from "react/jsx-runtime";
|
|
2
2
|
import { AuthStateType, createSanityInstance } from "@sanity/sdk";
|
|
3
|
-
import { Fragment, Suspense, useEffect, useCallback, useMemo } from "react";
|
|
3
|
+
import { Fragment, Suspense, useEffect, useCallback, useMemo, useState } from "react";
|
|
4
4
|
import { ErrorBoundary } from "react-error-boundary";
|
|
5
5
|
import { useLoginUrls, useHandleCallback, useLogOut, useAuthState } from "./_chunks-es/useLogOut.js";
|
|
6
6
|
import { SanityLogo } from "@sanity/logos";
|
|
@@ -8,6 +8,10 @@ import { SanityProvider } from "./_chunks-es/context.js";
|
|
|
8
8
|
function isInIframe() {
|
|
9
9
|
return typeof window < "u" && window.self !== window.top;
|
|
10
10
|
}
|
|
11
|
+
function isLocalUrl(window2) {
|
|
12
|
+
const url = typeof window2 < "u" ? window2.location.href : "";
|
|
13
|
+
return url.startsWith("http://localhost") || url.startsWith("https://localhost") || url.startsWith("http://127.0.0.1") || url.startsWith("https://127.0.0.1");
|
|
14
|
+
}
|
|
11
15
|
class AuthError extends Error {
|
|
12
16
|
constructor(error) {
|
|
13
17
|
typeof error == "object" && error && "message" in error && typeof error.message == "string" ? super(error.message) : super(), this.cause = error;
|
|
@@ -125,15 +129,29 @@ function AuthSwitch({
|
|
|
125
129
|
return /* @__PURE__ */ jsx(LoginComponent, { ...props });
|
|
126
130
|
}
|
|
127
131
|
}
|
|
128
|
-
function SDKProvider({ children,
|
|
129
|
-
const
|
|
130
|
-
|
|
132
|
+
function SDKProvider({ children, sanityConfigs }) {
|
|
133
|
+
const sanityInstances = sanityConfigs.map(
|
|
134
|
+
(sanityConfig) => createSanityInstance(sanityConfig)
|
|
135
|
+
);
|
|
136
|
+
return /* @__PURE__ */ jsx(SanityProvider, { sanityInstances, children: /* @__PURE__ */ jsx(AuthBoundary, { children }) });
|
|
131
137
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
138
|
+
const CORE_URL = "https://core.sanity.io";
|
|
139
|
+
function SanityApp({ sanityConfigs, children }) {
|
|
140
|
+
const [_sanityConfigs, setSanityConfigs] = useState(sanityConfigs);
|
|
141
|
+
return useEffect(() => {
|
|
142
|
+
let timeout;
|
|
143
|
+
return isInIframe() ? setSanityConfigs(
|
|
144
|
+
sanityConfigs.map((sanityConfig) => ({
|
|
145
|
+
...sanityConfig,
|
|
146
|
+
auth: {
|
|
147
|
+
...sanityConfig.auth,
|
|
148
|
+
storageArea: void 0
|
|
149
|
+
}
|
|
150
|
+
}))
|
|
151
|
+
) : isLocalUrl(window) || (timeout = setTimeout(() => {
|
|
152
|
+
console.warn("Redirecting to core", CORE_URL), window.location.replace(CORE_URL);
|
|
153
|
+
}, 1e3)), () => clearTimeout(timeout);
|
|
154
|
+
}, [sanityConfigs]), /* @__PURE__ */ jsx(SDKProvider, { sanityConfigs: _sanityConfigs, children });
|
|
137
155
|
}
|
|
138
156
|
export {
|
|
139
157
|
AuthBoundary,
|
package/dist/components.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"components.js","sources":["../src/components/utils.ts","../src/components/auth/AuthError.ts","../src/components/auth/LoginFooter.tsx","../src/components/auth/LoginLayout.tsx","../src/components/auth/Login.tsx","../src/components/auth/LoginCallback.tsx","../src/components/auth/LoginError.tsx","../src/components/auth/AuthBoundary.tsx","../src/components/SDKProvider.tsx","../src/components/SanityApp.tsx"],"sourcesContent":["export function isInIframe(): boolean {\n return typeof window !== 'undefined' && window.self !== window.top\n}\n","/**\n * Error class for authentication-related errors. Wraps errors thrown during the\n * authentication flow.\n *\n * @remarks\n * This class provides a consistent error type for authentication failures while\n * preserving the original error as the cause. If the original error has a\n * message property, it will be used as the error message.\n *\n * @alpha\n */\nexport class AuthError extends Error {\n constructor(error: unknown) {\n if (\n typeof error === 'object' &&\n !!error &&\n 'message' in error &&\n typeof error.message === 'string'\n ) {\n super(error.message)\n } else {\n super()\n }\n\n this.cause = error\n }\n}\n","import {SanityLogo} from '@sanity/logos'\nimport {Fragment} from 'react'\n\nconst LINKS = [\n {\n url: 'https://slack.sanity.io/',\n i18nKey: 'workspaces.community-title',\n title: 'Community',\n },\n {\n url: 'https://www.sanity.io/docs',\n i18nKey: 'workspaces.docs-title',\n title: 'Docs',\n },\n {\n url: 'https://www.sanity.io/legal/privacy',\n i18nKey: 'workspaces.privacy-title',\n title: 'Privacy',\n },\n {\n url: 'https://www.sanity.io',\n i18nKey: 'workspaces.sanity-io-title',\n title: 'sanity.io',\n },\n]\n\n/**\n * Default footer component for login screens showing Sanity branding and legal\n * links.\n *\n * @alpha\n */\nexport function LoginFooter(): React.ReactNode {\n return (\n <div className=\"sc-login-footer\">\n <SanityLogo className=\"sc-login-footer__logo\" />\n\n <ul className=\"sc-login-footer__links\">\n {LINKS.map((link) => (\n <Fragment key={link.title}>\n <li className=\"sc-login-footer__link\">\n <a href={link.url} target=\"_blank\" rel=\"noopener noreferrer\">\n {link.title}\n </a>\n </li>\n </Fragment>\n ))}\n </ul>\n </div>\n )\n}\n","import {LoginFooter} from './LoginFooter'\n\n/**\n * @alpha\n */\nexport interface LoginLayoutProps {\n /** Optional header content rendered at top of card */\n header?: React.ReactNode\n\n /** Optional footer content rendered below card. Defaults to an internal login footer */\n footer?: React.ReactNode\n\n /** Main content rendered in card body */\n children?: React.ReactNode\n}\n\n/**\n * Layout component for login-related screens providing consistent styling and structure.\n * Renders content in a centered card with optional header and footer sections.\n *\n * Can be used to build custom login screens for the AuthBoundary component, including:\n * - Login provider selection (LoginComponent)\n * - OAuth callback handling (CallbackComponent)\n * - Error states (LoginErrorComponent)\n *\n * @example\n * ```tsx\n * // Custom login screen using the layout\n * function CustomLogin({header, footer}: LoginLayoutProps) {\n * return (\n * <LoginLayout\n * header={header}\n * footer={footer}\n * >\n * <CustomLoginContent />\n * </LoginLayout>\n * )\n * }\n *\n * // Use with AuthBoundary\n * <AuthBoundary\n * LoginComponent={CustomLogin}\n * header={<Logo />}\n * >\n * <ProtectedContent />\n * </AuthBoundary>\n * ```\n *\n * @alpha\n */\nexport function LoginLayout({\n children,\n footer = <LoginFooter />,\n header,\n}: LoginLayoutProps): React.ReactNode {\n return (\n <div className=\"sc-login-layout\">\n <div className=\"sc-login-layout__container\">\n <div className=\"sc-login-layout__card\">\n {header && <div className=\"sc-login-layout__card-header\">{header}</div>}\n\n {children && <div className=\"sc-login-layout__card-body\">{children}</div>}\n </div>\n\n {footer}\n </div>\n </div>\n )\n}\n","import {type JSX, Suspense} from 'react'\n\nimport {useLoginUrls} from '../../hooks/auth/useLoginUrls'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * Login component that displays available authentication providers.\n * Renders a list of login options with a loading fallback while providers load.\n *\n * @alpha\n * @internal\n */\nexport function Login({header, footer}: LoginLayoutProps): JSX.Element {\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login\">\n <h1 className=\"sc-login__title\">Choose login provider</h1>\n\n <Suspense fallback={<div className=\"sc-login__loading\">Loading…</div>}>\n <Providers />\n </Suspense>\n </div>\n </LoginLayout>\n )\n}\n\nfunction Providers() {\n const loginUrls = useLoginUrls()\n\n return (\n <div className=\"sc-login-providers\">\n {loginUrls.map(({title, url}) => (\n <a key={url} href={url}>\n {title}\n </a>\n ))}\n </div>\n )\n}\n","import {useEffect} from 'react'\n\nimport {useHandleCallback} from '../../hooks/auth/useHandleCallback'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n/**\n * Component shown during auth callback processing that handles login completion.\n * Automatically processes the auth callback when mounted and updates the URL\n * to remove callback parameters without triggering a page reload.\n *\n * @alpha\n */\nexport function LoginCallback({header, footer}: LoginLayoutProps): React.ReactNode {\n const handleCallback = useHandleCallback()\n\n useEffect(() => {\n const url = new URL(location.href)\n handleCallback(url.toString()).then((replacementLocation) => {\n if (replacementLocation) {\n // history API with `replaceState` is used to prevent a reload but still\n // remove the short-lived token from the URL\n history.replaceState(null, '', replacementLocation)\n }\n })\n }, [handleCallback])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login-callback\">\n <h1 className=\"sc-login-callback__title\">Logging you in…</h1>\n <div className=\"sc-login-callback__loading\">Loading…</div>\n </div>\n </LoginLayout>\n )\n}\n","import {useCallback} from 'react'\nimport {type FallbackProps} from 'react-error-boundary'\n\nimport {useLogOut} from '../../hooks/auth/useLogOut'\nimport {AuthError} from './AuthError'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * @alpha\n */\nexport type LoginErrorProps = FallbackProps & LoginLayoutProps\n\n/**\n * Displays authentication error details and provides retry functionality.\n * Only handles {@link AuthError} instances - rethrows other error types.\n *\n * @alpha\n */\nexport function LoginError({\n error,\n resetErrorBoundary,\n header,\n footer,\n}: LoginErrorProps): React.ReactNode {\n if (!(error instanceof AuthError)) throw error\n const logout = useLogOut()\n\n const handleRetry = useCallback(async () => {\n await logout()\n resetErrorBoundary()\n }, [logout, resetErrorBoundary])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login-error\">\n <div className=\"sc-login-error__content\">\n <h2 className=\"sc-login-error__title\">Authentication Error</h2>\n <p className=\"sc-login-error__description\">\n Please try again or contact support if the problem persists.\n </p>\n </div>\n\n <button className=\"sc-login-error__button\" onClick={handleRetry}>\n Retry\n </button>\n </div>\n </LoginLayout>\n )\n}\n","import {AuthStateType} from '@sanity/sdk'\nimport {useMemo} from 'react'\nimport {ErrorBoundary, type FallbackProps} from 'react-error-boundary'\n\nimport {useAuthState} from '../../hooks/auth/useAuthState'\nimport {isInIframe} from '../utils'\nimport {AuthError} from './AuthError'\nimport {Login} from './Login'\nimport {LoginCallback} from './LoginCallback'\nimport {LoginError, type LoginErrorProps} from './LoginError'\nimport {type LoginLayoutProps} from './LoginLayout'\n\n// Only import bridge if we're in an iframe. This assumes that the app is\n// running within SanityOS if it is in an iframe.\nif (isInIframe()) {\n const parsedUrl = new URL(window.location.href)\n const mode = new URLSearchParams(parsedUrl.hash.slice(1)).get('mode')\n const script = document.createElement('script')\n script.src =\n mode === 'core-ui--staging'\n ? 'https://core.sanity-cdn.work/bridge.js'\n : 'https://core.sanity-cdn.com/bridge.js'\n script.type = 'module'\n script.async = true\n document.head.appendChild(script)\n}\n\n/**\n * @internal\n */\ninterface AuthBoundaryProps extends LoginLayoutProps {\n /**\n * Custom component to render the login screen.\n * Receives all login layout props. Defaults to {@link Login}.\n */\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render during OAuth callback processing.\n * Receives all login layout props. Defaults to {@link LoginCallback}.\n */\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render when authentication errors occur.\n * Receives login layout props and error boundary props. Defaults to\n * {@link LoginError}\n */\n LoginErrorComponent?: React.ComponentType<LoginErrorProps>\n}\n\n/**\n * A component that handles authentication flow and error boundaries for a\n * protected section of the application.\n *\n * @remarks\n * This component manages different authentication states and renders the\n * appropriate components based on that state.\n *\n * @example\n * ```tsx\n * function App() {\n * return (\n * <AuthBoundary header={<MyLogo />}>\n * <ProtectedContent />\n * </AuthBoundary>\n * )\n * }\n * ```\n *\n * @internal\n */\nexport function AuthBoundary({\n LoginErrorComponent = LoginError,\n ...props\n}: AuthBoundaryProps): React.ReactNode {\n const {header, footer} = props\n const FallbackComponent = useMemo(() => {\n return function LoginComponentWithLayoutProps(fallbackProps: FallbackProps) {\n return <LoginErrorComponent {...fallbackProps} header={header} footer={footer} />\n }\n }, [header, footer, LoginErrorComponent])\n\n return (\n <ErrorBoundary FallbackComponent={FallbackComponent}>\n <AuthSwitch {...props} />\n </ErrorBoundary>\n )\n}\n\ninterface AuthSwitchProps extends LoginLayoutProps {\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n}\n\nfunction AuthSwitch({\n LoginComponent = Login,\n CallbackComponent = LoginCallback,\n children,\n ...props\n}: AuthSwitchProps) {\n const authState = useAuthState()\n\n switch (authState.type) {\n case AuthStateType.ERROR: {\n throw new AuthError(authState.error)\n }\n case AuthStateType.LOGGING_IN: {\n return <CallbackComponent {...props} />\n }\n case AuthStateType.LOGGED_IN: {\n return children\n }\n default: {\n return <LoginComponent {...props} />\n }\n }\n}\n","import {createSanityInstance, type SanityConfig} from '@sanity/sdk'\nimport {type ReactElement, type ReactNode} from 'react'\n\nimport {SanityProvider} from '../context/SanityProvider'\nimport {AuthBoundary} from './auth/AuthBoundary'\n\n/**\n * @internal\n */\nexport interface SDKProviderProps {\n children: ReactNode\n sanityConfig: SanityConfig\n}\n\n// Marking this as internal since this should not be used directly by consumers\n/**\n * @internal\n *\n * Top-level context provider that provides access to the Sanity SDK.\n */\nexport function SDKProvider({children, sanityConfig}: SDKProviderProps): ReactElement {\n const sanityInstance = createSanityInstance(sanityConfig)\n\n return (\n <SanityProvider sanityInstance={sanityInstance}>\n <AuthBoundary>{children}</AuthBoundary>\n </SanityProvider>\n )\n}\n","import {type SanityConfig} from '@sanity/sdk'\nimport {type ReactElement} from 'react'\n\nimport {SDKProvider} from './SDKProvider'\nimport {isInIframe} from './utils'\n\n/**\n * @public\n */\nexport interface SanityAppProps {\n sanityConfig: SanityConfig\n children: React.ReactNode\n}\n\n/**\n * @public\n *\n * The SanityApp component provides your Sanity application with access to your Sanity configuration,\n * as well as application context and state which is used by the Sanity React hooks. Your application\n * must be wrapped with the SanityApp component to function properly.\n *\n * @param props - Your Sanity configuration and the React children to render\n * @returns Your Sanity application, integrated with your Sanity configuration and application context\n *\n * @example\n * ```\n * import { SanityApp } from '@sanity/sdk-react\n *\n * import MyAppRoot from './Root'\n *\n * const mySanityConfig = {\n * procectId: 'my-project-id',\n * dataset: 'production',\n * }\n *\n * export default function MyApp() {\n * return (\n * <SanityApp sanityConfig={mySanityConfig}>\n * <MyAppRoot />\n * </SanityApp>\n * )\n * }\n * ```\n */\nexport function SanityApp({sanityConfig, children}: SanityAppProps): ReactElement {\n if (isInIframe()) {\n // When running in an iframe Content OS, we don't want to store tokens\n sanityConfig.auth = {\n ...sanityConfig.auth,\n storageArea: undefined,\n }\n }\n\n return <SDKProvider sanityConfig={sanityConfig}>{children}</SDKProvider>\n}\n"],"names":[],"mappings":";;;;;;;AAAO,SAAS,aAAsB;AACpC,SAAO,OAAO,SAAW,OAAe,OAAO,SAAS,OAAO;AACjE;ACSO,MAAM,kBAAkB,MAAM;AAAA,EACnC,YAAY,OAAgB;AAExB,WAAO,SAAU,YACf,SACF,aAAa,SACb,OAAO,MAAM,WAAY,WAEzB,MAAM,MAAM,OAAO,IAEnB,MAAM,GAGR,KAAK,QAAQ;AAAA,EAAA;AAEjB;ACvBA,MAAM,QAAQ;AAAA,EACZ;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EAAA;AAEX;AAQO,SAAS,cAA+B;AAE3C,SAAA,qBAAC,OAAI,EAAA,WAAU,mBACb,UAAA;AAAA,IAAC,oBAAA,YAAA,EAAW,WAAU,wBAAwB,CAAA;AAAA,IAE7C,oBAAA,MAAA,EAAG,WAAU,0BACX,UAAM,MAAA,IAAI,CAAC,SACT,oBAAA,UAAA,EACC,UAAC,oBAAA,MAAA,EAAG,WAAU,yBACZ,UAAA,oBAAC,KAAE,EAAA,MAAM,KAAK,KAAK,QAAO,UAAS,KAAI,uBACpC,UAAK,KAAA,OACR,EACF,CAAA,EAAA,GALa,KAAK,KAMpB,CACD,EACH,CAAA;AAAA,EAAA,GACF;AAEJ;ACAO,SAAS,YAAY;AAAA,EAC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,QAAA,EAAC,QAAQ,WAAU,OACnB,oBAAoB,QAAQ,MACzB,SAAuC,eAA8B;AAC1E,WAAQ,oBAAA,qBAAA,EAAqB,GAAG,eAAe,QAAgB,QAAgB;AAAA,EAEhF,GAAA,CAAC,QAAQ,QAAQ,mBAAmB,CAAC;AAExC,6BACG,eAAc,EAAA,mBACb,8BAAC,YAAY,EAAA,GAAG,MAAO,CAAA,GACzB;AAEJ;AAOA,SAAS,WAAW;AAAA,EAClB,iBAAiB;AAAA,EACjB,oBAAoB;AAAA,EACpB;AAAA,EACA,GAAG;AACL,GAAoB;AAClB,QAAM,YAAY,aAAa;AAE/B,UAAQ,UAAU,MAAM;AAAA,IACtB,KAAK,cAAc;AACX,YAAA,IAAI,UAAU,UAAU,KAAK;AAAA,IAErC,KAAK,cAAc;AACV,aAAA,oBAAC,mBAAmB,EAAA,GAAG,MAAO,CAAA;AAAA,IAEvC,KAAK,cAAc;AACV,aAAA;AAAA,IAET;AACS,aAAA,oBAAC,gBAAgB,EAAA,GAAG,MAAO,CAAA;AAAA,EAAA;AAGxC;ACjGO,SAAS,YAAY,EAAC,UAAU,gBAA+C;AAC9E,QAAA,iBAAiB,qBAAqB,YAAY;AAExD,6BACG,gBAAe,EAAA,gBACd,UAAC,oBAAA,cAAA,EAAc,SAAS,CAAA,GAC1B;AAEJ;ACgBO,SAAS,UAAU,EAAC,cAAc,YAAyC;AAC5E,SAAA,WAAA,MAEF,aAAa,OAAO;AAAA,IAClB,GAAG,aAAa;AAAA,IAChB,aAAa;AAAA,EAIV,IAAA,oBAAC,aAAY,EAAA,cAA6B,SAAS,CAAA;AAC5D;"}
|
|
1
|
+
{"version":3,"file":"components.js","sources":["../src/components/utils.ts","../src/components/auth/AuthError.ts","../src/components/auth/LoginFooter.tsx","../src/components/auth/LoginLayout.tsx","../src/components/auth/Login.tsx","../src/components/auth/LoginCallback.tsx","../src/components/auth/LoginError.tsx","../src/components/auth/AuthBoundary.tsx","../src/components/SDKProvider.tsx","../src/components/SanityApp.tsx"],"sourcesContent":["export function isInIframe(): boolean {\n return typeof window !== 'undefined' && window.self !== window.top\n}\n\n/**\n * @internal\n *\n * Checks if the current URL is a local URL.\n *\n * @param window - The window object\n * @returns True if the current URL is a local URL, false otherwise\n */\nexport function isLocalUrl(window: Window): boolean {\n const url = typeof window !== 'undefined' ? window.location.href : ''\n\n return (\n url.startsWith('http://localhost') ||\n url.startsWith('https://localhost') ||\n url.startsWith('http://127.0.0.1') ||\n url.startsWith('https://127.0.0.1')\n )\n}\n","/**\n * Error class for authentication-related errors. Wraps errors thrown during the\n * authentication flow.\n *\n * @remarks\n * This class provides a consistent error type for authentication failures while\n * preserving the original error as the cause. If the original error has a\n * message property, it will be used as the error message.\n *\n * @alpha\n */\nexport class AuthError extends Error {\n constructor(error: unknown) {\n if (\n typeof error === 'object' &&\n !!error &&\n 'message' in error &&\n typeof error.message === 'string'\n ) {\n super(error.message)\n } else {\n super()\n }\n\n this.cause = error\n }\n}\n","import {SanityLogo} from '@sanity/logos'\nimport {Fragment} from 'react'\n\nconst LINKS = [\n {\n url: 'https://slack.sanity.io/',\n i18nKey: 'workspaces.community-title',\n title: 'Community',\n },\n {\n url: 'https://www.sanity.io/docs',\n i18nKey: 'workspaces.docs-title',\n title: 'Docs',\n },\n {\n url: 'https://www.sanity.io/legal/privacy',\n i18nKey: 'workspaces.privacy-title',\n title: 'Privacy',\n },\n {\n url: 'https://www.sanity.io',\n i18nKey: 'workspaces.sanity-io-title',\n title: 'sanity.io',\n },\n]\n\n/**\n * Default footer component for login screens showing Sanity branding and legal\n * links.\n *\n * @alpha\n */\nexport function LoginFooter(): React.ReactNode {\n return (\n <div className=\"sc-login-footer\">\n <SanityLogo className=\"sc-login-footer__logo\" />\n\n <ul className=\"sc-login-footer__links\">\n {LINKS.map((link) => (\n <Fragment key={link.title}>\n <li className=\"sc-login-footer__link\">\n <a href={link.url} target=\"_blank\" rel=\"noopener noreferrer\">\n {link.title}\n </a>\n </li>\n </Fragment>\n ))}\n </ul>\n </div>\n )\n}\n","import {LoginFooter} from './LoginFooter'\n\n/**\n * @alpha\n */\nexport interface LoginLayoutProps {\n /** Optional header content rendered at top of card */\n header?: React.ReactNode\n\n /** Optional footer content rendered below card. Defaults to an internal login footer */\n footer?: React.ReactNode\n\n /** Main content rendered in card body */\n children?: React.ReactNode\n}\n\n/**\n * Layout component for login-related screens providing consistent styling and structure.\n * Renders content in a centered card with optional header and footer sections.\n *\n * Can be used to build custom login screens for the AuthBoundary component, including:\n * - Login provider selection (LoginComponent)\n * - OAuth callback handling (CallbackComponent)\n * - Error states (LoginErrorComponent)\n *\n * @example\n * ```tsx\n * // Custom login screen using the layout\n * function CustomLogin({header, footer}: LoginLayoutProps) {\n * return (\n * <LoginLayout\n * header={header}\n * footer={footer}\n * >\n * <CustomLoginContent />\n * </LoginLayout>\n * )\n * }\n *\n * // Use with AuthBoundary\n * <AuthBoundary\n * LoginComponent={CustomLogin}\n * header={<Logo />}\n * >\n * <ProtectedContent />\n * </AuthBoundary>\n * ```\n *\n * @alpha\n */\nexport function LoginLayout({\n children,\n footer = <LoginFooter />,\n header,\n}: LoginLayoutProps): React.ReactNode {\n return (\n <div className=\"sc-login-layout\">\n <div className=\"sc-login-layout__container\">\n <div className=\"sc-login-layout__card\">\n {header && <div className=\"sc-login-layout__card-header\">{header}</div>}\n\n {children && <div className=\"sc-login-layout__card-body\">{children}</div>}\n </div>\n\n {footer}\n </div>\n </div>\n )\n}\n","import {type JSX, Suspense} from 'react'\n\nimport {useLoginUrls} from '../../hooks/auth/useLoginUrls'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * Login component that displays available authentication providers.\n * Renders a list of login options with a loading fallback while providers load.\n *\n * @alpha\n * @internal\n */\nexport function Login({header, footer}: LoginLayoutProps): JSX.Element {\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login\">\n <h1 className=\"sc-login__title\">Choose login provider</h1>\n\n <Suspense fallback={<div className=\"sc-login__loading\">Loading…</div>}>\n <Providers />\n </Suspense>\n </div>\n </LoginLayout>\n )\n}\n\nfunction Providers() {\n const loginUrls = useLoginUrls()\n\n return (\n <div className=\"sc-login-providers\">\n {loginUrls.map(({title, url}) => (\n <a key={url} href={url}>\n {title}\n </a>\n ))}\n </div>\n )\n}\n","import {useEffect} from 'react'\n\nimport {useHandleCallback} from '../../hooks/auth/useHandleCallback'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n/**\n * Component shown during auth callback processing that handles login completion.\n * Automatically processes the auth callback when mounted and updates the URL\n * to remove callback parameters without triggering a page reload.\n *\n * @alpha\n */\nexport function LoginCallback({header, footer}: LoginLayoutProps): React.ReactNode {\n const handleCallback = useHandleCallback()\n\n useEffect(() => {\n const url = new URL(location.href)\n handleCallback(url.toString()).then((replacementLocation) => {\n if (replacementLocation) {\n // history API with `replaceState` is used to prevent a reload but still\n // remove the short-lived token from the URL\n history.replaceState(null, '', replacementLocation)\n }\n })\n }, [handleCallback])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login-callback\">\n <h1 className=\"sc-login-callback__title\">Logging you in…</h1>\n <div className=\"sc-login-callback__loading\">Loading…</div>\n </div>\n </LoginLayout>\n )\n}\n","import {useCallback} from 'react'\nimport {type FallbackProps} from 'react-error-boundary'\n\nimport {useLogOut} from '../../hooks/auth/useLogOut'\nimport {AuthError} from './AuthError'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * @alpha\n */\nexport type LoginErrorProps = FallbackProps & LoginLayoutProps\n\n/**\n * Displays authentication error details and provides retry functionality.\n * Only handles {@link AuthError} instances - rethrows other error types.\n *\n * @alpha\n */\nexport function LoginError({\n error,\n resetErrorBoundary,\n header,\n footer,\n}: LoginErrorProps): React.ReactNode {\n if (!(error instanceof AuthError)) throw error\n const logout = useLogOut()\n\n const handleRetry = useCallback(async () => {\n await logout()\n resetErrorBoundary()\n }, [logout, resetErrorBoundary])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login-error\">\n <div className=\"sc-login-error__content\">\n <h2 className=\"sc-login-error__title\">Authentication Error</h2>\n <p className=\"sc-login-error__description\">\n Please try again or contact support if the problem persists.\n </p>\n </div>\n\n <button className=\"sc-login-error__button\" onClick={handleRetry}>\n Retry\n </button>\n </div>\n </LoginLayout>\n )\n}\n","import {AuthStateType} from '@sanity/sdk'\nimport {useMemo} from 'react'\nimport {ErrorBoundary, type FallbackProps} from 'react-error-boundary'\n\nimport {useAuthState} from '../../hooks/auth/useAuthState'\nimport {isInIframe} from '../utils'\nimport {AuthError} from './AuthError'\nimport {Login} from './Login'\nimport {LoginCallback} from './LoginCallback'\nimport {LoginError, type LoginErrorProps} from './LoginError'\nimport {type LoginLayoutProps} from './LoginLayout'\n\n// Only import bridge if we're in an iframe. This assumes that the app is\n// running within SanityOS if it is in an iframe.\nif (isInIframe()) {\n const parsedUrl = new URL(window.location.href)\n const mode = new URLSearchParams(parsedUrl.hash.slice(1)).get('mode')\n const script = document.createElement('script')\n script.src =\n mode === 'core-ui--staging'\n ? 'https://core.sanity-cdn.work/bridge.js'\n : 'https://core.sanity-cdn.com/bridge.js'\n script.type = 'module'\n script.async = true\n document.head.appendChild(script)\n}\n\n/**\n * @internal\n */\ninterface AuthBoundaryProps extends LoginLayoutProps {\n /**\n * Custom component to render the login screen.\n * Receives all login layout props. Defaults to {@link Login}.\n */\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render during OAuth callback processing.\n * Receives all login layout props. Defaults to {@link LoginCallback}.\n */\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render when authentication errors occur.\n * Receives login layout props and error boundary props. Defaults to\n * {@link LoginError}\n */\n LoginErrorComponent?: React.ComponentType<LoginErrorProps>\n}\n\n/**\n * A component that handles authentication flow and error boundaries for a\n * protected section of the application.\n *\n * @remarks\n * This component manages different authentication states and renders the\n * appropriate components based on that state.\n *\n * @example\n * ```tsx\n * function App() {\n * return (\n * <AuthBoundary header={<MyLogo />}>\n * <ProtectedContent />\n * </AuthBoundary>\n * )\n * }\n * ```\n *\n * @internal\n */\nexport function AuthBoundary({\n LoginErrorComponent = LoginError,\n ...props\n}: AuthBoundaryProps): React.ReactNode {\n const {header, footer} = props\n const FallbackComponent = useMemo(() => {\n return function LoginComponentWithLayoutProps(fallbackProps: FallbackProps) {\n return <LoginErrorComponent {...fallbackProps} header={header} footer={footer} />\n }\n }, [header, footer, LoginErrorComponent])\n\n return (\n <ErrorBoundary FallbackComponent={FallbackComponent}>\n <AuthSwitch {...props} />\n </ErrorBoundary>\n )\n}\n\ninterface AuthSwitchProps extends LoginLayoutProps {\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n}\n\nfunction AuthSwitch({\n LoginComponent = Login,\n CallbackComponent = LoginCallback,\n children,\n ...props\n}: AuthSwitchProps) {\n const authState = useAuthState()\n\n switch (authState.type) {\n case AuthStateType.ERROR: {\n throw new AuthError(authState.error)\n }\n case AuthStateType.LOGGING_IN: {\n return <CallbackComponent {...props} />\n }\n case AuthStateType.LOGGED_IN: {\n return children\n }\n default: {\n return <LoginComponent {...props} />\n }\n }\n}\n","import {createSanityInstance, type SanityConfig} from '@sanity/sdk'\nimport {type ReactElement, type ReactNode} from 'react'\n\nimport {SanityProvider} from '../context/SanityProvider'\nimport {AuthBoundary} from './auth/AuthBoundary'\n\n/**\n * @internal\n */\nexport interface SDKProviderProps {\n children: ReactNode\n sanityConfigs: SanityConfig[]\n}\n\n// Marking this as internal since this should not be used directly by consumers\n/**\n * @internal\n *\n * Top-level context provider that provides access to the Sanity SDK.\n */\nexport function SDKProvider({children, sanityConfigs}: SDKProviderProps): ReactElement {\n const sanityInstances = sanityConfigs.map((sanityConfig: SanityConfig) =>\n createSanityInstance(sanityConfig),\n )\n\n return (\n <SanityProvider sanityInstances={sanityInstances}>\n <AuthBoundary>{children}</AuthBoundary>\n </SanityProvider>\n )\n}\n","import {type SanityConfig} from '@sanity/sdk'\nimport {type ReactElement, useEffect, useState} from 'react'\n\nimport {SDKProvider} from './SDKProvider'\nimport {isInIframe, isLocalUrl} from './utils'\n\n/**\n * @public\n */\nexport interface SanityAppProps {\n sanityConfigs: SanityConfig[]\n children: React.ReactNode\n}\n\nconst CORE_URL = 'https://core.sanity.io'\n\n/**\n * @public\n *\n * The SanityApp component provides your Sanity application with access to your Sanity configuration,\n * as well as application context and state which is used by the Sanity React hooks. Your application\n * must be wrapped with the SanityApp component to function properly.\n *\n * @param props - Your Sanity configuration and the React children to render\n * @returns Your Sanity application, integrated with your Sanity configuration and application context\n *\n * @example\n * ```\n * import { SanityApp } from '@sanity/sdk-react\n *\n * import MyAppRoot from './Root'\n *\n * const mySanityConfig = {\n * procectId: 'my-project-id',\n * dataset: 'production',\n * }\n *\n * export default function MyApp() {\n * return (\n * <SanityApp sanityConfig={mySanityConfig}>\n * <MyAppRoot />\n * </SanityApp>\n * )\n * }\n * ```\n */\nexport function SanityApp({sanityConfigs, children}: SanityAppProps): ReactElement {\n const [_sanityConfigs, setSanityConfigs] = useState<SanityConfig[]>(sanityConfigs)\n\n useEffect(() => {\n let timeout: NodeJS.Timeout | undefined\n\n if (isInIframe()) {\n // When running in an iframe Content OS, we don't want to store tokens\n setSanityConfigs(\n sanityConfigs.map((sanityConfig) => ({\n ...sanityConfig,\n auth: {\n ...sanityConfig.auth,\n storageArea: undefined,\n },\n })),\n )\n } else if (!isLocalUrl(window)) {\n // If the app is not running in an iframe and is not a local url, redirect to core.\n timeout = setTimeout(() => {\n // eslint-disable-next-line no-console\n console.warn('Redirecting to core', CORE_URL)\n window.location.replace(CORE_URL)\n }, 1000)\n }\n return () => clearTimeout(timeout)\n }, [sanityConfigs])\n\n return <SDKProvider sanityConfigs={_sanityConfigs}>{children}</SDKProvider>\n}\n"],"names":["window"],"mappings":";;;;;;;AAAO,SAAS,aAAsB;AACpC,SAAO,OAAO,SAAW,OAAe,OAAO,SAA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,IAC/B,GAAA,GAAI,IAEF,MAAM,aAAa,OAAO;AAAA,EAAA,GAChC,CAAC,aAAa,CAAC,GAEV,oBAAA,aAAA,EAAY,eAAe,gBAAiB,UAAS;AAC/D;"}
|
package/dist/context.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ import {SanityInstance} from '@sanity/sdk'
|
|
|
33
33
|
*/
|
|
34
34
|
export declare const SanityProvider: ({
|
|
35
35
|
children,
|
|
36
|
-
|
|
36
|
+
sanityInstances,
|
|
37
37
|
}: SanityProviderProps) => ReactElement
|
|
38
38
|
|
|
39
39
|
/**
|
|
@@ -41,7 +41,7 @@ export declare const SanityProvider: ({
|
|
|
41
41
|
*/
|
|
42
42
|
export declare interface SanityProviderProps {
|
|
43
43
|
children: React.ReactNode
|
|
44
|
-
|
|
44
|
+
sanityInstances: SanityInstance[]
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
export {}
|
package/dist/hooks.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {ResourceType} from '@sanity/sdk'
|
|
|
19
19
|
import {SanityDocument} from '@sanity/types'
|
|
20
20
|
import {SanityInstance} from '@sanity/sdk'
|
|
21
21
|
import {SanityUser as SanityUser_2} from '@sanity/sdk'
|
|
22
|
+
import {ValidProjection} from '@sanity/sdk'
|
|
22
23
|
import {WindowMessage} from '@sanity/sdk'
|
|
23
24
|
|
|
24
25
|
/** @public */
|
|
@@ -3315,7 +3316,7 @@ export declare const useDatasets: () => DatasetsResponse
|
|
|
3315
3316
|
export declare function useDocument<
|
|
3316
3317
|
TDocument extends SanityDocument,
|
|
3317
3318
|
TPath extends JsonMatchPath<TDocument>,
|
|
3318
|
-
>(doc:
|
|
3319
|
+
>(doc: DocumentHandle<TDocument>, path: TPath): JsonMatch<TDocument, TPath> | undefined
|
|
3319
3320
|
|
|
3320
3321
|
/**
|
|
3321
3322
|
* @beta
|
|
@@ -3353,7 +3354,7 @@ export declare function useDocument<
|
|
|
3353
3354
|
*
|
|
3354
3355
|
*/
|
|
3355
3356
|
export declare function useDocument<TDocument extends SanityDocument>(
|
|
3356
|
-
doc:
|
|
3357
|
+
doc: DocumentHandle<TDocument>,
|
|
3357
3358
|
): TDocument | null
|
|
3358
3359
|
|
|
3359
3360
|
/**
|
|
@@ -3379,7 +3380,10 @@ export declare function useDocument<TDocument extends SanityDocument>(
|
|
|
3379
3380
|
* })
|
|
3380
3381
|
* ```
|
|
3381
3382
|
*/
|
|
3382
|
-
export declare function useDocumentEvent(
|
|
3383
|
+
export declare function useDocumentEvent(
|
|
3384
|
+
handler: (documentEvent: DocumentEvent) => void,
|
|
3385
|
+
doc: DocumentHandle,
|
|
3386
|
+
): void
|
|
3383
3387
|
|
|
3384
3388
|
/**
|
|
3385
3389
|
* @public
|
|
@@ -3449,7 +3453,7 @@ declare type UseDocumentSyncStatus = {
|
|
|
3449
3453
|
* @returns `true` if local changes are synced with remote, `false` if the changes are not synced, and `undefined` if the document is not found
|
|
3450
3454
|
* @example Disable a Save button when there are no changes to sync
|
|
3451
3455
|
* ```
|
|
3452
|
-
* const myDocumentHandle = { _id: 'documentId', _type: 'documentType' }
|
|
3456
|
+
* const myDocumentHandle = { _id: 'documentId', _type: 'documentType', resourceId: 'document:projectId:dataset:documentId' }
|
|
3453
3457
|
* const documentSynced = useDocumentSyncStatus(myDocumentHandle)
|
|
3454
3458
|
*
|
|
3455
3459
|
* return (
|
|
@@ -3515,7 +3519,7 @@ export declare function useEditDocument<
|
|
|
3515
3519
|
TDocument extends SanityDocument,
|
|
3516
3520
|
TPath extends JsonMatchPath<TDocument>,
|
|
3517
3521
|
>(
|
|
3518
|
-
doc:
|
|
3522
|
+
doc: DocumentHandle<TDocument>,
|
|
3519
3523
|
path: TPath,
|
|
3520
3524
|
): (nextValue: Updater<JsonMatch<TDocument, TPath>>) => Promise<ActionsResult<TDocument>>
|
|
3521
3525
|
|
|
@@ -3582,7 +3586,7 @@ export declare function useEditDocument<
|
|
|
3582
3586
|
* ```
|
|
3583
3587
|
*/
|
|
3584
3588
|
export declare function useEditDocument<TDocument extends SanityDocument>(
|
|
3585
|
-
doc:
|
|
3589
|
+
doc: DocumentHandle<TDocument>,
|
|
3586
3590
|
): (nextValue: Updater<TDocument>) => Promise<ActionsResult<TDocument>>
|
|
3587
3591
|
|
|
3588
3592
|
/**
|
|
@@ -3819,6 +3823,78 @@ export declare interface UsePreviewResults {
|
|
|
3819
3823
|
/** @public */
|
|
3820
3824
|
export declare const useProject: (projectId: string) => SanityProject
|
|
3821
3825
|
|
|
3826
|
+
/**
|
|
3827
|
+
* @beta
|
|
3828
|
+
*
|
|
3829
|
+
* Returns the projection values of a document (specified via a `DocumentHandle`),
|
|
3830
|
+
* based on the provided projection string. These values are live and will update in realtime.
|
|
3831
|
+
* To reduce unnecessary network requests for resolving the projection values, an optional `ref` can be passed to the hook so that projection
|
|
3832
|
+
* resolution will only occur if the `ref` is intersecting the current viewport.
|
|
3833
|
+
*
|
|
3834
|
+
* @category Documents
|
|
3835
|
+
* @param options - The document handle for the document you want to project values from, the projection string, and an optional ref
|
|
3836
|
+
* @returns The projection values for the given document and a boolean to indicate whether the resolution is pending
|
|
3837
|
+
*
|
|
3838
|
+
* @example Using a projection to display specific document fields
|
|
3839
|
+
* ```
|
|
3840
|
+
* // ProjectionComponent.jsx
|
|
3841
|
+
* export default function ProjectionComponent({ document }) {
|
|
3842
|
+
* const ref = useRef(null)
|
|
3843
|
+
* const { results: { title, description, authors }, isPending } = useProjection({
|
|
3844
|
+
* document,
|
|
3845
|
+
* projection: '{title, "description": pt::text("description"), "authors": array::join(authors[]->name, ", ")}',
|
|
3846
|
+
* ref
|
|
3847
|
+
* })
|
|
3848
|
+
*
|
|
3849
|
+
* return (
|
|
3850
|
+
* <article ref={ref} style={{ opacity: isPending ? 0.5 : 1}}>
|
|
3851
|
+
* <h2>{title}</h2>
|
|
3852
|
+
* <p>{description}</p>
|
|
3853
|
+
* <p>{authors}</p>
|
|
3854
|
+
* </article>
|
|
3855
|
+
* )
|
|
3856
|
+
* }
|
|
3857
|
+
* ```
|
|
3858
|
+
*
|
|
3859
|
+
* @example Combining with useDocuments to render a collection with specific fields
|
|
3860
|
+
* ```
|
|
3861
|
+
* // DocumentList.jsx
|
|
3862
|
+
* const { results, isPending } = useDocuments({ filter: '_type == "article"' })
|
|
3863
|
+
* return (
|
|
3864
|
+
* <div>
|
|
3865
|
+
* <h1>Articles</h1>
|
|
3866
|
+
* <ul>
|
|
3867
|
+
* {isPending ? 'Loading…' : results.map(article => (
|
|
3868
|
+
* <li key={article._id}>
|
|
3869
|
+
* <Suspense fallback='Loading…'>
|
|
3870
|
+
* <ProjectionComponent
|
|
3871
|
+
* document={article}
|
|
3872
|
+
* />
|
|
3873
|
+
* </Suspense>
|
|
3874
|
+
* </li>
|
|
3875
|
+
* ))}
|
|
3876
|
+
* </ul>
|
|
3877
|
+
* </div>
|
|
3878
|
+
* )
|
|
3879
|
+
* ```
|
|
3880
|
+
*/
|
|
3881
|
+
export declare function useProjection<TResult extends object>({
|
|
3882
|
+
document: {_id, _type},
|
|
3883
|
+
projection,
|
|
3884
|
+
ref,
|
|
3885
|
+
}: UseProjectionOptions): UseProjectionResults<TResult>
|
|
3886
|
+
|
|
3887
|
+
declare interface UseProjectionOptions {
|
|
3888
|
+
document: DocumentHandle
|
|
3889
|
+
projection: ValidProjection
|
|
3890
|
+
ref?: React.RefObject<unknown>
|
|
3891
|
+
}
|
|
3892
|
+
|
|
3893
|
+
declare interface UseProjectionResults<TResult extends object> {
|
|
3894
|
+
results: TResult
|
|
3895
|
+
isPending: boolean
|
|
3896
|
+
}
|
|
3897
|
+
|
|
3822
3898
|
/** @public */
|
|
3823
3899
|
export declare const useProjects: () => Omit<SanityProject, 'members'>[]
|
|
3824
3900
|
|
|
@@ -3838,13 +3914,15 @@ declare class UsersClient {
|
|
|
3838
3914
|
* `useSanityInstance` returns the current Sanity instance from the application context.
|
|
3839
3915
|
* This must be called from within a `SanityProvider` component.
|
|
3840
3916
|
* @internal
|
|
3917
|
+
*
|
|
3918
|
+
* @param resourceId - The resourceId of the Sanity instance to return (optional)
|
|
3841
3919
|
* @returns The current Sanity instance
|
|
3842
3920
|
* @example
|
|
3843
3921
|
* ```tsx
|
|
3844
|
-
* const instance = useSanityInstance()
|
|
3922
|
+
* const instance = useSanityInstance('abc123:production')
|
|
3845
3923
|
* ```
|
|
3846
3924
|
*/
|
|
3847
|
-
export declare const useSanityInstance: () => SanityInstance
|
|
3925
|
+
export declare const useSanityInstance: (resourceId?: string) => SanityInstance
|
|
3848
3926
|
|
|
3849
3927
|
/**
|
|
3850
3928
|
* @public
|