Skip to content

Commit 3670459

Browse files
authoredMar 4, 2024
feat(core): export useWorkspaceLoader from core (#5898)
1 parent 174a616 commit 3670459

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed
 

‎packages/sanity/src/core/studio/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export * from './StudioLayout'
99
export * from './StudioProvider'
1010
export * from './upsell'
1111
export * from './workspace'
12+
export * from './workspaceLoader'
1213
export * from './workspaces'

‎packages/sanity/src/core/studio/workspaceLoader/ErrorMessage.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ const ListItem = styled(Flex)``
88

99
const ErrorMessageRoot = styled(Box).attrs({padding: 4})``
1010

11+
/**
12+
* @internal
13+
*/
1114
export interface ErrorMessageProps {
1215
message: string
1316
stack?: string
1417
error: unknown
1518
path: Array<{name: string; type: string}>
1619
}
1720

21+
/**
22+
* @internal
23+
*/
1824
export function ErrorMessage({error, message, path, stack}: ErrorMessageProps) {
1925
useEffect(() => {
2026
console.error(error)

‎packages/sanity/src/core/studio/workspaceLoader/WorkspaceLoader.tsx

+22-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import {type ComponentType, type ReactNode, useEffect, useState} from 'react'
33
import {combineLatest, of} from 'rxjs'
44
import {catchError, map} from 'rxjs/operators'
55

6-
import {ConfigResolutionError, type Source, type Workspace} from '../../config'
6+
import {
7+
ConfigResolutionError,
8+
type Source,
9+
type Workspace,
10+
type WorkspaceSummary,
11+
} from '../../config'
712
import {useActiveWorkspace} from '../activeWorkspaceMatcher'
813
import {SourceProvider} from '../source'
914
import {WorkspaceProvider} from '../workspace'
@@ -18,14 +23,13 @@ interface WorkspaceLoaderProps {
1823
LoadingComponent: ComponentType
1924
}
2025

21-
function WorkspaceLoader({
22-
children,
23-
LoadingComponent,
24-
}: Omit<WorkspaceLoaderProps, 'ConfigErrorsComponent'>) {
26+
/**
27+
* @internal
28+
*/
29+
export function useWorkspaceLoader(activeWorkspace: WorkspaceSummary) {
2530
const [error, handleError] = useState<unknown>(null)
2631
if (error) throw error
2732

28-
const {activeWorkspace} = useActiveWorkspace()
2933
const [workspace, setWorkspace] = useState<Workspace | null>(null)
3034

3135
useEffect(() => {
@@ -69,6 +73,15 @@ function WorkspaceLoader({
6973
return () => subscription.unsubscribe()
7074
}, [activeWorkspace])
7175

76+
return workspace
77+
}
78+
79+
function WorkspaceLoader({
80+
children,
81+
LoadingComponent,
82+
}: Omit<WorkspaceLoaderProps, 'ConfigErrorsComponent'>) {
83+
const {activeWorkspace} = useActiveWorkspace()
84+
const workspace = useWorkspaceLoader(activeWorkspace)
7285
if (!workspace) return <LoadingComponent />
7386

7487
// TODO: may need a screen if one of the sources is not logged in. e.g. it
@@ -90,6 +103,9 @@ function WorkspaceLoader({
90103
)
91104
}
92105

106+
/**
107+
* @internal
108+
*/
93109
function WorkspaceLoaderBoundary({ConfigErrorsComponent, ...props}: WorkspaceLoaderProps) {
94110
const [{error}, setError] = useState<{error: unknown}>({error: null})
95111

0 commit comments

Comments
 (0)
Please sign in to comment.