@sanity/sdk-react 0.0.0-alpha.19 → 0.0.0-alpha.20

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.
@@ -9,14 +9,22 @@ import {distinctUntilChanged, EMPTY, Observable, startWith, switchMap} from 'rxj
9
9
 
10
10
  import {useSanityInstance} from '../context/useSanityInstance'
11
11
 
12
- interface UseProjectionOptions {
12
+ /**
13
+ * @public
14
+ * @category Types
15
+ */
16
+ export interface UseProjectionOptions {
13
17
  document: DocumentHandle
14
18
  projection: ValidProjection
15
19
  ref?: React.RefObject<unknown>
16
20
  }
17
21
 
18
- interface UseProjectionResults<TResult extends object> {
19
- results: TResult
22
+ /**
23
+ * @public
24
+ * @category Types
25
+ */
26
+ export interface UseProjectionResults<TResult extends object> {
27
+ data: TResult
20
28
  isPending: boolean
21
29
  }
22
30
 
@@ -32,21 +40,25 @@ interface UseProjectionResults<TResult extends object> {
32
40
  * @param options - The document handle for the document you want to project values from, the projection string, and an optional ref
33
41
  * @returns The projection values for the given document and a boolean to indicate whether the resolution is pending
34
42
  *
35
- * @example Using a projection to display specific document fields
43
+ * @example Using a projection to render a preview of document
36
44
  * ```
37
45
  * // ProjectionComponent.jsx
38
46
  * export default function ProjectionComponent({ document }) {
39
47
  * const ref = useRef(null)
40
- * const { results: { title, description, authors }, isPending } = useProjection({
48
+ * const { results: { title, coverImage, authors }, isPending } = useProjection({
41
49
  * document,
42
- * projection: '{title, "description": pt::text("description"), "authors": array::join(authors[]->name, ", ")}',
43
- * ref
50
+ * ref,
51
+ * projection: `{
52
+ * title,
53
+ * 'coverImage': cover.asset->url,
54
+ * 'authors': array::join(authors[]->{'name': firstName + ' ' + lastName + ' '}.name, ', ')
55
+ * }`,
44
56
  * })
45
57
  *
46
58
  * return (
47
59
  * <article ref={ref} style={{ opacity: isPending ? 0.5 : 1}}>
48
60
  * <h2>{title}</h2>
49
- * <p>{description}</p>
61
+ * <img src={coverImage} alt={title} />
50
62
  * <p>{authors}</p>
51
63
  * </article>
52
64
  * )
@@ -59,13 +71,13 @@ interface UseProjectionResults<TResult extends object> {
59
71
  * const { data } = useInfiniteList({ filter: '_type == "article"' })
60
72
  * return (
61
73
  * <div>
62
- * <h1>Articles</h1>
74
+ * <h1>Books</h1>
63
75
  * <ul>
64
- * {data.map(article => (
65
- * <li key={article._id}>
76
+ * {data.map(book => (
77
+ * <li key={book._id}>
66
78
  * <Suspense fallback='Loading…'>
67
79
  * <ProjectionComponent
68
- * document={article}
80
+ * document={book}
69
81
  * />
70
82
  * </Suspense>
71
83
  * </li>
@@ -126,7 +138,7 @@ export function useProjection<TResult extends object>({
126
138
  // Create getSnapshot function to return current state
127
139
  const getSnapshot = useCallback(() => {
128
140
  const currentState = stateSource.getCurrent()
129
- if (currentState.results === null)
141
+ if (currentState.data === null)
130
142
  throw resolveProjection(instance, {document: {_id, _type}, projection})
131
143
  return currentState as UseProjectionResults<TResult>
132
144
  }, [_id, _type, projection, instance, stateSource])