@sanity/sdk-react 0.0.0-alpha.5 → 0.0.0-alpha.7
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 +3 -2
- package/dist/_chunks-es/context.js.map +1 -1
- package/dist/_chunks-es/useLogOut.js.map +1 -1
- package/dist/components.d.ts +37 -10
- package/dist/components.js +2 -4
- package/dist/components.js.map +1 -1
- package/dist/context.d.ts +12 -6
- package/dist/hooks.d.ts +48 -23
- package/dist/hooks.js +3 -2
- package/dist/hooks.js.map +1 -1
- package/package.json +11 -10
- package/src/_exports/components.ts +1 -1
- package/src/_exports/hooks.ts +1 -0
- package/src/components/SanityApp.tsx +36 -9
- package/src/components/auth/AuthBoundary.tsx +8 -2
- package/src/context/SanityProvider.tsx +12 -6
- package/src/hooks/auth/useAuthState.tsx +4 -3
- package/src/hooks/auth/useAuthToken.tsx +1 -1
- package/src/hooks/auth/useCurrentUser.tsx +21 -4
- package/src/hooks/auth/useHandleCallback.tsx +1 -0
- package/src/hooks/auth/useLogOut.tsx +1 -1
- package/src/hooks/auth/useLoginUrls.tsx +1 -0
- package/src/hooks/comlink/useFrameConnection.ts +4 -4
- package/src/hooks/comlink/useWindowConnection.ts +4 -4
- package/src/hooks/context/useSanityInstance.ts +2 -2
- package/src/hooks/documentCollection/useDocuments.ts +3 -2
- package/src/hooks/preview/usePreview.test.tsx +3 -3
- package/src/hooks/preview/usePreview.tsx +13 -7
package/README.md
CHANGED
|
@@ -44,9 +44,10 @@ import './App.css'
|
|
|
44
44
|
const sanityConfig: SanityConfig = {
|
|
45
45
|
projectId: '<your-project-id>',
|
|
46
46
|
dataset: '<your-dataset>',
|
|
47
|
-
// optional auth config set projectId and dataset to '' and authScope to '
|
|
47
|
+
// optional auth config set projectId and dataset to '' and authScope to 'global' for a global token
|
|
48
48
|
// auth: {
|
|
49
|
-
// authScope:
|
|
49
|
+
// authScope: 'global',
|
|
50
|
+
// ...
|
|
50
51
|
// },
|
|
51
52
|
}
|
|
52
53
|
|
|
@@ -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 * @public\n */\nexport interface SanityProviderProps {\n children: React.ReactNode\n sanityInstance: SanityInstance\n}\n\nexport const SanityInstanceContext = createContext<SanityInstance | null>(null)\n\n/**\n * Top-level context provider that provides
|
|
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 * @public\n */\nexport interface SanityProviderProps {\n children: React.ReactNode\n sanityInstance: SanityInstance\n}\n\nexport const SanityInstanceContext = createContext<SanityInstance | null>(null)\n\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 * @remarks In most cases, SanityApp should be used rather than SanityProvider directly; SanityApp bundles both SanityProvider and an authentication layer.\n * @internal\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, sanityInstance}: SanityProviderProps): ReactElement => {\n return (\n <SanityInstanceContext.Provider value={sanityInstance}>\n {children}\n </SanityInstanceContext.Provider>\n )\n}\n"],"names":[],"mappings":";;AAWO,MAAM,wBAAwB,cAAqC,IAAI,GA8BjE,iBAAiB,CAAC,EAAC,UAAU,eAAc,0BAEnD,sBAAsB,UAAtB,EAA+B,OAAO,gBACpC,SACH,CAAA;"}
|
|
@@ -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 *
|
|
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 * @public\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\nexport function createStateSourceHook<TParams extends unknown[], TState>(\n stateSourceFactory: (instance: SanityInstance, ...params: TParams) => StateSource<TState>,\n): (...params: TParams) => TState {\n function useHook(...params: TParams) {\n const instance = useSanityInstance()\n const {subscribe, getCurrent} = useMemo(\n () => stateSourceFactory(instance, ...params),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [instance, ...params],\n )\n\n return useSyncExternalStore(subscribe, 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 *\n * @public\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":";;;AAeO,MAAM,oBAAoB,MAAsB;AAC/C,QAAA,iBAAiB,WAAW,qBAAqB;AACvD,MAAI,CAAC;AACG,UAAA,IAAI,MAAM,iEAAiE;AAG5E,SAAA;AACT;ACjBO,SAAS,sBACd,oBACgC;AAChC,WAAS,WAAW,QAAiB;AACnC,UAAM,WAAW,kBAAkB,GAC7B,EAAC,WAAW,WAAc,IAAA;AAAA,MAC9B,MAAM,mBAAmB,UAAU,GAAG,MAAM;AAAA;AAAA,MAE5C,CAAC,UAAU,GAAG,MAAM;AAAA,IACtB;AAEO,WAAA,qBAAqB,WAAW,UAAU;AAAA,EAAA;AAG5C,SAAA;AACT;ACOa,MAAA,eAAgC,sBAAsB,YAAY;ACtBxE,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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {FallbackProps} from 'react-error-boundary'
|
|
2
|
-
import {
|
|
2
|
+
import {ReactElement} from 'react'
|
|
3
3
|
import {SanityConfig} from '@sanity/sdk'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -21,7 +21,7 @@ import {SanityConfig} from '@sanity/sdk'
|
|
|
21
21
|
* }
|
|
22
22
|
* ```
|
|
23
23
|
*
|
|
24
|
-
* @
|
|
24
|
+
* @internal
|
|
25
25
|
*/
|
|
26
26
|
export declare function AuthBoundary({
|
|
27
27
|
LoginErrorComponent,
|
|
@@ -29,7 +29,7 @@ export declare function AuthBoundary({
|
|
|
29
29
|
}: AuthBoundaryProps): React.ReactNode
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
* @
|
|
32
|
+
* @internal
|
|
33
33
|
*/
|
|
34
34
|
declare interface AuthBoundaryProps extends LoginLayoutProps {
|
|
35
35
|
/**
|
|
@@ -71,14 +71,41 @@ declare interface LoginLayoutProps {
|
|
|
71
71
|
/**
|
|
72
72
|
* @public
|
|
73
73
|
*
|
|
74
|
-
*
|
|
74
|
+
* The SanityApp component provides your Sanity application with access to your Sanity configuration,
|
|
75
|
+
* as well as application context and state which is used by the Sanity React hooks. Your application
|
|
76
|
+
* must be wrapped with the SanityApp component to function properly.
|
|
77
|
+
*
|
|
78
|
+
* @param props - Your Sanity configuration and the React children to render
|
|
79
|
+
* @returns Your Sanity application, integrated with your Sanity configuration and application context
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```
|
|
83
|
+
* import { SanityApp } from '@sanity/sdk-react
|
|
84
|
+
*
|
|
85
|
+
* import MyAppRoot from './Root'
|
|
86
|
+
*
|
|
87
|
+
* const mySanityConfig = {
|
|
88
|
+
* procectId: 'my-project-id',
|
|
89
|
+
* dataset: 'production',
|
|
90
|
+
* }
|
|
91
|
+
*
|
|
92
|
+
* export default function MyApp() {
|
|
93
|
+
* return (
|
|
94
|
+
* <SanityApp sanityConfig={mySanityConfig}>
|
|
95
|
+
* <MyAppRoot />
|
|
96
|
+
* </SanityApp>
|
|
97
|
+
* )
|
|
98
|
+
* }
|
|
99
|
+
* ```
|
|
75
100
|
*/
|
|
76
|
-
export declare function SanityApp({
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
101
|
+
export declare function SanityApp({sanityConfig, children}: SanityAppProps): ReactElement
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @public
|
|
105
|
+
*/
|
|
106
|
+
export declare interface SanityAppProps {
|
|
81
107
|
sanityConfig: SanityConfig
|
|
82
|
-
|
|
108
|
+
children: React.ReactNode
|
|
109
|
+
}
|
|
83
110
|
|
|
84
111
|
export {}
|
package/dist/components.js
CHANGED
|
@@ -114,6 +114,7 @@ function LoginError({
|
|
|
114
114
|
/* @__PURE__ */ jsx(Button, { mode: "ghost", onClick: handleRetry, text: "Retry", fontSize: 2 })
|
|
115
115
|
] }) });
|
|
116
116
|
}
|
|
117
|
+
typeof window < "u" && window.self !== window.top && import("@sanity/os/bridge");
|
|
117
118
|
function AuthBoundary({
|
|
118
119
|
LoginErrorComponent = LoginError,
|
|
119
120
|
...props
|
|
@@ -141,10 +142,7 @@ function AuthSwitch({
|
|
|
141
142
|
return /* @__PURE__ */ jsx(LoginComponent, { ...props });
|
|
142
143
|
}
|
|
143
144
|
}
|
|
144
|
-
function SanityApp({
|
|
145
|
-
sanityConfig,
|
|
146
|
-
children
|
|
147
|
-
}) {
|
|
145
|
+
function SanityApp({ sanityConfig, children }) {
|
|
148
146
|
const sanityInstance = createSanityInstance(sanityConfig);
|
|
149
147
|
return /* @__PURE__ */ jsx(SanityProvider, { sanityInstance, children: /* @__PURE__ */ jsx(AuthBoundary, { children }) });
|
|
150
148
|
}
|
package/dist/components.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"components.js","sources":["../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/SanityApp.tsx"],"sourcesContent":["/**\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 {Box, Flex, Inline, Text} from '@sanity/ui'\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 <Box>\n <Flex justify=\"center\">\n <SanityLogo />\n </Flex>\n\n <Flex justify=\"center\">\n <Inline space={2} paddingY={3}>\n {LINKS.map((link) => (\n <Text size={0} key={link.url}>\n <a\n href={link.url}\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n style={{color: 'inherit'}}\n >\n {link.title}\n </a>\n </Text>\n ))}\n </Inline>\n </Flex>\n </Box>\n )\n}\n","import {Card, Container} from '@sanity/ui'\n\nimport {LoginFooter} from './LoginFooter'\n\n/**\n * @alpha\n * @internal\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 <Container width={0}>\n <Card shadow={1} radius={2} padding={4}>\n {header && header}\n\n {children && children}\n\n {footer}\n </Card>\n </Container>\n )\n}\n","import {Box, Button, Flex, Heading, Spinner, Stack} from '@sanity/ui'\nimport {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 <Heading as=\"h6\" align=\"center\">\n Choose login provider:\n </Heading>\n\n <Suspense\n fallback={\n <Box padding={5}>\n <Flex align=\"center\" justify=\"center\">\n <Spinner />\n </Flex>\n </Box>\n }\n >\n <Providers />\n </Suspense>\n </LoginLayout>\n )\n}\n\nfunction Providers() {\n const loginUrls = useLoginUrls()\n\n return (\n <Stack space={3} marginY={5}>\n {loginUrls.map(({title, url}) => (\n <Button\n key={url}\n as=\"a\"\n href={url}\n mode=\"ghost\"\n text={title}\n textAlign=\"center\"\n fontSize={2}\n ></Button>\n ))}\n </Stack>\n )\n}\n","import {Flex, Heading, Spinner} from '@sanity/ui'\nimport {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 <Heading as=\"h6\" align=\"center\">\n Logging you in…\n </Heading>\n <Flex paddingY={5} align=\"center\" justify=\"center\">\n <Spinner />\n </Flex>\n </LoginLayout>\n )\n}\n","import {Button, Heading, Stack, Text} from '@sanity/ui'\nimport {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 <Stack space={5} marginBottom={5}>\n <Heading as=\"h6\" align=\"center\">\n Authentication Error\n </Heading>\n <Text align=\"center\">Please try again or contact support if the problem persists.</Text>\n <Button mode=\"ghost\" onClick={handleRetry} text=\"Retry\" fontSize={2} />\n </Stack>\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 {AuthError} from './AuthError'\nimport {Login} from './Login'\nimport {LoginCallback} from './LoginCallback'\nimport {LoginError, type LoginErrorProps} from './LoginError'\nimport {type LoginLayoutProps} from './LoginLayout'\n\n/**\n * @alpha\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 * @alpha\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 JSX} from 'react'\n\nimport {SanityProvider} from '../context/SanityProvider'\nimport {AuthBoundary} from './auth/AuthBoundary'\n\n/**\n * @public\n *\n * @returns Rendered child component wrapped in a SanityProvider and AuthBoundary\n */\nexport function SanityApp({\n sanityConfig,\n children,\n}: {\n children: React.ReactNode\n sanityConfig: SanityConfig\n}): JSX.Element {\n const sanityInstance = createSanityInstance(sanityConfig)\n\n return (\n <SanityProvider sanityInstance={sanityInstance}>\n <AuthBoundary>{children}</AuthBoundary>\n </SanityProvider>\n )\n}\n"],"names":[],"mappings":";;;;;;;;AAWO,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;AAC7C,8BACG,KACC,EAAA,UAAA;AAAA,IAAA,oBAAC,MAAK,EAAA,SAAQ,UACZ,UAAA,oBAAC,aAAW,CAAA,GACd;AAAA,wBAEC,MAAK,EAAA,SAAQ,UACZ,UAAA,oBAAC,UAAO,OAAO,GAAG,UAAU,GACzB,gBAAM,IAAI,CAAC,SACT,oBAAA,MAAA,EAAK,MAAM,GACV,UAAA;AAAA,MAAC;AAAA,MAAA;AAAA,QACC,MAAM,KAAK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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;AC1FO,SAAS,UAAU;AAAA,EACxB;AAAA,EACA;AACF,GAGgB;AACR,QAAA,iBAAiB,qBAAqB,YAAY;AAExD,6BACG,gBAAe,EAAA,gBACd,UAAC,oBAAA,cAAA,EAAc,SAAS,CAAA,GAC1B;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"components.js","sources":["../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/SanityApp.tsx"],"sourcesContent":["/**\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 {Box, Flex, Inline, Text} from '@sanity/ui'\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 <Box>\n <Flex justify=\"center\">\n <SanityLogo />\n </Flex>\n\n <Flex justify=\"center\">\n <Inline space={2} paddingY={3}>\n {LINKS.map((link) => (\n <Text size={0} key={link.url}>\n <a\n href={link.url}\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n style={{color: 'inherit'}}\n >\n {link.title}\n </a>\n </Text>\n ))}\n </Inline>\n </Flex>\n </Box>\n )\n}\n","import {Card, Container} from '@sanity/ui'\n\nimport {LoginFooter} from './LoginFooter'\n\n/**\n * @alpha\n * @internal\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 <Container width={0}>\n <Card shadow={1} radius={2} padding={4}>\n {header && header}\n\n {children && children}\n\n {footer}\n </Card>\n </Container>\n )\n}\n","import {Box, Button, Flex, Heading, Spinner, Stack} from '@sanity/ui'\nimport {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 <Heading as=\"h6\" align=\"center\">\n Choose login provider:\n </Heading>\n\n <Suspense\n fallback={\n <Box padding={5}>\n <Flex align=\"center\" justify=\"center\">\n <Spinner />\n </Flex>\n </Box>\n }\n >\n <Providers />\n </Suspense>\n </LoginLayout>\n )\n}\n\nfunction Providers() {\n const loginUrls = useLoginUrls()\n\n return (\n <Stack space={3} marginY={5}>\n {loginUrls.map(({title, url}) => (\n <Button\n key={url}\n as=\"a\"\n href={url}\n mode=\"ghost\"\n text={title}\n textAlign=\"center\"\n fontSize={2}\n ></Button>\n ))}\n </Stack>\n )\n}\n","import {Flex, Heading, Spinner} from '@sanity/ui'\nimport {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 <Heading as=\"h6\" align=\"center\">\n Logging you in…\n </Heading>\n <Flex paddingY={5} align=\"center\" justify=\"center\">\n <Spinner />\n </Flex>\n </LoginLayout>\n )\n}\n","import {Button, Heading, Stack, Text} from '@sanity/ui'\nimport {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 <Stack space={5} marginBottom={5}>\n <Heading as=\"h6\" align=\"center\">\n Authentication Error\n </Heading>\n <Text align=\"center\">Please try again or contact support if the problem persists.</Text>\n <Button mode=\"ghost\" onClick={handleRetry} text=\"Retry\" fontSize={2} />\n </Stack>\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 {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 withing SanityOS if it is in an iframe.\nif (typeof window !== 'undefined' && window.self !== window.top) {\n import('@sanity/os/bridge')\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} from 'react'\n\nimport {SanityProvider} from '../context/SanityProvider'\nimport {AuthBoundary} from './auth/AuthBoundary'\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 const sanityInstance = createSanityInstance(sanityConfig)\n\n return (\n <SanityProvider sanityInstance={sanityInstance}>\n <AuthBoundary>{children}</AuthBoundary>\n </SanityProvider>\n )\n}\n"],"names":[],"mappings":";;;;;;;;AAWO,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;A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GAAG,MAAO,CAAA;AAAA,EAAA;AAGxC;AC/DO,SAAS,UAAU,EAAC,cAAc,YAAyC;AAC1E,QAAA,iBAAiB,qBAAqB,YAAY;AAExD,6BACG,gBAAe,EAAA,gBACd,UAAC,oBAAA,cAAA,EAAc,SAAS,CAAA,GAC1B;AAEJ;"}
|
package/dist/context.d.ts
CHANGED
|
@@ -2,22 +2,28 @@ import {ReactElement} from 'react'
|
|
|
2
2
|
import {SanityInstance} from '@sanity/sdk'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Top-level context provider that provides
|
|
6
|
-
* This must wrap any Sanity SDK React
|
|
7
|
-
* @
|
|
5
|
+
* Top-level context provider that provides access to the Sanity configuration instance.
|
|
6
|
+
* This must wrap any components making use of the Sanity SDK React hooks.
|
|
7
|
+
* @remarks In most cases, SanityApp should be used rather than SanityProvider directly; SanityApp bundles both SanityProvider and an authentication layer.
|
|
8
|
+
* @internal
|
|
8
9
|
* @param props - Sanity project and dataset configuration
|
|
9
10
|
* @returns Rendered component
|
|
10
11
|
* @example
|
|
11
12
|
* ```tsx
|
|
12
13
|
* import {createSanityInstance} from '@sanity/sdk'
|
|
13
|
-
* import {
|
|
14
|
+
* import {SanityProvider} from '@sanity/sdk-react'
|
|
15
|
+
*
|
|
16
|
+
* import MyAppRoot from './Root'
|
|
14
17
|
*
|
|
15
|
-
* const sanityInstance = createSanityInstance({
|
|
18
|
+
* const sanityInstance = createSanityInstance({
|
|
19
|
+
* projectId: 'your-project-id',
|
|
20
|
+
* dataset: 'production',
|
|
21
|
+
* })
|
|
16
22
|
*
|
|
17
23
|
* export default function MyApp() {
|
|
18
24
|
* return (
|
|
19
25
|
* <SanityProvider sanityInstance={sanityInstance}>
|
|
20
|
-
*
|
|
26
|
+
* <MyAppRoot />
|
|
21
27
|
* </SanityProvider>
|
|
22
28
|
* )
|
|
23
29
|
* }
|
package/dist/hooks.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {AuthProvider} from '@sanity/sdk'
|
|
2
2
|
import {AuthState} from '@sanity/sdk'
|
|
3
|
-
import {CurrentUser} from '@sanity/
|
|
3
|
+
import {CurrentUser} from '@sanity/sdk'
|
|
4
4
|
import {DocumentHandle} from '@sanity/sdk'
|
|
5
5
|
import {DocumentListOptions} from '@sanity/sdk'
|
|
6
6
|
import {FrameMessage} from '@sanity/sdk'
|
|
@@ -8,6 +8,8 @@ import {PreviewValue} from '@sanity/sdk'
|
|
|
8
8
|
import {SanityInstance} from '@sanity/sdk'
|
|
9
9
|
import {WindowMessage} from '@sanity/sdk'
|
|
10
10
|
|
|
11
|
+
export {CurrentUser}
|
|
12
|
+
|
|
11
13
|
/**
|
|
12
14
|
* @public
|
|
13
15
|
*/
|
|
@@ -25,7 +27,7 @@ export declare interface DocumentCollection {
|
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
/**
|
|
28
|
-
* @
|
|
30
|
+
* @internal
|
|
29
31
|
*/
|
|
30
32
|
export declare interface FrameConnection<TFrameMessage extends FrameMessage> {
|
|
31
33
|
connect: (frameWindow: Window) => () => void
|
|
@@ -50,17 +52,18 @@ export declare interface FrameConnection<TFrameMessage extends FrameMessage> {
|
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
/**
|
|
53
|
-
* @
|
|
55
|
+
* @internal
|
|
54
56
|
*/
|
|
55
57
|
export declare type MessageHandler<TWindowMessage extends WindowMessage> = (
|
|
56
58
|
event: TWindowMessage['data'],
|
|
57
59
|
) => TWindowMessage['response'] | Promise<TWindowMessage['response']>
|
|
58
60
|
|
|
59
61
|
/**
|
|
62
|
+
* @internal
|
|
60
63
|
* A React hook that subscribes to authentication state changes.
|
|
61
64
|
*
|
|
62
65
|
* This hook provides access to the current authentication state type from the Sanity auth store.
|
|
63
|
-
* It automatically re-renders
|
|
66
|
+
* It automatically re-renders when the authentication state changes.
|
|
64
67
|
*
|
|
65
68
|
* @remarks
|
|
66
69
|
* The hook uses `useSyncExternalStore` to safely subscribe to auth state changes
|
|
@@ -82,15 +85,32 @@ export declare const useAuthState: () => AuthState
|
|
|
82
85
|
|
|
83
86
|
/**
|
|
84
87
|
* Hook to get the currently logged in user
|
|
85
|
-
* @
|
|
88
|
+
* @internal
|
|
86
89
|
* @returns The current user or null if not authenticated
|
|
87
90
|
*/
|
|
88
91
|
export declare const useAuthToken: () => string | null
|
|
89
92
|
|
|
90
93
|
/**
|
|
91
|
-
*
|
|
94
|
+
* @TODO This should suspend! And possibly not return `null`?
|
|
95
|
+
*
|
|
92
96
|
* @public
|
|
93
|
-
*
|
|
97
|
+
*
|
|
98
|
+
* The `useCurrentUser` hook returns the currently authenticated user’s profile information (their name, email, roles, etc).
|
|
99
|
+
* If no users are currently logged in, the hook returns null.
|
|
100
|
+
*
|
|
101
|
+
* @returns The current user data, or `null` if not authenticated
|
|
102
|
+
*
|
|
103
|
+
* @example Rendering a basic user profile
|
|
104
|
+
* ```
|
|
105
|
+
* const user = useCurrentUser()
|
|
106
|
+
*
|
|
107
|
+
* return (
|
|
108
|
+
* <figure>
|
|
109
|
+
* <img src={user?.profileImage} alt=`Profile image for ${user?.name}` />
|
|
110
|
+
* <h2>{user?.name}</h2>
|
|
111
|
+
* </figure>
|
|
112
|
+
* )
|
|
113
|
+
* ```
|
|
94
114
|
*/
|
|
95
115
|
export declare const useCurrentUser: () => CurrentUser | null
|
|
96
116
|
|
|
@@ -120,13 +140,14 @@ export declare const useCurrentUser: () => CurrentUser | null
|
|
|
120
140
|
* )
|
|
121
141
|
* ```
|
|
122
142
|
*
|
|
123
|
-
* @example Retrieving all movies released since 1980, sorted by
|
|
143
|
+
* @example Retrieving all movies released since 1980, sorted by director’s last name
|
|
124
144
|
* ```
|
|
125
145
|
* const { results } = useDocuments({
|
|
126
146
|
* filter: '_type == "movie" && releaseDate >= "1980-01-01"',
|
|
127
147
|
* sort: [
|
|
128
148
|
* {
|
|
129
|
-
* field
|
|
149
|
+
* // Expand the `director` reference field with the dereferencing operator `->`
|
|
150
|
+
* field: 'director->lastName',
|
|
130
151
|
* sort: 'asc',
|
|
131
152
|
* },
|
|
132
153
|
* ],
|
|
@@ -147,7 +168,7 @@ export declare const useCurrentUser: () => CurrentUser | null
|
|
|
147
168
|
export declare function useDocuments(options?: DocumentListOptions): DocumentCollection
|
|
148
169
|
|
|
149
170
|
/**
|
|
150
|
-
* @
|
|
171
|
+
* @internal
|
|
151
172
|
*/
|
|
152
173
|
export declare function useFrameConnection<
|
|
153
174
|
TFrameMessage extends FrameMessage,
|
|
@@ -155,7 +176,7 @@ export declare function useFrameConnection<
|
|
|
155
176
|
>(options: UseFrameConnectionOptions<TWindowMessage>): FrameConnection<TFrameMessage>
|
|
156
177
|
|
|
157
178
|
/**
|
|
158
|
-
* @
|
|
179
|
+
* @internal
|
|
159
180
|
*/
|
|
160
181
|
export declare interface UseFrameConnectionOptions<TWindowMessage extends WindowMessage> {
|
|
161
182
|
name: string
|
|
@@ -165,6 +186,7 @@ export declare interface UseFrameConnectionOptions<TWindowMessage extends Window
|
|
|
165
186
|
}
|
|
166
187
|
|
|
167
188
|
/**
|
|
189
|
+
* @internal
|
|
168
190
|
* A React hook that returns a function for handling authentication callbacks.
|
|
169
191
|
*
|
|
170
192
|
* @remarks
|
|
@@ -208,6 +230,7 @@ export declare const useHandleCallback: () => (
|
|
|
208
230
|
) => Promise<string | false>
|
|
209
231
|
|
|
210
232
|
/**
|
|
233
|
+
* @internal
|
|
211
234
|
* A React hook that retrieves the available authentication provider URLs for login.
|
|
212
235
|
*
|
|
213
236
|
* @remarks
|
|
@@ -249,7 +272,7 @@ export declare function useLoginUrls(): AuthProvider[]
|
|
|
249
272
|
|
|
250
273
|
/**
|
|
251
274
|
* Hook to log out of the current session
|
|
252
|
-
* @
|
|
275
|
+
* @internal
|
|
253
276
|
* @returns A function to log out of the current session
|
|
254
277
|
*/
|
|
255
278
|
export declare const useLogOut: () => () => Promise<void>
|
|
@@ -270,8 +293,8 @@ export declare const useLogOut: () => () => Promise<void>
|
|
|
270
293
|
* // PreviewComponent.jsx
|
|
271
294
|
* export default function PreviewComponent({ document }) {
|
|
272
295
|
* const { results: { title, subtitle, media }, isPending } = usePreview({ document })
|
|
273
|
-
* return
|
|
274
|
-
* <article>
|
|
296
|
+
* return (
|
|
297
|
+
* <article style={{ opacity: isPending ? 0.5 : 1}}>
|
|
275
298
|
* {media?.type === 'image-asset' ? <img src={media.url} alt='' /> : ''}
|
|
276
299
|
* <h2>{title}</h2>
|
|
277
300
|
* <p>{subtitle}</p>
|
|
@@ -287,7 +310,9 @@ export declare const useLogOut: () => () => Promise<void>
|
|
|
287
310
|
* <ul>
|
|
288
311
|
* {isPending ? 'Loading…' : results.map(movie => (
|
|
289
312
|
* <li key={movie._id}>
|
|
290
|
-
* <
|
|
313
|
+
* <Suspense fallback='Loading…'>
|
|
314
|
+
* <PreviewComponent document={movie} />
|
|
315
|
+
* </Suspense>
|
|
291
316
|
* </li>
|
|
292
317
|
* ))}
|
|
293
318
|
* </ul>
|
|
@@ -305,7 +330,7 @@ export declare function usePreview({
|
|
|
305
330
|
*/
|
|
306
331
|
export declare interface UsePreviewOptions {
|
|
307
332
|
document: DocumentHandle
|
|
308
|
-
ref
|
|
333
|
+
ref?: React.RefObject<unknown>
|
|
309
334
|
}
|
|
310
335
|
|
|
311
336
|
/**
|
|
@@ -314,15 +339,15 @@ export declare interface UsePreviewOptions {
|
|
|
314
339
|
export declare interface UsePreviewResults {
|
|
315
340
|
/** The results of resolving the document’s preview values */
|
|
316
341
|
results: PreviewValue
|
|
317
|
-
/**
|
|
342
|
+
/** True when preview values are being refreshed */
|
|
318
343
|
isPending: boolean
|
|
319
344
|
}
|
|
320
345
|
|
|
321
346
|
/**
|
|
322
|
-
*
|
|
347
|
+
* `useSanityInstance` returns the current Sanity instance from the application context.
|
|
323
348
|
* This must be called from within a `SanityProvider` component.
|
|
324
349
|
* @public
|
|
325
|
-
* @returns
|
|
350
|
+
* @returns The current Sanity instance
|
|
326
351
|
* @example
|
|
327
352
|
* ```tsx
|
|
328
353
|
* const instance = useSanityInstance()
|
|
@@ -331,7 +356,7 @@ export declare interface UsePreviewResults {
|
|
|
331
356
|
export declare const useSanityInstance: () => SanityInstance
|
|
332
357
|
|
|
333
358
|
/**
|
|
334
|
-
* @
|
|
359
|
+
* @internal
|
|
335
360
|
*/
|
|
336
361
|
export declare function useWindowConnection<
|
|
337
362
|
TWindowMessage extends WindowMessage,
|
|
@@ -339,7 +364,7 @@ export declare function useWindowConnection<
|
|
|
339
364
|
>(options: UseWindowConnectionOptions<TFrameMessage>): WindowConnection<TWindowMessage>
|
|
340
365
|
|
|
341
366
|
/**
|
|
342
|
-
* @
|
|
367
|
+
* @internal
|
|
343
368
|
*/
|
|
344
369
|
export declare interface UseWindowConnectionOptions<TMessage extends FrameMessage> {
|
|
345
370
|
name: string
|
|
@@ -348,7 +373,7 @@ export declare interface UseWindowConnectionOptions<TMessage extends FrameMessag
|
|
|
348
373
|
}
|
|
349
374
|
|
|
350
375
|
/**
|
|
351
|
-
* @
|
|
376
|
+
* @internal
|
|
352
377
|
*/
|
|
353
378
|
export declare interface WindowConnection<TMessage extends WindowMessage> {
|
|
354
379
|
sendMessage: <TType extends TMessage['type']>(
|
|
@@ -363,7 +388,7 @@ export declare interface WindowConnection<TMessage extends WindowMessage> {
|
|
|
363
388
|
}
|
|
364
389
|
|
|
365
390
|
/**
|
|
366
|
-
* @
|
|
391
|
+
* @internal
|
|
367
392
|
*/
|
|
368
393
|
export declare type WindowMessageHandler<TFrameMessage extends FrameMessage> = (
|
|
369
394
|
event: TFrameMessage['data'],
|
package/dist/hooks.js
CHANGED
|
@@ -111,12 +111,13 @@ function usePreview({ document: { _id, _type }, ref }) {
|
|
|
111
111
|
), subscribe = useCallback(
|
|
112
112
|
(onStoreChanged) => {
|
|
113
113
|
const subscription = new Observable((observer) => {
|
|
114
|
-
if (typeof IntersectionObserver > "u")
|
|
114
|
+
if (typeof IntersectionObserver > "u" || typeof HTMLElement > "u")
|
|
115
|
+
return;
|
|
115
116
|
const intersectionObserver = new IntersectionObserver(
|
|
116
117
|
([entry]) => observer.next(entry.isIntersecting),
|
|
117
118
|
{ rootMargin: "0px", threshold: 0 }
|
|
118
119
|
);
|
|
119
|
-
return ref && intersectionObserver.observe(ref), () => intersectionObserver.disconnect();
|
|
120
|
+
return ref?.current && ref.current instanceof HTMLElement && intersectionObserver.observe(ref.current), () => intersectionObserver.disconnect();
|
|
120
121
|
}).pipe(
|
|
121
122
|
startWith(!1),
|
|
122
123
|
distinctUntilChanged(),
|
package/dist/hooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.js","sources":["../src/hooks/auth/useAuthToken.tsx","../src/hooks/auth/useCurrentUser.tsx","../src/hooks/comlink/useFrameConnection.ts","../src/hooks/comlink/useWindowConnection.ts","../src/hooks/documentCollection/useDocuments.ts","../src/hooks/preview/usePreview.tsx"],"sourcesContent":["import {getTokenState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * Hook to get the currently logged in user\n * @public\n * @returns The current user or null if not authenticated\n */\nexport const useAuthToken = createStateSourceHook(getTokenState)\n","import {getCurrentUserState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * Hook to get the currently logged in user\n * @public\n * @returns The current user or null if not authenticated\n */\nexport const useCurrentUser = createStateSourceHook(getCurrentUserState)\n","import {\n type FrameMessage,\n getOrCreateChannel,\n getOrCreateController,\n releaseChannel,\n type WindowMessage,\n} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @public\n */\nexport type FrameMessageHandler<TWindowMessage extends WindowMessage> = (\n event: TWindowMessage['data'],\n) => TWindowMessage['response'] | Promise<TWindowMessage['response']>\n\n/**\n * @public\n */\nexport interface UseFrameConnectionOptions<TWindowMessage extends WindowMessage> {\n name: string\n connectTo: string\n targetOrigin: string\n onMessage?: Record<string, FrameMessageHandler<TWindowMessage>>\n}\n\n/**\n * @public\n */\nexport interface FrameConnection<TFrameMessage extends FrameMessage> {\n connect: (frameWindow: Window) => () => void // Return cleanup function\n sendMessage: <T extends TFrameMessage['type']>(\n ...params: Extract<TFrameMessage, {type: T}>['data'] extends undefined\n ? [type: T]\n : [type: T, data: Extract<TFrameMessage, {type: T}>['data']]\n ) => void\n}\n\n/**\n * @public\n */\nexport function useFrameConnection<\n TFrameMessage extends FrameMessage,\n TWindowMessage extends WindowMessage,\n>(options: UseFrameConnectionOptions<TWindowMessage>): FrameConnection<TFrameMessage> {\n const {onMessage, targetOrigin, name, connectTo} = options\n const instance = useSanityInstance()\n\n const controller = useMemo(\n () => getOrCreateController(instance, targetOrigin),\n [instance, targetOrigin],\n )\n\n const channel = useMemo(\n () =>\n getOrCreateChannel(instance, {\n name,\n connectTo,\n }),\n [instance, name, connectTo],\n )\n\n useEffect(() => {\n if (!channel || !onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n const unsubscribe = channel.on(type, handler)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [channel, onMessage])\n\n const connect = useCallback(\n (frameWindow: Window) => {\n const removeTarget = controller?.addTarget(frameWindow)\n return () => {\n removeTarget?.()\n }\n },\n [controller],\n )\n\n const sendMessage = useCallback(\n <T extends TFrameMessage['type']>(\n type: T,\n data?: Extract<TFrameMessage, {type: T}>['data'],\n ) => {\n channel?.post(type, data)\n },\n [channel],\n )\n\n // cleanup channel on unmount\n useEffect(() => {\n return () => {\n releaseChannel(instance, name)\n }\n }, [name, instance])\n\n return {\n connect,\n sendMessage,\n }\n}\n","import {type FrameMessage, getOrCreateNode, releaseNode, type WindowMessage} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @public\n */\nexport type WindowMessageHandler<TFrameMessage extends FrameMessage> = (\n event: TFrameMessage['data'],\n) => TFrameMessage['response']\n\n/**\n * @public\n */\nexport interface UseWindowConnectionOptions<TMessage extends FrameMessage> {\n name: string\n connectTo: string\n onMessage?: Record<TMessage['type'], WindowMessageHandler<TMessage>>\n}\n\n/**\n * @public\n */\nexport interface WindowConnection<TMessage extends WindowMessage> {\n sendMessage: <TType extends TMessage['type']>(\n type: TType,\n data?: Extract<TMessage, {type: TType}>['data'],\n ) => void\n}\n\n/**\n * @public\n */\nexport function useWindowConnection<\n TWindowMessage extends WindowMessage,\n TFrameMessage extends FrameMessage,\n>(options: UseWindowConnectionOptions<TFrameMessage>): WindowConnection<TWindowMessage> {\n const {name, onMessage, connectTo} = options\n const instance = useSanityInstance()\n\n const node = useMemo(\n () => getOrCreateNode(instance, {name, connectTo}),\n [instance, name, connectTo],\n )\n\n useEffect(() => {\n if (!onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n const unsubscribe = node.on(type, handler as WindowMessageHandler<TFrameMessage>)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [node, onMessage])\n\n const sendMessage = useCallback(\n <TType extends WindowMessage['type']>(\n type: TType,\n data?: Extract<WindowMessage, {type: TType}>['data'],\n ) => {\n node?.post(type, data)\n },\n [node],\n )\n\n // cleanup node on unmount\n useEffect(() => {\n return () => {\n releaseNode(instance, name)\n }\n }, [instance, name])\n\n return {\n sendMessage,\n }\n}\n","import {createDocumentListStore, type DocumentHandle, type DocumentListOptions} from '@sanity/sdk'\nimport {useCallback, useEffect, useState, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @public\n */\nexport interface DocumentCollection {\n /** Retrieve more documents matching the provided options */\n loadMore: () => void\n /** The retrieved document handles of the documents matching the provided options */\n results: DocumentHandle[]\n /** Whether a retrieval of documents is in flight */\n isPending: boolean\n /** Whether more documents exist that match the provided options than have been retrieved */\n hasMore: boolean\n /** The total number of documents in the collection */\n count: number\n}\n\ntype DocumentListStore = ReturnType<typeof createDocumentListStore>\ntype DocumentListState = ReturnType<DocumentListStore['getState']>['getCurrent']\nconst STABLE_EMPTY = {\n results: [],\n isPending: false,\n hasMore: false,\n count: 0,\n}\n\n/**\n * @public\n *\n * The `useDocuments` hook retrieves and provides access to a live collection of documents, optionally filtered, sorted, and matched to a given Content Lake perspective.\n * Because the returned document collection is live, the results will update in real time until the component invoking the hook is unmounted.\n *\n * @param options - Options for narrowing and sorting the document collection\n * @returns The collection of documents matching the provided options (if any), as well as properties describing the collection and a function to load more.\n *\n * @example Retrieving all documents of type 'movie'\n * ```\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n *\n * return (\n * <div>\n * <h1>Movies</h1>\n * {results && (\n * <ul>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ul>\n * )}\n * {isPending && <div>Loading movies…</div>}\n * </div>\n * )\n * ```\n *\n * @example Retrieving all movies released since 1980, sorted by release date\n * ```\n * const { results } = useDocuments({\n * filter: '_type == \"movie\" && releaseDate >= \"1980-01-01\"',\n * sort: [\n * {\n * field: 'releaseDate',\n * sort: 'asc',\n * },\n * ],\n * })\n *\n * return (\n * <div>\n * <h1>Movies released since 1980</h1>\n * {results && (\n * <ol>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ol>\n * )}\n * </div>\n * )\n * ```\n */\nexport function useDocuments(options: DocumentListOptions = {}): DocumentCollection {\n const instance = useSanityInstance()\n\n // NOTE: useState is used because it guaranteed to return a stable reference\n // across renders\n const [ref] = useState<{\n storeInstance: DocumentListStore | null\n getCurrent: DocumentListState\n initialOptions: DocumentListOptions\n }>(() => ({\n storeInstance: null,\n getCurrent: () => STABLE_EMPTY,\n initialOptions: options,\n }))\n\n // serialize options to ensure it only calls `setOptions` when the values\n // themselves changes (in cases where devs put config inline)\n const serializedOptions = JSON.stringify(options)\n useEffect(() => {\n ref.storeInstance?.setOptions(JSON.parse(serializedOptions))\n }, [ref, serializedOptions])\n\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n // to match the lifecycle of `useSyncExternalState`, we create the store\n // instance after subscribe and mutate the ref to connect everything\n ref.storeInstance = createDocumentListStore(instance)\n ref.storeInstance.setOptions(ref.initialOptions)\n const state = ref.storeInstance.getState()\n ref.getCurrent = state.getCurrent\n const unsubscribe = state.subscribe(onStoreChanged)\n\n return () => {\n // unsubscribe to clean up the state subscriptions\n unsubscribe()\n // dispose of the instance\n ref.storeInstance?.dispose()\n }\n },\n [instance, ref],\n )\n\n const getSnapshot = useCallback(() => {\n return ref.getCurrent()\n }, [ref])\n\n const state = useSyncExternalStore(subscribe, getSnapshot)\n\n const loadMore = useCallback(() => {\n ref.storeInstance?.loadMore()\n }, [ref])\n\n return {loadMore, ...state}\n}\n","import {type DocumentHandle, getPreviewState, type PreviewValue, resolvePreview} from '@sanity/sdk'\nimport {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {distinctUntilChanged, EMPTY, Observable, startWith, switchMap} from 'rxjs'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @alpha\n */\nexport interface UsePreviewOptions {\n document: DocumentHandle\n ref: HTMLElement | null\n}\n\n/**\n * @alpha\n */\nexport interface UsePreviewResults {\n /** The results of resolving the document’s preview values */\n results: PreviewValue\n /** Whether the resolution of the preview values is pending */\n isPending: boolean\n}\n\n/**\n * @alpha\n *\n * The `usePreview` hook takes a document (via a `DocumentHandle`) and returns its resolved preview values,\n * including the document’s `title`, `subtitle`, `media`, and `status`. These values are live and will update in realtime.\n * To reduce unnecessary network requests for resolving the preview values, an optional `ref` can be passed to the hook so that preview\n * resolution will only occur if the `ref` is intersecting the current viewport.\n *\n * @param options - The document handle for the document you want to resolve preview values for, and an optional ref\n * @returns The preview values for the given document and a boolean to indicate whether the resolution is pending\n *\n * @example Combining with useDocuments to render a collection of document previews\n * ```\n * // PreviewComponent.jsx\n * export default function PreviewComponent({ document }) {\n * const { results: { title, subtitle, media }, isPending } = usePreview({ document })\n * return isPending ? 'Loading…' : (\n * <article>\n * {media?.type === 'image-asset' ? <img src={media.url} alt='' /> : ''}\n * <h2>{title}</h2>\n * <p>{subtitle}</p>\n * </article>\n * )\n * }\n *\n * // DocumentList.jsx\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n * return (\n * <div>\n * <h1>Movies</h1>\n * <ul>\n * {isPending ? 'Loading…' : results.map(movie => (\n * <li key={movie._id}>\n * <PreviewComponent document={movie} />\n * </li>\n * ))}\n * </ul>\n * </div>\n * )\n * ```\n */\nexport function usePreview({document: {_id, _type}, ref}: UsePreviewOptions): UsePreviewResults {\n const instance = useSanityInstance()\n\n const stateSource = useMemo(\n () => getPreviewState(instance, {document: {_id, _type}}),\n [instance, _id, _type],\n )\n\n // Create subscribe function for useSyncExternalStore\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n const subscription = new Observable<boolean>((observer) => {\n // for environments that don't have an intersection observer\n if (typeof IntersectionObserver === 'undefined') return\n\n const intersectionObserver = new IntersectionObserver(\n ([entry]) => observer.next(entry.isIntersecting),\n {rootMargin: '0px', threshold: 0},\n )\n if (ref) intersectionObserver.observe(ref)\n return () => intersectionObserver.disconnect()\n })\n .pipe(\n startWith(false),\n distinctUntilChanged(),\n switchMap((isVisible) =>\n isVisible\n ? new Observable<void>((obs) => {\n return stateSource.subscribe(() => obs.next())\n })\n : EMPTY,\n ),\n )\n .subscribe({next: onStoreChanged})\n\n return () => subscription.unsubscribe()\n },\n [stateSource, ref],\n )\n\n // Create getSnapshot function to return current state\n const getSnapshot = useCallback(() => {\n const currentState = stateSource.getCurrent()\n if (currentState.results === null) throw resolvePreview(instance, {document: {_id, _type}})\n return currentState as UsePreviewResults\n }, [_id, _type, instance, stateSource])\n\n return useSyncExternalStore(subscribe, getSnapshot)\n}\n"],"names":["state"],"mappings":";;;;;AASa,MAAA,eAAe,sBAAsB,aAAa,GCAlD,iB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AAA,EAAA,GAIb,cAAc,YAAY,MAAM;AAC9B,UAAA,eAAe,YAAY,WAAW;AAC5C,QAAI,aAAa,YAAY,KAAM,OAAM,eAAe,UAAU,EAAC,UAAU,EAAC,KAAK,MAAK,EAAA,CAAE;AACnF,WAAA;AAAA,KACN,CAAC,KAAK,OAAO,UAAU,WAAW,CAAC;AAE/B,SAAA,qBAAqB,WAAW,WAAW;AACpD;"}
|
|
1
|
+
{"version":3,"file":"hooks.js","sources":["../src/hooks/auth/useAuthToken.tsx","../src/hooks/auth/useCurrentUser.tsx","../src/hooks/comlink/useFrameConnection.ts","../src/hooks/comlink/useWindowConnection.ts","../src/hooks/documentCollection/useDocuments.ts","../src/hooks/preview/usePreview.tsx"],"sourcesContent":["import {getTokenState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * Hook to get the currently logged in user\n * @internal\n * @returns The current user or null if not authenticated\n */\nexport const useAuthToken = createStateSourceHook(getTokenState)\n","import {type CurrentUser, getCurrentUserState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * @TODO This should suspend! And possibly not return `null`?\n *\n * @public\n *\n * The `useCurrentUser` hook returns the currently authenticated user’s profile information (their name, email, roles, etc).\n * If no users are currently logged in, the hook returns null.\n *\n * @returns The current user data, or `null` if not authenticated\n *\n * @example Rendering a basic user profile\n * ```\n * const user = useCurrentUser()\n *\n * return (\n * <figure>\n * <img src={user?.profileImage} alt=`Profile image for ${user?.name}` />\n * <h2>{user?.name}</h2>\n * </figure>\n * )\n * ```\n */\nexport const useCurrentUser: () => CurrentUser | null = createStateSourceHook(getCurrentUserState)\n","import {\n type FrameMessage,\n getOrCreateChannel,\n getOrCreateController,\n releaseChannel,\n type WindowMessage,\n} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n */\nexport type FrameMessageHandler<TWindowMessage extends WindowMessage> = (\n event: TWindowMessage['data'],\n) => TWindowMessage['response'] | Promise<TWindowMessage['response']>\n\n/**\n * @internal\n */\nexport interface UseFrameConnectionOptions<TWindowMessage extends WindowMessage> {\n name: string\n connectTo: string\n targetOrigin: string\n onMessage?: Record<string, FrameMessageHandler<TWindowMessage>>\n}\n\n/**\n * @internal\n */\nexport interface FrameConnection<TFrameMessage extends FrameMessage> {\n connect: (frameWindow: Window) => () => void // Return cleanup function\n sendMessage: <T extends TFrameMessage['type']>(\n ...params: Extract<TFrameMessage, {type: T}>['data'] extends undefined\n ? [type: T]\n : [type: T, data: Extract<TFrameMessage, {type: T}>['data']]\n ) => void\n}\n\n/**\n * @internal\n */\nexport function useFrameConnection<\n TFrameMessage extends FrameMessage,\n TWindowMessage extends WindowMessage,\n>(options: UseFrameConnectionOptions<TWindowMessage>): FrameConnection<TFrameMessage> {\n const {onMessage, targetOrigin, name, connectTo} = options\n const instance = useSanityInstance()\n\n const controller = useMemo(\n () => getOrCreateController(instance, targetOrigin),\n [instance, targetOrigin],\n )\n\n const channel = useMemo(\n () =>\n getOrCreateChannel(instance, {\n name,\n connectTo,\n }),\n [instance, name, connectTo],\n )\n\n useEffect(() => {\n if (!channel || !onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n const unsubscribe = channel.on(type, handler)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [channel, onMessage])\n\n const connect = useCallback(\n (frameWindow: Window) => {\n const removeTarget = controller?.addTarget(frameWindow)\n return () => {\n removeTarget?.()\n }\n },\n [controller],\n )\n\n const sendMessage = useCallback(\n <T extends TFrameMessage['type']>(\n type: T,\n data?: Extract<TFrameMessage, {type: T}>['data'],\n ) => {\n channel?.post(type, data)\n },\n [channel],\n )\n\n // cleanup channel on unmount\n useEffect(() => {\n return () => {\n releaseChannel(instance, name)\n }\n }, [name, instance])\n\n return {\n connect,\n sendMessage,\n }\n}\n","import {type FrameMessage, getOrCreateNode, releaseNode, type WindowMessage} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n */\nexport type WindowMessageHandler<TFrameMessage extends FrameMessage> = (\n event: TFrameMessage['data'],\n) => TFrameMessage['response']\n\n/**\n * @internal\n */\nexport interface UseWindowConnectionOptions<TMessage extends FrameMessage> {\n name: string\n connectTo: string\n onMessage?: Record<TMessage['type'], WindowMessageHandler<TMessage>>\n}\n\n/**\n * @internal\n */\nexport interface WindowConnection<TMessage extends WindowMessage> {\n sendMessage: <TType extends TMessage['type']>(\n type: TType,\n data?: Extract<TMessage, {type: TType}>['data'],\n ) => void\n}\n\n/**\n * @internal\n */\nexport function useWindowConnection<\n TWindowMessage extends WindowMessage,\n TFrameMessage extends FrameMessage,\n>(options: UseWindowConnectionOptions<TFrameMessage>): WindowConnection<TWindowMessage> {\n const {name, onMessage, connectTo} = options\n const instance = useSanityInstance()\n\n const node = useMemo(\n () => getOrCreateNode(instance, {name, connectTo}),\n [instance, name, connectTo],\n )\n\n useEffect(() => {\n if (!onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n const unsubscribe = node.on(type, handler as WindowMessageHandler<TFrameMessage>)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [node, onMessage])\n\n const sendMessage = useCallback(\n <TType extends WindowMessage['type']>(\n type: TType,\n data?: Extract<WindowMessage, {type: TType}>['data'],\n ) => {\n node?.post(type, data)\n },\n [node],\n )\n\n // cleanup node on unmount\n useEffect(() => {\n return () => {\n releaseNode(instance, name)\n }\n }, [instance, name])\n\n return {\n sendMessage,\n }\n}\n","import {createDocumentListStore, type DocumentHandle, type DocumentListOptions} from '@sanity/sdk'\nimport {useCallback, useEffect, useState, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @public\n */\nexport interface DocumentCollection {\n /** Retrieve more documents matching the provided options */\n loadMore: () => void\n /** The retrieved document handles of the documents matching the provided options */\n results: DocumentHandle[]\n /** Whether a retrieval of documents is in flight */\n isPending: boolean\n /** Whether more documents exist that match the provided options than have been retrieved */\n hasMore: boolean\n /** The total number of documents in the collection */\n count: number\n}\n\ntype DocumentListStore = ReturnType<typeof createDocumentListStore>\ntype DocumentListState = ReturnType<DocumentListStore['getState']>['getCurrent']\nconst STABLE_EMPTY = {\n results: [],\n isPending: false,\n hasMore: false,\n count: 0,\n}\n\n/**\n * @public\n *\n * The `useDocuments` hook retrieves and provides access to a live collection of documents, optionally filtered, sorted, and matched to a given Content Lake perspective.\n * Because the returned document collection is live, the results will update in real time until the component invoking the hook is unmounted.\n *\n * @param options - Options for narrowing and sorting the document collection\n * @returns The collection of documents matching the provided options (if any), as well as properties describing the collection and a function to load more.\n *\n * @example Retrieving all documents of type 'movie'\n * ```\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n *\n * return (\n * <div>\n * <h1>Movies</h1>\n * {results && (\n * <ul>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ul>\n * )}\n * {isPending && <div>Loading movies…</div>}\n * </div>\n * )\n * ```\n *\n * @example Retrieving all movies released since 1980, sorted by director’s last name\n * ```\n * const { results } = useDocuments({\n * filter: '_type == \"movie\" && releaseDate >= \"1980-01-01\"',\n * sort: [\n * {\n * // Expand the `director` reference field with the dereferencing operator `->`\n * field: 'director->lastName',\n * sort: 'asc',\n * },\n * ],\n * })\n *\n * return (\n * <div>\n * <h1>Movies released since 1980</h1>\n * {results && (\n * <ol>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ol>\n * )}\n * </div>\n * )\n * ```\n */\nexport function useDocuments(options: DocumentListOptions = {}): DocumentCollection {\n const instance = useSanityInstance()\n\n // NOTE: useState is used because it guaranteed to return a stable reference\n // across renders\n const [ref] = useState<{\n storeInstance: DocumentListStore | null\n getCurrent: DocumentListState\n initialOptions: DocumentListOptions\n }>(() => ({\n storeInstance: null,\n getCurrent: () => STABLE_EMPTY,\n initialOptions: options,\n }))\n\n // serialize options to ensure it only calls `setOptions` when the values\n // themselves changes (in cases where devs put config inline)\n const serializedOptions = JSON.stringify(options)\n useEffect(() => {\n ref.storeInstance?.setOptions(JSON.parse(serializedOptions))\n }, [ref, serializedOptions])\n\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n // to match the lifecycle of `useSyncExternalState`, we create the store\n // instance after subscribe and mutate the ref to connect everything\n ref.storeInstance = createDocumentListStore(instance)\n ref.storeInstance.setOptions(ref.initialOptions)\n const state = ref.storeInstance.getState()\n ref.getCurrent = state.getCurrent\n const unsubscribe = state.subscribe(onStoreChanged)\n\n return () => {\n // unsubscribe to clean up the state subscriptions\n unsubscribe()\n // dispose of the instance\n ref.storeInstance?.dispose()\n }\n },\n [instance, ref],\n )\n\n const getSnapshot = useCallback(() => {\n return ref.getCurrent()\n }, [ref])\n\n const state = useSyncExternalStore(subscribe, getSnapshot)\n\n const loadMore = useCallback(() => {\n ref.storeInstance?.loadMore()\n }, [ref])\n\n return {loadMore, ...state}\n}\n","import {type DocumentHandle, getPreviewState, type PreviewValue, resolvePreview} from '@sanity/sdk'\nimport {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {distinctUntilChanged, EMPTY, Observable, startWith, switchMap} from 'rxjs'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @alpha\n */\nexport interface UsePreviewOptions {\n document: DocumentHandle\n ref?: React.RefObject<unknown>\n}\n\n/**\n * @alpha\n */\nexport interface UsePreviewResults {\n /** The results of resolving the document’s preview values */\n results: PreviewValue\n /** True when preview values are being refreshed */\n isPending: boolean\n}\n\n/**\n * @alpha\n *\n * The `usePreview` hook takes a document (via a `DocumentHandle`) and returns its resolved preview values,\n * including the document’s `title`, `subtitle`, `media`, and `status`. These values are live and will update in realtime.\n * To reduce unnecessary network requests for resolving the preview values, an optional `ref` can be passed to the hook so that preview\n * resolution will only occur if the `ref` is intersecting the current viewport.\n *\n * @param options - The document handle for the document you want to resolve preview values for, and an optional ref\n * @returns The preview values for the given document and a boolean to indicate whether the resolution is pending\n *\n * @example Combining with useDocuments to render a collection of document previews\n * ```\n * // PreviewComponent.jsx\n * export default function PreviewComponent({ document }) {\n * const { results: { title, subtitle, media }, isPending } = usePreview({ document })\n * return (\n * <article style={{ opacity: isPending ? 0.5 : 1}}>\n * {media?.type === 'image-asset' ? <img src={media.url} alt='' /> : ''}\n * <h2>{title}</h2>\n * <p>{subtitle}</p>\n * </article>\n * )\n * }\n *\n * // DocumentList.jsx\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n * return (\n * <div>\n * <h1>Movies</h1>\n * <ul>\n * {isPending ? 'Loading…' : results.map(movie => (\n * <li key={movie._id}>\n * <Suspense fallback='Loading…'>\n * <PreviewComponent document={movie} />\n * </Suspense>\n * </li>\n * ))}\n * </ul>\n * </div>\n * )\n * ```\n */\nexport function usePreview({document: {_id, _type}, ref}: UsePreviewOptions): UsePreviewResults {\n const instance = useSanityInstance()\n\n const stateSource = useMemo(\n () => getPreviewState(instance, {document: {_id, _type}}),\n [instance, _id, _type],\n )\n\n // Create subscribe function for useSyncExternalStore\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n const subscription = new Observable<boolean>((observer) => {\n // for environments that don't have an intersection observer\n if (typeof IntersectionObserver === 'undefined' || typeof HTMLElement === 'undefined') {\n return\n }\n\n const intersectionObserver = new IntersectionObserver(\n ([entry]) => observer.next(entry.isIntersecting),\n {rootMargin: '0px', threshold: 0},\n )\n if (ref?.current && ref.current instanceof HTMLElement) {\n intersectionObserver.observe(ref.current)\n }\n return () => intersectionObserver.disconnect()\n })\n .pipe(\n startWith(false),\n distinctUntilChanged(),\n switchMap((isVisible) =>\n isVisible\n ? new Observable<void>((obs) => {\n return stateSource.subscribe(() => obs.next())\n })\n : EMPTY,\n ),\n )\n .subscribe({next: onStoreChanged})\n\n return () => subscription.unsubscribe()\n },\n [stateSource, ref],\n )\n\n // Create getSnapshot function to return current state\n const getSnapshot = useCallback(() => {\n const currentState = stateSource.getCurrent()\n if (currentState.results === null) throw resolvePreview(instance, {document: {_id, _type}})\n return currentState as UsePreviewResults\n }, [_id, _type, instance, stateSource])\n\n return useSyncExternalStore(subscribe, getSnapshot)\n}\n"],"names":["state"],"mappings":";;;;;AASa,MAAA,eAAe,sBAAsB,aAAa,GCiBlD,iBAA2C,sBAAsB,mBAAmB;ACiB1F,SAAS,mBAGd,SAAoF;AAC9E,QAAA,EAAC,WAAW,cAAc,MAAM,UAAA,IAAa,SAC7C,WAAW,qBAEX,aAAa;AAAA,IACjB,MAAM,sBAAsB,UAAU,YAAY;AAAA,IAClD,CAAC,UAAU,YAAY;AAAA,KAGnB,UAAU;AAAA,IACd,MACE,mBAAmB,UAAU;AAAA,MAC3B;AAAA,MACA;AAAA,IAAA,CACD;AAAA,IACH,CAAC,UAAU,MAAM,SAAS;AAAA,EAC5B;AAEA,YAAU,MAAM;AACV,QAAA,CAAC,WAAW,CAAC,UAAW;AAE5B,UAAM,gBAAmC,CAAC;AAEnC,WAAA,OAAA,QAAQ,SAAS,EAAE,QAAQ,CAAC,CAAC,MAAM,OAAO,MAAM;AACrD,YAAM,cAAc,QAAQ,GAAG,MAAM,OAAO;AAC5C,oBAAc,KAAK,WAAW;AAAA,IAC/B,CAAA,GAEM,MAAM;AACX,oBAAc,QAAQ,CAAC,UAAU,MAAA,CAAO;AAAA,IAC1C;AAAA,EAAA,GACC,CAAC,SAAS,SAAS,CAAC;AAEvB,QAAM,UAAU;AAAA,IACd,CAAC,gBAAwB;AACjB,YAAA,eAAe,YAAY,UAAU,WAAW;AACtD,aAAO,MAAM;AACI,uBAAA;AAAA,MACjB;AAAA,IACF;AAAA,IACA,CAAC,UAAU;AAAA,KAGP,cAAc;AAAA,IAClB,CACE,MACA,SACG;AACM,eAAA,KAAK,MAAM,IAAI;A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zC,qBAAqB,QAAQ,IAAI,OAAO,GAEnC,MAAM,qBAAqB,WAAW;AAAA,MAC9C,CAAA,EACE;AAAA,QACC,UAAU,EAAK;AAAA,QACf,qBAAqB;AAAA,QACrB;AAAA,UAAU,CAAC,cACT,YACI,IAAI,WAAiB,CAAC,QACb,YAAY,UAAU,MAAM,IAAI,KAAK,CAAC,CAC9C,IACD;AAAA,QAAA;AAAA,MAGP,EAAA,UAAU,EAAC,MAAM,gBAAe;AAE5B,aAAA,MAAM,aAAa,YAAY;AAAA,IACxC;AAAA,IACA,CAAC,aAAa,GAAG;AAAA,EAAA,GAIb,cAAc,YAAY,MAAM;AAC9B,UAAA,eAAe,YAAY,WAAW;AAC5C,QAAI,aAAa,YAAY,KAAM,OAAM,eAAe,UAAU,EAAC,UAAU,EAAC,KAAK,MAAK,EAAA,CAAE;AACnF,WAAA;AAAA,KACN,CAAC,KAAK,OAAO,UAAU,WAAW,CAAC;AAE/B,SAAA,qBAAqB,WAAW,WAAW;AACpD;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/sdk-react",
|
|
3
|
-
"version": "0.0.0-alpha.
|
|
3
|
+
"version": "0.0.0-alpha.7",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Sanity SDK React toolkit for Content OS",
|
|
6
6
|
"keywords": [
|
|
@@ -68,30 +68,31 @@
|
|
|
68
68
|
"prettier": "@sanity/prettier-config",
|
|
69
69
|
"dependencies": {
|
|
70
70
|
"@sanity/logos": "^2.1.13",
|
|
71
|
+
"@sanity/os": "^0.2.0",
|
|
71
72
|
"@sanity/ui": "^2.8.19",
|
|
72
73
|
"react-error-boundary": "^4.1.2",
|
|
73
74
|
"rxjs": "^7.8.1",
|
|
74
|
-
"@sanity/sdk": "0.0.0-alpha.
|
|
75
|
+
"@sanity/sdk": "0.0.0-alpha.7"
|
|
75
76
|
},
|
|
76
77
|
"devDependencies": {
|
|
77
|
-
"@sanity/client": "^6.
|
|
78
|
-
"@sanity/comlink": "^2.0.
|
|
78
|
+
"@sanity/client": "^6.27.2",
|
|
79
|
+
"@sanity/comlink": "^2.0.5",
|
|
79
80
|
"@sanity/pkg-utils": "^6.12.2",
|
|
80
81
|
"@sanity/prettier-config": "^1.0.3",
|
|
81
82
|
"@testing-library/jest-dom": "^6.6.3",
|
|
82
|
-
"@testing-library/react": "^16.
|
|
83
|
-
"@types/react": "^19.0.
|
|
84
|
-
"@types/react-dom": "^19.0.
|
|
83
|
+
"@testing-library/react": "^16.2.0",
|
|
84
|
+
"@types/react": "^19.0.8",
|
|
85
|
+
"@types/react-dom": "^19.0.3",
|
|
85
86
|
"@vitejs/plugin-react": "^4.3.4",
|
|
86
|
-
"@vitest/coverage-v8": "
|
|
87
|
+
"@vitest/coverage-v8": "3.0.5",
|
|
87
88
|
"eslint": "^9.17.0",
|
|
88
89
|
"jsdom": "^25.0.1",
|
|
89
90
|
"prettier": "^3.4.2",
|
|
90
91
|
"react": "^19.0.0",
|
|
91
92
|
"react-dom": "^19.0.0",
|
|
92
93
|
"typescript": "^5.7.2",
|
|
93
|
-
"vite": "^5.4.
|
|
94
|
-
"vitest": "^
|
|
94
|
+
"vite": "^5.4.14",
|
|
95
|
+
"vitest": "^3.0.5",
|
|
95
96
|
"@repo/config-eslint": "0.0.0",
|
|
96
97
|
"@repo/config-test": "0.0.1",
|
|
97
98
|
"@repo/package.config": "0.0.1",
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export {AuthBoundary} from '../components/auth/AuthBoundary'
|
|
2
|
-
export {SanityApp} from '../components/SanityApp'
|
|
2
|
+
export {SanityApp, type SanityAppProps} from '../components/SanityApp'
|
package/src/_exports/hooks.ts
CHANGED
|
@@ -1,21 +1,48 @@
|
|
|
1
1
|
import {createSanityInstance, type SanityConfig} from '@sanity/sdk'
|
|
2
|
-
import {type
|
|
2
|
+
import {type ReactElement} from 'react'
|
|
3
3
|
|
|
4
4
|
import {SanityProvider} from '../context/SanityProvider'
|
|
5
5
|
import {AuthBoundary} from './auth/AuthBoundary'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @public
|
|
9
|
-
*
|
|
10
|
-
* @returns Rendered child component wrapped in a SanityProvider and AuthBoundary
|
|
11
9
|
*/
|
|
12
|
-
export
|
|
13
|
-
sanityConfig,
|
|
14
|
-
children,
|
|
15
|
-
}: {
|
|
16
|
-
children: React.ReactNode
|
|
10
|
+
export interface SanityAppProps {
|
|
17
11
|
sanityConfig: SanityConfig
|
|
18
|
-
|
|
12
|
+
children: React.ReactNode
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @public
|
|
17
|
+
*
|
|
18
|
+
* The SanityApp component provides your Sanity application with access to your Sanity configuration,
|
|
19
|
+
* as well as application context and state which is used by the Sanity React hooks. Your application
|
|
20
|
+
* must be wrapped with the SanityApp component to function properly.
|
|
21
|
+
*
|
|
22
|
+
* @param props - Your Sanity configuration and the React children to render
|
|
23
|
+
* @returns Your Sanity application, integrated with your Sanity configuration and application context
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```
|
|
27
|
+
* import { SanityApp } from '@sanity/sdk-react
|
|
28
|
+
*
|
|
29
|
+
* import MyAppRoot from './Root'
|
|
30
|
+
*
|
|
31
|
+
* const mySanityConfig = {
|
|
32
|
+
* procectId: 'my-project-id',
|
|
33
|
+
* dataset: 'production',
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* export default function MyApp() {
|
|
37
|
+
* return (
|
|
38
|
+
* <SanityApp sanityConfig={mySanityConfig}>
|
|
39
|
+
* <MyAppRoot />
|
|
40
|
+
* </SanityApp>
|
|
41
|
+
* )
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function SanityApp({sanityConfig, children}: SanityAppProps): ReactElement {
|
|
19
46
|
const sanityInstance = createSanityInstance(sanityConfig)
|
|
20
47
|
|
|
21
48
|
return (
|
|
@@ -9,8 +9,14 @@ import {LoginCallback} from './LoginCallback'
|
|
|
9
9
|
import {LoginError, type LoginErrorProps} from './LoginError'
|
|
10
10
|
import {type LoginLayoutProps} from './LoginLayout'
|
|
11
11
|
|
|
12
|
+
// Only import bridge if we're in an iframe. This assumes that the app is
|
|
13
|
+
// running withing SanityOS if it is in an iframe.
|
|
14
|
+
if (typeof window !== 'undefined' && window.self !== window.top) {
|
|
15
|
+
import('@sanity/os/bridge')
|
|
16
|
+
}
|
|
17
|
+
|
|
12
18
|
/**
|
|
13
|
-
* @
|
|
19
|
+
* @internal
|
|
14
20
|
*/
|
|
15
21
|
interface AuthBoundaryProps extends LoginLayoutProps {
|
|
16
22
|
/**
|
|
@@ -52,7 +58,7 @@ interface AuthBoundaryProps extends LoginLayoutProps {
|
|
|
52
58
|
* }
|
|
53
59
|
* ```
|
|
54
60
|
*
|
|
55
|
-
* @
|
|
61
|
+
* @internal
|
|
56
62
|
*/
|
|
57
63
|
export function AuthBoundary({
|
|
58
64
|
LoginErrorComponent = LoginError,
|
|
@@ -12,22 +12,28 @@ export interface SanityProviderProps {
|
|
|
12
12
|
export const SanityInstanceContext = createContext<SanityInstance | null>(null)
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* Top-level context provider that provides
|
|
16
|
-
* This must wrap any Sanity SDK React
|
|
17
|
-
* @
|
|
15
|
+
* Top-level context provider that provides access to the Sanity configuration instance.
|
|
16
|
+
* This must wrap any components making use of the Sanity SDK React hooks.
|
|
17
|
+
* @remarks In most cases, SanityApp should be used rather than SanityProvider directly; SanityApp bundles both SanityProvider and an authentication layer.
|
|
18
|
+
* @internal
|
|
18
19
|
* @param props - Sanity project and dataset configuration
|
|
19
20
|
* @returns Rendered component
|
|
20
21
|
* @example
|
|
21
22
|
* ```tsx
|
|
22
23
|
* import {createSanityInstance} from '@sanity/sdk'
|
|
23
|
-
* import {
|
|
24
|
+
* import {SanityProvider} from '@sanity/sdk-react'
|
|
25
|
+
*
|
|
26
|
+
* import MyAppRoot from './Root'
|
|
24
27
|
*
|
|
25
|
-
* const sanityInstance = createSanityInstance({
|
|
28
|
+
* const sanityInstance = createSanityInstance({
|
|
29
|
+
* projectId: 'your-project-id',
|
|
30
|
+
* dataset: 'production',
|
|
31
|
+
* })
|
|
26
32
|
*
|
|
27
33
|
* export default function MyApp() {
|
|
28
34
|
* return (
|
|
29
35
|
* <SanityProvider sanityInstance={sanityInstance}>
|
|
30
|
-
*
|
|
36
|
+
* <MyAppRoot />
|
|
31
37
|
* </SanityProvider>
|
|
32
38
|
* )
|
|
33
39
|
* }
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import {getAuthState} from '@sanity/sdk'
|
|
1
|
+
import {type AuthState, getAuthState} from '@sanity/sdk'
|
|
2
2
|
|
|
3
3
|
import {createStateSourceHook} from '../helpers/createStateSourceHook'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
+
* @internal
|
|
6
7
|
* A React hook that subscribes to authentication state changes.
|
|
7
8
|
*
|
|
8
9
|
* This hook provides access to the current authentication state type from the Sanity auth store.
|
|
9
|
-
* It automatically re-renders
|
|
10
|
+
* It automatically re-renders when the authentication state changes.
|
|
10
11
|
*
|
|
11
12
|
* @remarks
|
|
12
13
|
* The hook uses `useSyncExternalStore` to safely subscribe to auth state changes
|
|
@@ -24,4 +25,4 @@ import {createStateSourceHook} from '../helpers/createStateSourceHook'
|
|
|
24
25
|
*
|
|
25
26
|
* @public
|
|
26
27
|
*/
|
|
27
|
-
export const useAuthState = createStateSourceHook(getAuthState)
|
|
28
|
+
export const useAuthState: () => AuthState = createStateSourceHook(getAuthState)
|
|
@@ -4,7 +4,7 @@ import {createStateSourceHook} from '../helpers/createStateSourceHook'
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Hook to get the currently logged in user
|
|
7
|
-
* @
|
|
7
|
+
* @internal
|
|
8
8
|
* @returns The current user or null if not authenticated
|
|
9
9
|
*/
|
|
10
10
|
export const useAuthToken = createStateSourceHook(getTokenState)
|
|
@@ -1,10 +1,27 @@
|
|
|
1
|
-
import {getCurrentUserState} from '@sanity/sdk'
|
|
1
|
+
import {type CurrentUser, getCurrentUserState} from '@sanity/sdk'
|
|
2
2
|
|
|
3
3
|
import {createStateSourceHook} from '../helpers/createStateSourceHook'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* @TODO This should suspend! And possibly not return `null`?
|
|
7
|
+
*
|
|
7
8
|
* @public
|
|
8
|
-
*
|
|
9
|
+
*
|
|
10
|
+
* The `useCurrentUser` hook returns the currently authenticated user’s profile information (their name, email, roles, etc).
|
|
11
|
+
* If no users are currently logged in, the hook returns null.
|
|
12
|
+
*
|
|
13
|
+
* @returns The current user data, or `null` if not authenticated
|
|
14
|
+
*
|
|
15
|
+
* @example Rendering a basic user profile
|
|
16
|
+
* ```
|
|
17
|
+
* const user = useCurrentUser()
|
|
18
|
+
*
|
|
19
|
+
* return (
|
|
20
|
+
* <figure>
|
|
21
|
+
* <img src={user?.profileImage} alt=`Profile image for ${user?.name}` />
|
|
22
|
+
* <h2>{user?.name}</h2>
|
|
23
|
+
* </figure>
|
|
24
|
+
* )
|
|
25
|
+
* ```
|
|
9
26
|
*/
|
|
10
|
-
export const useCurrentUser = createStateSourceHook(getCurrentUserState)
|
|
27
|
+
export const useCurrentUser: () => CurrentUser | null = createStateSourceHook(getCurrentUserState)
|
|
@@ -10,14 +10,14 @@ import {useCallback, useEffect, useMemo} from 'react'
|
|
|
10
10
|
import {useSanityInstance} from '../context/useSanityInstance'
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* @
|
|
13
|
+
* @internal
|
|
14
14
|
*/
|
|
15
15
|
export type FrameMessageHandler<TWindowMessage extends WindowMessage> = (
|
|
16
16
|
event: TWindowMessage['data'],
|
|
17
17
|
) => TWindowMessage['response'] | Promise<TWindowMessage['response']>
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
* @
|
|
20
|
+
* @internal
|
|
21
21
|
*/
|
|
22
22
|
export interface UseFrameConnectionOptions<TWindowMessage extends WindowMessage> {
|
|
23
23
|
name: string
|
|
@@ -27,7 +27,7 @@ export interface UseFrameConnectionOptions<TWindowMessage extends WindowMessage>
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
|
-
* @
|
|
30
|
+
* @internal
|
|
31
31
|
*/
|
|
32
32
|
export interface FrameConnection<TFrameMessage extends FrameMessage> {
|
|
33
33
|
connect: (frameWindow: Window) => () => void // Return cleanup function
|
|
@@ -39,7 +39,7 @@ export interface FrameConnection<TFrameMessage extends FrameMessage> {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
|
-
* @
|
|
42
|
+
* @internal
|
|
43
43
|
*/
|
|
44
44
|
export function useFrameConnection<
|
|
45
45
|
TFrameMessage extends FrameMessage,
|
|
@@ -4,14 +4,14 @@ import {useCallback, useEffect, useMemo} from 'react'
|
|
|
4
4
|
import {useSanityInstance} from '../context/useSanityInstance'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* @
|
|
7
|
+
* @internal
|
|
8
8
|
*/
|
|
9
9
|
export type WindowMessageHandler<TFrameMessage extends FrameMessage> = (
|
|
10
10
|
event: TFrameMessage['data'],
|
|
11
11
|
) => TFrameMessage['response']
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
* @
|
|
14
|
+
* @internal
|
|
15
15
|
*/
|
|
16
16
|
export interface UseWindowConnectionOptions<TMessage extends FrameMessage> {
|
|
17
17
|
name: string
|
|
@@ -20,7 +20,7 @@ export interface UseWindowConnectionOptions<TMessage extends FrameMessage> {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* @
|
|
23
|
+
* @internal
|
|
24
24
|
*/
|
|
25
25
|
export interface WindowConnection<TMessage extends WindowMessage> {
|
|
26
26
|
sendMessage: <TType extends TMessage['type']>(
|
|
@@ -30,7 +30,7 @@ export interface WindowConnection<TMessage extends WindowMessage> {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
* @
|
|
33
|
+
* @internal
|
|
34
34
|
*/
|
|
35
35
|
export function useWindowConnection<
|
|
36
36
|
TWindowMessage extends WindowMessage,
|
|
@@ -4,10 +4,10 @@ import {useContext} from 'react'
|
|
|
4
4
|
import {SanityInstanceContext} from '../../context/SanityProvider'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* `useSanityInstance` returns the current Sanity instance from the application context.
|
|
8
8
|
* This must be called from within a `SanityProvider` component.
|
|
9
9
|
* @public
|
|
10
|
-
* @returns
|
|
10
|
+
* @returns The current Sanity instance
|
|
11
11
|
* @example
|
|
12
12
|
* ```tsx
|
|
13
13
|
* const instance = useSanityInstance()
|
|
@@ -54,13 +54,14 @@ const STABLE_EMPTY = {
|
|
|
54
54
|
* )
|
|
55
55
|
* ```
|
|
56
56
|
*
|
|
57
|
-
* @example Retrieving all movies released since 1980, sorted by
|
|
57
|
+
* @example Retrieving all movies released since 1980, sorted by director’s last name
|
|
58
58
|
* ```
|
|
59
59
|
* const { results } = useDocuments({
|
|
60
60
|
* filter: '_type == "movie" && releaseDate >= "1980-01-01"',
|
|
61
61
|
* sort: [
|
|
62
62
|
* {
|
|
63
|
-
* field
|
|
63
|
+
* // Expand the `director` reference field with the dereferencing operator `->`
|
|
64
|
+
* field: 'director->lastName',
|
|
64
65
|
* sort: 'asc',
|
|
65
66
|
* },
|
|
66
67
|
* ],
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {type DocumentHandle, getPreviewState, type PreviewValue, resolvePreview} from '@sanity/sdk'
|
|
2
2
|
import {act, render, screen} from '@testing-library/react'
|
|
3
|
-
import {Suspense,
|
|
3
|
+
import {Suspense, useRef} from 'react'
|
|
4
4
|
import {type Mock} from 'vitest'
|
|
5
5
|
|
|
6
6
|
import {usePreview} from './usePreview'
|
|
@@ -44,11 +44,11 @@ const mockDocument: DocumentHandle = {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
function TestComponent({document}: {document: DocumentHandle}) {
|
|
47
|
-
const
|
|
47
|
+
const ref = useRef(null)
|
|
48
48
|
const {results, isPending} = usePreview({document, ref})
|
|
49
49
|
|
|
50
50
|
return (
|
|
51
|
-
<div ref={
|
|
51
|
+
<div ref={ref}>
|
|
52
52
|
<h1>{results?.title}</h1>
|
|
53
53
|
<p>{results?.subtitle}</p>
|
|
54
54
|
{isPending && <div>Pending...</div>}
|
|
@@ -9,7 +9,7 @@ import {useSanityInstance} from '../context/useSanityInstance'
|
|
|
9
9
|
*/
|
|
10
10
|
export interface UsePreviewOptions {
|
|
11
11
|
document: DocumentHandle
|
|
12
|
-
ref
|
|
12
|
+
ref?: React.RefObject<unknown>
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -18,7 +18,7 @@ export interface UsePreviewOptions {
|
|
|
18
18
|
export interface UsePreviewResults {
|
|
19
19
|
/** The results of resolving the document’s preview values */
|
|
20
20
|
results: PreviewValue
|
|
21
|
-
/**
|
|
21
|
+
/** True when preview values are being refreshed */
|
|
22
22
|
isPending: boolean
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -38,8 +38,8 @@ export interface UsePreviewResults {
|
|
|
38
38
|
* // PreviewComponent.jsx
|
|
39
39
|
* export default function PreviewComponent({ document }) {
|
|
40
40
|
* const { results: { title, subtitle, media }, isPending } = usePreview({ document })
|
|
41
|
-
* return
|
|
42
|
-
* <article>
|
|
41
|
+
* return (
|
|
42
|
+
* <article style={{ opacity: isPending ? 0.5 : 1}}>
|
|
43
43
|
* {media?.type === 'image-asset' ? <img src={media.url} alt='' /> : ''}
|
|
44
44
|
* <h2>{title}</h2>
|
|
45
45
|
* <p>{subtitle}</p>
|
|
@@ -55,7 +55,9 @@ export interface UsePreviewResults {
|
|
|
55
55
|
* <ul>
|
|
56
56
|
* {isPending ? 'Loading…' : results.map(movie => (
|
|
57
57
|
* <li key={movie._id}>
|
|
58
|
-
* <
|
|
58
|
+
* <Suspense fallback='Loading…'>
|
|
59
|
+
* <PreviewComponent document={movie} />
|
|
60
|
+
* </Suspense>
|
|
59
61
|
* </li>
|
|
60
62
|
* ))}
|
|
61
63
|
* </ul>
|
|
@@ -76,13 +78,17 @@ export function usePreview({document: {_id, _type}, ref}: UsePreviewOptions): Us
|
|
|
76
78
|
(onStoreChanged: () => void) => {
|
|
77
79
|
const subscription = new Observable<boolean>((observer) => {
|
|
78
80
|
// for environments that don't have an intersection observer
|
|
79
|
-
if (typeof IntersectionObserver === 'undefined')
|
|
81
|
+
if (typeof IntersectionObserver === 'undefined' || typeof HTMLElement === 'undefined') {
|
|
82
|
+
return
|
|
83
|
+
}
|
|
80
84
|
|
|
81
85
|
const intersectionObserver = new IntersectionObserver(
|
|
82
86
|
([entry]) => observer.next(entry.isIntersecting),
|
|
83
87
|
{rootMargin: '0px', threshold: 0},
|
|
84
88
|
)
|
|
85
|
-
if (ref
|
|
89
|
+
if (ref?.current && ref.current instanceof HTMLElement) {
|
|
90
|
+
intersectionObserver.observe(ref.current)
|
|
91
|
+
}
|
|
86
92
|
return () => intersectionObserver.disconnect()
|
|
87
93
|
})
|
|
88
94
|
.pipe(
|