Skip to content

Commit 8975397

Browse files
authored
feat: update to App SDK 1.0.0 (#50)
1 parent 2cfe3cd commit 8975397

File tree

10 files changed

+114
-79
lines changed

10 files changed

+114
-79
lines changed

apps/sdk-explorer/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"@sanity/color": "^3.0.6",
1515
"@sanity/icons": "^3.5.7",
1616
"@sanity/logos": "^2.1.13",
17-
"@sanity/sdk": "0.0.0-alpha.29",
18-
"@sanity/sdk-react": "0.0.0-alpha.29",
17+
"@sanity/sdk": "^1.0.0",
18+
"@sanity/sdk-react": "^1.0.0",
1919
"@sanity/types": "^3.72.1",
2020
"@sanity/ui": "^2.11.4",
2121
"@tailwindcss/vite": "^4.0.0",

apps/sdk-explorer/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default function App(): JSX.Element {
5151
<a href="/">Home</a>
5252
</Text>
5353
<Text weight="medium" size={1}>
54-
<a href="https://sdk-docs.sanity.dev">SDK Docs</a>
54+
<a href="https://sanity.io/docs/app-sdk">App SDK Docs</a>
5555
</Text>
5656
<Text weight="medium" size={1}>
5757
<a href="https://github.com/sanity-io/sdk">GitHub</a>

apps/sdk-explorer/src/Home.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ export default function Home(): JSX.Element {
2525
<Stack space={5} style={{maxInlineSize: '64ch'}}>
2626
<Text size={2} muted>
2727
The Sanity App SDK Explorer contains an assortment of example interfaces built with our
28-
React SDK’s hooks. The purpose of the Explorer is to demonstrate how these hooks can be
29-
used to build out interfaces powered by Sanity, with a variety of approaches to styling.
28+
React App SDK’s hooks. The purpose of the Explorer is to demonstrate how these hooks can
29+
be used to build out interfaces powered by Sanity, with a variety of approaches to
30+
styling.
3031
</Text>
3132
<Text size={2} muted>
3233
Each example contains an interface rendered in the browser as well as a link to the
@@ -47,31 +48,31 @@ export default function Home(): JSX.Element {
4748
to="/document-collections/document-search"
4849
title="Document search"
4950
description="An interface for generating dynamic document searches"
50-
hooks={['useDocuments', 'useProjection']}
51+
hooks={['useDocuments', 'useDocumentProjection']}
5152
styling="Sanity UI"
5253
img={DocumentSearchImage}
5354
/>
5455
<ExampleCard
5556
to="/document-collections/document-table"
5657
title="Document table"
5758
description="A tabular rendering of documents and their content"
58-
hooks={['usePaginatedDocuments', 'useProjection']}
59+
hooks={['usePaginatedDocuments', 'useDocumentProjection']}
5960
styling="Tailwind"
6061
img={DocumentTableImage}
6162
/>
6263
<ExampleCard
6364
to="/document-collections/preview-grid"
6465
title="Preview grid"
6566
description="A grid of document previews"
66-
hooks={['useDocuments', 'useProjection']}
67+
hooks={['useDocuments', 'useDocumentProjection']}
6768
styling="Tailwind"
6869
img={PreviewGridImage}
6970
/>
7071
<ExampleCard
7172
to="/document-collections/preview-list"
7273
title="Preview list"
7374
description="A list of document previews"
74-
hooks={['useDocuments', 'useProjection']}
75+
hooks={['useDocuments', 'useDocumentProjection']}
7576
styling="Sanity UI"
7677
img={PreviewListImage}
7778
/>

apps/sdk-explorer/src/components/Hooks.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import {type JSX} from 'react'
44
const baseUrl = 'https://reference.sanity.io/_sanity/sdk-react/exports/'
55

66
// Just update this array with a hook name to add a ReferenceLink for it
7-
const hookNames = ['useDocuments', 'usePaginatedDocuments', 'useProjection', 'useQuery'] as const
7+
const hookNames = [
8+
'useDocuments',
9+
'usePaginatedDocuments',
10+
'useDocumentProjection',
11+
'useQuery',
12+
] as const
813

914
type HookName = (typeof hookNames)[number]
1015

apps/sdk-explorer/src/examples/document-collections/DocumentSearch/DocumentSearch.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {DocumentHandle, useDocuments, useProjection} from '@sanity/sdk-react'
1+
import {type DocumentHandle, useDocumentProjection, useDocuments} from '@sanity/sdk-react'
22
import {Card, Flex, Label, Stack, Text, TextInput} from '@sanity/ui'
33
import {type JSX, Suspense, useState} from 'react'
44

@@ -12,10 +12,10 @@ interface Projection {
1212

1313
// Component for rendering an item from the `useDocuments` result
1414
function DocumentResult({documentHandle}: {documentHandle: DocumentHandle}) {
15-
// Use the `useProjection` hook to get the document’s title, overview text, and poster image URL
15+
// Use the `useDocumentProjection` hook to get the document’s title, overview text, and poster image URL
1616
const {
1717
data: {title, overviewText, imageUrl},
18-
} = useProjection<Projection>({
18+
} = useDocumentProjection<Projection>({
1919
...documentHandle,
2020
projection: `{
2121
title,
@@ -44,15 +44,15 @@ export default function DocumentSearch(): JSX.Element {
4444
const [search, setSearch] = useState('')
4545

4646
const {data: movies} = useDocuments({
47-
filter: '_type == "movie"',
47+
documentType: 'movie',
4848
// Pass the `search` state variable to the `useDocuments` hook’s `search` parameter
4949
search,
5050
})
5151

5252
return (
5353
<ExampleLayout
5454
title="Document search"
55-
hooks={['useDocuments', 'useProjection']}
55+
hooks={['useDocuments', 'useDocumentProjection']}
5656
codeUrl="https://github.com/sanity-io/sdk-examples/blob/main/apps/sdk-explorer/src/examples/document-collections/DocumentSearch/DocumentSearch.tsx"
5757
styling="Sanity UI"
5858
summary="This example passes a state variable to the useDocuments hook’s ‘search’ argument, enabling the creation of a dynamic search interface for documents in the targeted dataset(s). (Note: the ‘search’ parameter currently searches for matches across all of a document’s string fields.)"

apps/sdk-explorer/src/examples/document-collections/DocumentTable/DocumentTable.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import './styles.css'
22

3-
import {DocumentHandle, usePaginatedDocuments, useProjection} from '@sanity/sdk-react'
3+
import {type DocumentHandle, useDocumentProjection, usePaginatedDocuments} from '@sanity/sdk-react'
44
import {type JSX, useRef} from 'react'
55

66
import ExampleLayout from '../../../components/ExampleLayout'
@@ -20,7 +20,7 @@ function DocumentRow({document}: {document: DocumentHandle}) {
2020

2121
const {
2222
data: {title, posterImage, cast, popularity, releaseDate},
23-
}: MovieProjectionResults = useProjection({
23+
}: MovieProjectionResults = useDocumentProjection({
2424
...document,
2525
ref,
2626
// In our projection, we will:
@@ -54,7 +54,7 @@ function DocumentRow({document}: {document: DocumentHandle}) {
5454
export default function DocumentTable(): JSX.Element {
5555
const {data, currentPage, totalPages, nextPage, previousPage, hasNextPage, hasPreviousPage} =
5656
usePaginatedDocuments({
57-
filter: '_type == "movie"',
57+
documentType: 'movie',
5858
pageSize: 6,
5959
orderings: [{field: 'releaseDate', direction: 'desc'}],
6060
})
@@ -63,9 +63,9 @@ export default function DocumentTable(): JSX.Element {
6363
<ExampleLayout
6464
title="Document table"
6565
codeUrl="https://github.com/sanity-io/sdk-examples/blob/main/apps/sdk-explorer/src/examples/document-collections/DocumentTable/DocumentTable.tsx"
66-
hooks={['usePaginatedDocuments', 'useProjection']}
66+
hooks={['usePaginatedDocuments', 'useDocumentProjection']}
6767
styling="Tailwind"
68-
summary="This example uses the usePaginatedDocuments hook to retrieve a paginated collection of documents with six items per page, in addition to state and functions to control the pagination. The useProjection hook is used to retrieve contents and create projections from each document. Each document and its content and projections are then rendered in a table row."
68+
summary="This example uses the usePaginatedDocuments hook to retrieve a paginated collection of documents with six items per page, in addition to state and functions to control the pagination. The useDocumentProjection hook is used to retrieve contents and create projections from each document. Each document and its content and projections are then rendered in a table row."
6969
>
7070
<div className="overflow-x-scroll">
7171
<table className="w-full text-sm border-collapse">

apps/sdk-explorer/src/examples/document-collections/PreviewGrid/PreviewGrid.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import './styles.css'
22

3-
import {DocumentHandle} from '@sanity/sdk'
4-
import {useDocuments, useProjection} from '@sanity/sdk-react'
3+
import {type DocumentHandle, useDocumentProjection, useDocuments} from '@sanity/sdk-react'
54
import {Spinner} from '@sanity/ui'
65
import {type JSX, Suspense, useRef} from 'react'
76

@@ -19,14 +18,14 @@ interface ProjectionResults {
1918
// The DocumentPreview component uses the `usePreview` hook to render a document preview interface
2019
function DocumentPreview({document}: {document: DocumentHandle}): JSX.Element {
2120
// Generate a ref for the outer element
22-
// This keeps the useProjection hook from resolving if the preview is not currently displayed in the viewport
21+
// This keeps the useDocumentProjection hook from resolving if the preview is not currently displayed in the viewport
2322
const ref = useRef(null)
2423

2524
// Project the title, first 2 cast members, and poster image values for the document,
2625
// plus an `isPending` flag to indicate if projection value resolutions are pending
2726
const {
2827
data: {title, cast, posterImage},
29-
}: ProjectionResults = useProjection({
28+
}: ProjectionResults = useDocumentProjection({
3029
...document,
3130
ref,
3231
projection: `{
@@ -69,17 +68,17 @@ function PreviewGrid(): JSX.Element {
6968
// Use the `useDocuments` hook to return an index of document handles for all of our 'movie' type documents
7069
// Sort the documents by the release date
7170
const {data: movies} = useDocuments({
72-
filter: '_type == "movie"',
71+
documentType: 'movie',
7372
orderings: [{field: 'releaseDate', direction: 'desc'}],
7473
})
7574

7675
return (
7776
<ExampleLayout
7877
title="Preview grid"
7978
codeUrl="https://github.com/sanity-io/sdk-examples/blob/main/apps/sdk-explorer/src/examples/document-collections/PreviewGrid/PreviewGrid.tsx"
80-
hooks={['useDocuments', 'useProjection']}
79+
hooks={['useDocuments', 'useDocumentProjection']}
8180
styling="Tailwind"
82-
summary="This example uses the useDocuments hook to retrieve a collection of documents. That collection is then mapped over, with each document passed to a component that uses the useProjection hook to retrieve each document’s title and poster image, and to create a projection of the first three listed cast members. The results are displayed in a grid."
81+
summary="This example uses the useDocuments hook to retrieve a collection of documents. That collection is then mapped over, with each document passed to a component that uses the useDocumentProjection hook to retrieve each document’s title and poster image, and to create a projection of the first three listed cast members. The results are displayed in a grid."
8382
>
8483
<div className="grid grid-cols-1 gap-x-4 gap-y-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
8584
{movies.map((movie) => (

apps/sdk-explorer/src/examples/document-collections/PreviewList/PreviewList.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {DocumentHandle} from '@sanity/sdk'
2-
import {useDocuments, useProjection} from '@sanity/sdk-react'
1+
import {type DocumentHandle, useDocumentProjection, useDocuments} from '@sanity/sdk-react'
32
import {Button, Card, Flex, Inline, Spinner, Stack, Text} from '@sanity/ui'
43
import {type JSX, Suspense, useRef} from 'react'
54

@@ -27,14 +26,14 @@ interface ProjectionResults {
2726
// The DocumentPreview component uses the `usePreview` hook to render a document preview interface
2827
function DocumentPreview({document}: {document: DocumentHandle}) {
2928
// Generate a ref for the outer element
30-
// This keeps the useProjection hook from firing if the preview is not currently displayed in the viewport
29+
// This keeps the useDocumentProjection hook from firing if the preview is not currently displayed in the viewport
3130
const ref = useRef(null)
3231

3332
// Project the title, first 3 cast mambers, and post image values for the document,
3433
// plus an `isPending` flag to indicate if projection value resolutions are pending
3534
const {
3635
data: {title, cast, posterImage},
37-
}: ProjectionResults = useProjection({
36+
}: ProjectionResults = useDocumentProjection({
3837
...document,
3938
ref,
4039
projection: `{
@@ -70,17 +69,17 @@ function PreviewList(): JSX.Element {
7069
// Use the `useDocuments` hook to return an index of document handles for all of our 'movie' type documents
7170
// Sort the documents by the the release date
7271
const {data: movies} = useDocuments({
73-
filter: '_type == "movie"',
72+
documentType: 'movie',
7473
orderings: [{field: 'releaseDate', direction: 'desc'}],
7574
})
7675

7776
return (
7877
<ExampleLayout
7978
title="Preview list"
8079
codeUrl="https://github.com/sanity-io/sdk-examples/blob/main/apps/sdk-explorer/src/examples/document-collections/PreviewList/PreviewList.tsx"
81-
hooks={['useDocuments', 'useProjection']}
80+
hooks={['useDocuments', 'useDocumentProjection']}
8281
styling="Sanity UI"
83-
summary="This example uses the useDocuments hook to retrieve a collection of documents. That collection is then mapped over, with each document passed to a component that uses the useProjection hook to retrieve each document’s title and poster image, and to create a projection of the first three listed cast members."
82+
summary="This example uses the useDocuments hook to retrieve a collection of documents. That collection is then mapped over, with each document passed to a component that uses the useDocumentProjection hook to retrieve each document’s title and poster image, and to create a projection of the first three listed cast members."
8483
>
8584
<Stack style={{overflowY: 'scroll'}}>
8685
{movies.map((movie) => (

apps/sdk-explorer/src/examples/groq/MoviesByActor/MoviesByActor.tsx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,33 @@ interface Movie {
1919

2020
export default function MoviesByActor(): JSX.Element {
2121
// Get a list of people who are referenced as cast members in more than 1 movie
22-
const {data: castMembers} = useQuery<CastMember[]>(
23-
`*[_type == "person" && count(*[_type == "movie" && ^._id in castMembers[].person._ref]) > 1] {
24-
_id,
25-
name,
26-
'photo': image.asset->url
27-
}`,
28-
)
22+
const castMembersQuery = `*[_type == "person" && count(*[_type == "movie" && ^._id in castMembers[].person._ref]) > 1] {
23+
_id,
24+
name,
25+
'photo': image.asset->url
26+
}`
27+
28+
const {data: castMembers} = useQuery<CastMember[]>({query: castMembersQuery})
2929

3030
// Create a state variable to store the selected cast member's ID
3131
// Initialize it with the ID of the first cast member
3232
const [castMemberId, setCastMemberId] = useState<string>(() => castMembers?.[0]?._id)
3333

3434
// Construct another query for movies that the cast member has appeared in
3535
// by passing the cast member's ID as a parameter to the query
36-
const {data: castMemberMovies} = useQuery<Movie[]>(
37-
`*[_type == "movie" && $castMemberId in castMembers[].person._ref]{
38-
_id,
39-
title,
40-
'posterImage': poster.asset->url,
41-
releaseDate,
42-
}`,
43-
{
44-
params: {
45-
castMemberId: castMemberId,
46-
},
36+
const castMembersMoviesQuery = `*[_type == "movie" && $castMemberId in castMembers[].person._ref]{
37+
_id,
38+
title,
39+
'posterImage': poster.asset->url,
40+
releaseDate,
41+
}`
42+
43+
const {data: castMemberMovies} = useQuery<Movie[]>({
44+
query: castMembersMoviesQuery,
45+
params: {
46+
castMemberId: castMemberId,
4747
},
48-
)
48+
})
4949

5050
return (
5151
<ExampleLayout

0 commit comments

Comments
 (0)