Skip to content

Commit f757d58

Browse files
authored
feat: list all studios from user applications list (#62)
1 parent 09736d5 commit f757d58

File tree

4 files changed

+59
-53
lines changed

4 files changed

+59
-53
lines changed

src/versionedClient.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {useClient} from 'sanity'
22

33
export function useVersionedClient() {
4-
return useClient({apiVersion: '2024-06-03'})
4+
return useClient({apiVersion: '2024-08-01'})
55
}

src/widgets/projectInfo/ProjectInfo.tsx

+29-51
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,14 @@ import {useVersionedClient} from '../../versionedClient'
44
import {Subscription} from 'rxjs'
55
import {WidgetContainer} from '../../containers/WidgetContainer'
66
import {DashboardWidgetContainer} from '../../components/DashboardWidgetContainer'
7-
import {DashboardWidget} from '../../types'
8-
9-
export interface ProjectInfoProps {
10-
__experimental_before?: DashboardWidget[]
11-
data: ProjectData[]
12-
}
13-
14-
interface App {
15-
title: string
16-
rows?: App[]
17-
value?: string | {error: string}
18-
}
19-
20-
interface ProjectData {
21-
title: string
22-
category?: string
23-
}
7+
import {type DashboardWidget} from '../../types'
8+
import {type App, type ProjectInfoProps, type ProjectData, UserApplication} from './types'
249

2510
function isUrl(url?: string) {
2611
return url && /^https?:\/\//.test(`${url}`)
2712
}
2813

29-
function getGraphQlUrl(projectId: string, dataset: string) {
14+
function getGraphQLUrl(projectId: string, dataset: string) {
3015
return `https://${projectId}.api.sanity.io/v1/graphql/${dataset}/default`
3116
}
3217

@@ -43,8 +28,8 @@ const NO_DATA: ProjectData[] = []
4328

4429
export function ProjectInfo(props: ProjectInfoProps) {
4530
const {__experimental_before = NO_EXPERIMENTAL, data = NO_DATA} = props
46-
const [studioHost, setStudioHost] = useState<string | {error: string} | undefined>()
47-
const [graphqlApi, setGraphQlApi] = useState<string | {error: string} | undefined>()
31+
const [studioApps, setStudioApps] = useState<UserApplication[] | {error: string} | undefined>()
32+
const [graphQLApi, setGraphQLApi] = useState<string | {error: string} | undefined>()
4833
const versionedClient = useVersionedClient()
4934
const {projectId = 'unknown', dataset = 'unknown'} = versionedClient.config()
5035

@@ -53,25 +38,13 @@ export function ProjectInfo(props: ProjectInfoProps) {
5338

5439
subscriptions.push(
5540
versionedClient.observable
56-
.request<{
57-
studioHost: string
58-
metadata?: {externalStudioHost?: string}
59-
}>({uri: `/projects/${projectId}`, tag: 'dashboard.project-info.studio-host'})
41+
.request<UserApplication[]>({uri: '/user-applications', tag: 'dashboard.project-info'})
6042
.subscribe({
61-
next: (result) => {
62-
if (result.metadata?.externalStudioHost) {
63-
setStudioHost(result.metadata.externalStudioHost)
64-
return
65-
}
66-
67-
setStudioHost(
68-
result.studioHost ? `https://${result.studioHost}.sanity.studio` : undefined,
69-
)
70-
},
43+
next: (result) => setStudioApps(result.filter((app) => app.type === 'studio')),
7144
error: (error) => {
72-
console.error('Error while looking for studioHost', error)
73-
setStudioHost({
74-
error: 'Something went wrong while looking up studioHost. See console.',
45+
console.error('Error while resolving user applications', error)
46+
setStudioApps({
47+
error: 'Something went wrong while resolving user applications. See console.',
7548
})
7649
},
7750
}),
@@ -86,14 +59,14 @@ export function ProjectInfo(props: ProjectInfoProps) {
8659
tag: 'dashboard.project-info.graphql-api',
8760
})
8861
.subscribe({
89-
next: () => setGraphQlApi(getGraphQlUrl(projectId, dataset)),
62+
next: () => setGraphQLApi(getGraphQLUrl(projectId, dataset)),
9063
error: (error) => {
9164
if (error.statusCode === 404) {
92-
setGraphQlApi(undefined)
65+
setGraphQLApi(undefined)
9366
} else {
94-
console.error('Error while looking for graphqlApi', error)
95-
setGraphQlApi({
96-
error: 'Something went wrong while looking up graphqlApi. See console.',
67+
console.error('Error while looking for graphQLApi', error)
68+
setGraphQLApi({
69+
error: 'Something went wrong while looking up graphQLApi. See console.',
9770
})
9871
}
9972
},
@@ -103,7 +76,7 @@ export function ProjectInfo(props: ProjectInfoProps) {
10376
return () => {
10477
subscriptions.forEach((s) => s.unsubscribe())
10578
}
106-
}, [dataset, projectId, versionedClient, setGraphQlApi, setStudioHost])
79+
}, [dataset, projectId, versionedClient, setGraphQLApi])
10780

10881
const assembleTableRows = useMemo(() => {
10982
let result: App[] = [
@@ -116,11 +89,16 @@ export function ProjectInfo(props: ProjectInfoProps) {
11689
},
11790
]
11891

119-
// Handle any apps
120-
const apps: App[] = [
121-
studioHost ? {title: 'Studio', value: studioHost} : null,
122-
...data.filter((item) => item.category === 'apps'),
123-
].filter((a): a is App => !!a)
92+
const apps: App[] = data.filter((item) => item.category === 'apps')
93+
94+
// Handle studios
95+
;(Array.isArray(studioApps) ? studioApps : []).forEach((app) => {
96+
apps.push({
97+
title: app.title || 'Studio',
98+
value: app.urlType === 'internal' ? `https://${app.appHost}.sanity.studio` : app.appHost,
99+
})
100+
})
101+
124102
if (apps.length > 0) {
125103
result = result.concat([{title: 'Apps', rows: apps}])
126104
}
@@ -134,7 +112,7 @@ export function ProjectInfo(props: ProjectInfoProps) {
134112
{title: 'GROQ', value: getGroqUrl(projectId, dataset)},
135113
{
136114
title: 'GraphQL',
137-
value: (typeof graphqlApi === 'object' ? 'Error' : graphqlApi) ?? 'Not deployed',
115+
value: (typeof graphQLApi === 'object' ? 'Error' : graphQLApi) ?? 'Not deployed',
138116
},
139117
],
140118
},
@@ -157,7 +135,7 @@ export function ProjectInfo(props: ProjectInfoProps) {
157135
})
158136

159137
return result
160-
}, [graphqlApi, studioHost, projectId, dataset, data])
138+
}, [graphQLApi, studioApps, projectId, dataset, data])
161139

162140
return (
163141
<>
@@ -207,7 +185,7 @@ export function ProjectInfo(props: ProjectInfoProps) {
207185
<Stack space={4} paddingX={3} role="rowgroup">
208186
{item.rows.map((row) => {
209187
return (
210-
<Grid key={row.title} columns={2} role="row">
188+
<Grid key={`${row.value}-${row.title}`} columns={2} role="row">
211189
<Text weight="medium" role="rowheader">
212190
{row.title}
213191
</Text>

src/widgets/projectInfo/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ProjectInfo} from './ProjectInfo'
2-
import {LayoutConfig, DashboardWidget} from '../../types'
2+
import {type LayoutConfig, type DashboardWidget} from '../../types'
33

44
export function projectInfoWidget(config?: {layout?: LayoutConfig}): DashboardWidget {
55
return {

src/widgets/projectInfo/types.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {type DashboardWidget} from '../../types'
2+
3+
export interface ProjectInfoProps {
4+
__experimental_before?: DashboardWidget[]
5+
data: ProjectData[]
6+
}
7+
8+
export interface App {
9+
title: string
10+
rows?: App[]
11+
value?: string | {error: string}
12+
}
13+
14+
export interface ProjectData {
15+
title: string
16+
category?: string
17+
}
18+
19+
export interface UserApplication {
20+
id: string
21+
projectId: string
22+
title: string | null
23+
type: string
24+
urlType: 'internal' | 'external'
25+
appHost: string
26+
27+
// … there are other props here, but we don't really care about them for our use case
28+
}

0 commit comments

Comments
 (0)