Skip to content

Commit b7bfb45

Browse files
authored
Add SDK Explorer readme and code comments (#6)
* Add SDK Explorer readme and code comments * Dupe readme text to SDK Explorer home page
1 parent c5037e7 commit b7bfb45

File tree

5 files changed

+56
-49
lines changed

5 files changed

+56
-49
lines changed

sdk-explorer/README.md

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,21 @@
1-
# React + TypeScript + Vite
1+
# SDK Explorer
22

3-
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
3+
The Sanity App SDK Explorer contains an assortment of example interfaces built with our React SDK’s hooks.
4+
The purpose of the Explorer is to demonstrate how these hooks can be used to build out interfaces powered
5+
by Sanity, with a variety of approaches to styling.
46

5-
Currently, two official plugins are available:
7+
Each example contains an interface rendered in the browser as well as a link to the example’s code on
8+
GitHub to demonstrate how the example is built. Example code is enriched with comments and may freely
9+
be used in your own applications.
610

7-
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
11+
## Running the SDK Explorer
912

10-
## Expanding the ESLint configuration
13+
We'll have a deployed version of the SDK Explorer available soon.
1114

12-
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
15+
For now, you can run the SDK Explorer locally:
1316

14-
- Configure the top-level `parserOptions` property like this:
15-
16-
```js
17-
export default tseslint.config({
18-
languageOptions: {
19-
// other options...
20-
parserOptions: {
21-
project: ['./tsconfig.node.json', './tsconfig.app.json'],
22-
tsconfigRootDir: import.meta.dirname,
23-
},
24-
},
25-
})
26-
```
27-
28-
- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
29-
- Optionally add `...tseslint.configs.stylisticTypeChecked`
30-
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:
31-
32-
```js
33-
// eslint.config.js
34-
import react from 'eslint-plugin-react'
35-
36-
export default tseslint.config({
37-
// Set the react version
38-
settings: { react: { version: '18.3' } },
39-
plugins: {
40-
// Add the react plugin
41-
react,
42-
},
43-
rules: {
44-
// other rules...
45-
// Enable its recommended rules
46-
...react.configs.recommended.rules,
47-
...react.configs['jsx-runtime'].rules,
48-
},
49-
})
50-
```
17+
1. Clone the SDK Examples Git repo: `git clone [email protected]:sanity-io/sdk-examples.git`
18+
1. Switch to the SDK Explorer app: `cd ./sdk-examples/sdk-explorer`
19+
1. Install dependencies: `pnpm install`
20+
1. Run the application’s dev server: `pnpm dev`
21+
1. Open the URL displayed by the dev server (usually `localhost:5173`)

sdk-explorer/src/Home.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ export default function Home() {
1515
>
1616
SDK Explorer
1717
</Heading>
18-
<Text size={3} muted>
19-
The Sanity SDK Explorer contains an assortment of examples for each
20-
component available in the SDK. This copywriting should be more
21-
exciting!
22-
</Text>
18+
<Stack space={5}>
19+
<Text size={3} muted>
20+
The Sanity App SDK Explorer contains an assortment of example
21+
interfaces built with our React SDK’s hooks. The purpose of the
22+
Explorer is to demonstrate how these hooks can be used to build out
23+
interfaces powered by Sanity, with a variety of approaches to
24+
styling.
25+
</Text>
26+
<Text size={3} muted>
27+
Each example contains an interface rendered in the browser as well
28+
as a link to the example’s code on GitHub to demonstrate how the
29+
example is built. Example code is enriched with comments and may
30+
freely be used in your own applications.
31+
</Text>
32+
</Stack>
2333
</Stack>
2434

2535
<Stack space={5} paddingY={4}>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@ import { Suspense, useRef } from 'react'
55
import ExampleLayout from '../../ExampleLayout'
66
import './styles.css'
77

8+
// The DocumentPreview component uses the `usePreview` hook to render a document preview interface
89
function DocumentPreview({ document }: { document: DocumentHandle }) {
10+
// Generate a ref for the outer element
11+
// This keeps the usePreview hook from resolving if the preview is not currently displayed in the viewport
912
const ref = useRef(null)
13+
14+
// Get the preview's title, subtitle, and media fields,
15+
// plus an `isPending` flag to indicate if preview value resolutions are pending
1016
const {
1117
results: { title, subtitle, media },
1218
isPending,
1319
} = usePreview({ document, ref })
1420

1521
return (
1622
<button
23+
// Assign the ref to the outer element
1724
ref={ref}
25+
// When preview values are resolving, we’ll lower the opacity to indicate this state visually
1826
className={`group appearance-none text-start p-3 rounded-md border-1 border-gray-100 shadow-xl ${isPending ? 'opacity-50' : 'opacity-100'}`}
1927
onClick={() => alert(`Great pick! ${title} is an excellent book.`)}
2028
>
@@ -31,6 +39,8 @@ function DocumentPreview({ document }: { document: DocumentHandle }) {
3139
}
3240

3341
function PreviewGrid() {
42+
// Use the `useDocuments` hook to return an index of document handles for all of our 'book' type documents
43+
// Sort the documents by the author's last name, then the release date
3444
const { results: books, isPending } = useDocuments({
3545
filter: '_type == "book"',
3646
sort: [

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,26 @@ function Loading() {
1414
)
1515
}
1616

17+
// The DocumentPreview component uses the `usePreview` hook to render a document preview interface
1718
function DocumentPreview({ document }: { document: DocumentHandle }) {
19+
// Generate a ref for the outer element
20+
// This keeps the usePreview hook from resolving if the preview is not currently displayed in the viewport
1821
const ref = useRef(null)
22+
23+
// Get the preview's title, subtitle, and media fields,
24+
// plus an `isPending` flag to indicate if preview value resolutions are pending
1925
const {
2026
results: { title, subtitle, media },
2127
isPending,
2228
} = usePreview({ document, ref })
2329

2430
return (
2531
<Button
32+
// Assign the ref to the outer element
2633
ref={ref}
2734
mode='bleed'
2835
onClick={() => alert(`Good choice! ${title} is an excellent book.`)}
36+
// When preview values are resolving, we’ll lower the opacity to indicate this state visually
2937
style={{ opacity: isPending ? 0.5 : 1 }}
3038
>
3139
<Inline space={4}>
@@ -46,6 +54,8 @@ function DocumentPreview({ document }: { document: DocumentHandle }) {
4654
}
4755

4856
function PreviewList() {
57+
// Use the `useDocuments` hook to return an index of document handles for all of our 'book' type documents
58+
// Sort the documents by the author's last name, then the release date
4959
const { results: books, isPending } = useDocuments({
5060
filter: '_type == "book"',
5161
sort: [

sdk-explorer/src/users/UserProfile/UserProfile.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { useAuthState, useCurrentUser } from '@sanity/sdk-react/hooks'
33
import { Avatar, Card, Container, Flex, Grid, Stack, Text } from '@sanity/ui'
44

55
export default function UserProfile() {
6+
// Use the `useAuthState` hook to ensure there's currently a logged in user
67
const { type } = useAuthState()
8+
9+
// Use the `useCurrentUser` hook to get the current user.
10+
// The returned object contains fields like the user's name, profile image, email address, roles, and authentication provider
711
const user = useCurrentUser()
812

13+
// Render a user profile if the authentication state indicated a logged in user and a user object is available
914
return type === AuthStateType.LOGGED_IN && user ? (
1015
<Container width={1}>
1116
<Card padding={4} margin={2} radius={3} shadow={3}>
@@ -36,6 +41,7 @@ export default function UserProfile() {
3641
</Card>
3742
</Container>
3843
) : (
44+
// Render a placeholder if there's no user currently logged in
3945
<Container padding={5}>
4046
<Text as='h2' size={4} align='center'>
4147
Nobody home! Try logging in?

0 commit comments

Comments
 (0)