Skip to content

Commit 2cfe3cd

Browse files
colepetersbinoy14
andauthored
fix: fix generation & content of reference URLs (#49)
* fix: fix generation & content of reference URLs * fix: correct URL TLD Co-authored-by: Binoy Patel <[email protected]> --------- Co-authored-by: Binoy Patel <[email protected]>
1 parent 2c3ec47 commit 2cfe3cd

File tree

7 files changed

+24
-53
lines changed

7 files changed

+24
-53
lines changed

apps/sdk-explorer/src/Home.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {Box, Heading, Stack, Text} from '@sanity/ui'
22
import {type JSX} from 'react'
33

44
import ExampleCard from './components/ExampleCard'
5-
import {documents, paginatedDocuments, projection, query} from './components/Hooks'
65
import DocumentTableImage from './images/DocumentTable.svg'
76
import DocumentSearchImage from './images/DoumentSearch.svg'
87
import MoviesByActorImage from './images/MoviesByActor.svg'
@@ -48,31 +47,31 @@ export default function Home(): JSX.Element {
4847
to="/document-collections/document-search"
4948
title="Document search"
5049
description="An interface for generating dynamic document searches"
51-
hooks={[documents, projection]}
50+
hooks={['useDocuments', 'useProjection']}
5251
styling="Sanity UI"
5352
img={DocumentSearchImage}
5453
/>
5554
<ExampleCard
5655
to="/document-collections/document-table"
5756
title="Document table"
5857
description="A tabular rendering of documents and their content"
59-
hooks={[paginatedDocuments, projection]}
58+
hooks={['usePaginatedDocuments', 'useProjection']}
6059
styling="Tailwind"
6160
img={DocumentTableImage}
6261
/>
6362
<ExampleCard
6463
to="/document-collections/preview-grid"
6564
title="Preview grid"
6665
description="A grid of document previews"
67-
hooks={[documents, projection]}
66+
hooks={['useDocuments', 'useProjection']}
6867
styling="Tailwind"
6968
img={PreviewGridImage}
7069
/>
7170
<ExampleCard
7271
to="/document-collections/preview-list"
7372
title="Preview list"
7473
description="A list of document previews"
75-
hooks={[documents, projection]}
74+
hooks={['useDocuments', 'useProjection']}
7675
styling="Sanity UI"
7776
img={PreviewListImage}
7877
/>
@@ -91,7 +90,7 @@ export default function Home(): JSX.Element {
9190
to="/groq/movies-by-actor"
9291
title="Movies by actor"
9392
description="Use multiple useQuery hooks to fetch and filter data"
94-
hooks={[query]}
93+
hooks={['useQuery']}
9594
styling="Sanity UI"
9695
img={MoviesByActorImage}
9796
/>

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

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,34 @@
11
import {Badge, Box, Inline} from '@sanity/ui'
22
import {type JSX} from 'react'
33

4-
const baseUrl = 'https://sdk-docs.sanity.dev/functions/React_SDK.exports.'
4+
const baseUrl = 'https://reference.sanity.io/_sanity/sdk-react/exports/'
55

6-
const referenceLinks = {
7-
useDocuments: `${baseUrl}useDocuments.html`,
8-
usePaginatedDocuments: `${baseUrl}usePaginatedDocuments.html`,
9-
useProjection: `${baseUrl}useProjection.html`,
10-
useQuery: `${baseUrl}useQuery.html`,
11-
}
12-
13-
interface HookReference {
14-
name: 'useDocuments' | 'usePaginatedDocuments' | 'useProjection' | 'useQuery'
15-
reference: (typeof referenceLinks)[keyof typeof referenceLinks]
16-
}
6+
// Just update this array with a hook name to add a ReferenceLink for it
7+
const hookNames = ['useDocuments', 'usePaginatedDocuments', 'useProjection', 'useQuery'] as const
178

18-
// These consts can't begin with the word `use` or eslint yells at you for
19-
// passing around a 'hook' reference
20-
21-
const documents: HookReference = {
22-
name: 'useDocuments',
23-
reference: referenceLinks.useDocuments,
24-
}
25-
26-
const paginatedDocuments: HookReference = {
27-
name: 'usePaginatedDocuments',
28-
reference: referenceLinks.usePaginatedDocuments,
29-
}
30-
31-
const projection: HookReference = {
32-
name: 'useProjection',
33-
reference: referenceLinks.useProjection,
34-
}
9+
type HookName = (typeof hookNames)[number]
3510

36-
const query: HookReference = {
37-
name: 'useQuery',
38-
reference: referenceLinks.useQuery,
11+
type ReferenceLink = {
12+
[key in HookName]: string
3913
}
4014

41-
export {documents, paginatedDocuments, projection, query}
15+
const referenceLinks = hookNames.reduce(
16+
(allHooks, current) => ({...allHooks, [current]: `${baseUrl}${current}`}),
17+
{} as ReferenceLink,
18+
)
4219

4320
export interface HooksProps {
44-
hooks: HookReference[]
21+
hooks: HookName[]
4522
}
4623

4724
export default function Hooks({hooks}: HooksProps): JSX.Element {
4825
return (
4926
<Inline space={3}>
5027
{hooks.map((hook) => (
51-
<Badge key={hook.name} tone="primary">
52-
<a href={hook.reference} style={{display: 'inline-block'}}>
28+
<Badge key={hook} tone="primary">
29+
<a href={referenceLinks[hook]} style={{display: 'inline-block'}}>
5330
<Box as="span" padding={1}>
54-
{hook.name}
31+
{hook}
5532
</Box>
5633
</a>
5734
</Badge>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {Card, Flex, Label, Stack, Text, TextInput} from '@sanity/ui'
33
import {type JSX, Suspense, useState} from 'react'
44

55
import ExampleLayout from '../../../components/ExampleLayout'
6-
import {documents, projection} from '../../../components/Hooks'
76

87
interface Projection {
98
title: string
@@ -53,7 +52,7 @@ export default function DocumentSearch(): JSX.Element {
5352
return (
5453
<ExampleLayout
5554
title="Document search"
56-
hooks={[documents, projection]}
55+
hooks={['useDocuments', 'useProjection']}
5756
codeUrl="https://github.com/sanity-io/sdk-examples/blob/main/apps/sdk-explorer/src/examples/document-collections/DocumentSearch/DocumentSearch.tsx"
5857
styling="Sanity UI"
5958
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {DocumentHandle, usePaginatedDocuments, useProjection} from '@sanity/sdk-
44
import {type JSX, useRef} from 'react'
55

66
import ExampleLayout from '../../../components/ExampleLayout'
7-
import {paginatedDocuments, projection} from '../../../components/Hooks'
87

98
interface MovieProjectionResults {
109
data: {
@@ -64,7 +63,7 @@ export default function DocumentTable(): JSX.Element {
6463
<ExampleLayout
6564
title="Document table"
6665
codeUrl="https://github.com/sanity-io/sdk-examples/blob/main/apps/sdk-explorer/src/examples/document-collections/DocumentTable/DocumentTable.tsx"
67-
hooks={[paginatedDocuments, projection]}
66+
hooks={['usePaginatedDocuments', 'useProjection']}
6867
styling="Tailwind"
6968
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."
7069
>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {Spinner} from '@sanity/ui'
66
import {type JSX, Suspense, useRef} from 'react'
77

88
import ExampleLayout from '../../../components/ExampleLayout'
9-
import {documents, projection} from '../../../components/Hooks'
109

1110
// @todo replace with type from SDK
1211
interface ProjectionResults {
@@ -78,7 +77,7 @@ function PreviewGrid(): JSX.Element {
7877
<ExampleLayout
7978
title="Preview grid"
8079
codeUrl="https://github.com/sanity-io/sdk-examples/blob/main/apps/sdk-explorer/src/examples/document-collections/PreviewGrid/PreviewGrid.tsx"
81-
hooks={[documents, projection]}
80+
hooks={['useDocuments', 'useProjection']}
8281
styling="Tailwind"
8382
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."
8483
>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {Button, Card, Flex, Inline, Spinner, Stack, Text} from '@sanity/ui'
44
import {type JSX, Suspense, useRef} from 'react'
55

66
import ExampleLayout from '../../../components/ExampleLayout'
7-
import {documents, projection} from '../../../components/Hooks'
87

98
function Loading() {
109
return (
@@ -79,7 +78,7 @@ function PreviewList(): JSX.Element {
7978
<ExampleLayout
8079
title="Preview list"
8180
codeUrl="https://github.com/sanity-io/sdk-examples/blob/main/apps/sdk-explorer/src/examples/document-collections/PreviewList/PreviewList.tsx"
82-
hooks={[documents, projection]}
81+
hooks={['useDocuments', 'useProjection']}
8382
styling="Sanity UI"
8483
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."
8584
>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {Avatar, Card, Inline, Label, Select, Stack, Text} from '@sanity/ui'
33
import {type JSX, useState} from 'react'
44

55
import ExampleLayout from '../../../components/ExampleLayout'
6-
import {query} from '../../../components/Hooks'
76

87
interface CastMember {
98
_id: string
@@ -52,7 +51,7 @@ export default function MoviesByActor(): JSX.Element {
5251
<ExampleLayout
5352
title="Movies by actor"
5453
codeUrl="https://github.com/sanity-io/sdk-examples/blob/main/apps/sdk-explorer/src/examples/groq/MoviesByActor/MoviesByActor.tsx"
55-
hooks={[query]}
54+
hooks={['useQuery']}
5655
styling="Sanity UI"
5756
summary="This example uses two instances of the useQuery hook. The first executes a GROQ query to look for entries of type ‘person’ in our dataset and filters those entries down to those who are referenced in at least 2 movie entries’ ‘castMembers’ field. For each of those results, we return a projection that includes the person’s name, photo, and document ID. The second useQuery hook executes a GROQ query for the movies the selected person has starred in, and returns the title, poster image, release date, and document ID for each."
5857
>

0 commit comments

Comments
 (0)