Skip to content

Commit

Permalink
feat: documentStore is now available in StructureContext
Browse files Browse the repository at this point in the history
  • Loading branch information
snorrees authored Oct 27, 2022
1 parent 07c1ef9 commit 3f2b888
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 11 deletions.
42 changes: 39 additions & 3 deletions dev/test-studio/structure/resolveStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
UsersIcon,
} from '@sanity/icons'
import {uuid} from '@sanity/uuid'
import {StructureResolver} from 'sanity/desk'
import {DocumentStore, SanityDocument} from 'sanity'
import {ItemChild, StructureBuilder, StructureResolver} from 'sanity/desk'
import {map} from 'rxjs/operators'
import {Observable, timer} from 'rxjs'
import {DebugPane} from '../components/panes/debug'
import {JsonDocumentDump} from '../components/panes/JsonDocumentDump'
import {_buildTypeGroup} from './_buildTypeGroup'
Expand All @@ -24,7 +27,7 @@ import {
import {delayValue} from './_helpers'
import {typesInOptionGroup} from './groupByOption'

export const structure: StructureResolver = (S, {schema}) => {
export const structure: StructureResolver = (S, {schema, documentStore}) => {
return S.list()
.title('Content')
.items([
Expand All @@ -33,7 +36,17 @@ export const structure: StructureResolver = (S, {schema}) => {
.child(
S.list()
.title('Untitled repro')
.items([S.documentListItem().id('grrm').schemaType('author')])
.items([
S.documentListItem().id('grrm').schemaType('author'),
S.listItem()
.id('documentStore')
.title('Document store')
.child(documentStoreDrivenChild(S, documentStore)),
S.listItem()
.id('randomObservable')
.title('Random observable')
.child(itemTitleChangesEverySecond(S)),
])
),

_buildTypeGroup(S, schema, {
Expand Down Expand Up @@ -358,3 +371,26 @@ export const structure: StructureResolver = (S, {schema}) => {
}),
])
}

function documentStoreDrivenChild(
S: StructureBuilder,
documentStore: DocumentStore
): Observable<ItemChild> {
return documentStore
.listenQuery(
'*[!(_type match "**.**")] | order(_updatedAt desc)[0...10]',
{},
{throttleTime: 1000}
)
.pipe(
map((docs: SanityDocument[]) => {
return S.list()
.title('Some recently edited documents')
.items(docs.map((doc) => S.documentListItem().schemaType(doc._type).id(doc._id)))
})
)
}

function itemTitleChangesEverySecond(S: StructureBuilder) {
return timer(0, 1000).pipe(map(() => S.list().title(`Random title ${Math.random()}`)))
}
11 changes: 8 additions & 3 deletions packages/sanity/src/desk/DeskToolProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {useMemo, useState} from 'react'
import {DeskToolContext} from './DeskToolContext'
import {createStructureBuilder, DefaultDocumentNodeResolver} from './structureBuilder'
import {StructureResolver, UnresolvedPaneNode} from './types'
import {useConfigContextFromSource, useSource} from 'sanity'
import {useConfigContextFromSource, useDocumentStore, useSource} from 'sanity'

/** @internal */
export interface DeskToolProviderProps {
Expand All @@ -20,6 +20,7 @@ export function DeskToolProvider({
const [layoutCollapsed, setLayoutCollapsed] = useState(false)
const source = useSource()
const configContext = useConfigContextFromSource(source)
const documentStore = useDocumentStore()

const S = useMemo(() => {
return createStructureBuilder({
Expand All @@ -30,9 +31,13 @@ export function DeskToolProvider({

const rootPaneNode = useMemo(() => {
// TODO: unify types and remove cast
if (resolveStructure) return resolveStructure(S, configContext) as UnresolvedPaneNode
if (resolveStructure)
return resolveStructure(S, {
...configContext,
documentStore,
}) as UnresolvedPaneNode
return S.defaults() as UnresolvedPaneNode
}, [S, resolveStructure, configContext])
}, [S, resolveStructure, configContext, documentStore])

return (
<DeskToolContext.Provider
Expand Down
2 changes: 2 additions & 0 deletions packages/sanity/src/desk/structureBuilder/ChildResolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {CollectionBuilder, Collection, SerializeOptions} from './StructureNodes'
import {StructureContext} from './types'
import {Observable} from 'rxjs'

/** @beta */
// TODO: unify with the RouterSplitPaneContext
Expand Down Expand Up @@ -28,5 +29,6 @@ export interface ChildResolver {
| ItemChild
| Promise<ItemChild>
| ChildObservable
| Observable<ItemChild>
| undefined
}
11 changes: 8 additions & 3 deletions packages/sanity/src/desk/structureBuilder/ListItem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {camelCase} from 'lodash'
import {SchemaType} from '@sanity/types'
import {Observable} from 'rxjs'
import {SerializeOptions, Serializable, Collection, CollectionBuilder} from './StructureNodes'
import {ChildResolver} from './ChildResolver'
import {ChildResolver, ItemChild} from './ChildResolver'
import {DocumentListBuilder} from './DocumentList'
import {SerializeError, HELP_URL} from './SerializeError'
import {ListBuilder} from './List'
Expand All @@ -11,10 +12,14 @@ import {validateId} from './util/validateId'
import {StructureContext} from './types'

/** @beta */
export type UnserializedListItemChild = Collection | CollectionBuilder | ChildResolver
export type UnserializedListItemChild =
| Collection
| CollectionBuilder
| ChildResolver
| Observable<ItemChild>

/** @beta */
export type ListItemChild = Collection | ChildResolver | undefined
export type ListItemChild = Collection | ChildResolver | Observable<ItemChild> | undefined

/** @beta */
export interface ListItemSerializeOptions extends SerializeOptions {
Expand Down
19 changes: 17 additions & 2 deletions packages/sanity/src/desk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
UserComponent,
View,
} from './structureBuilder'
import {GeneralPreviewLayoutKey, ConfigContext, InitialValueTemplateItem} from 'sanity'
import {
GeneralPreviewLayoutKey,
ConfigContext,
InitialValueTemplateItem,
DocumentStore,
} from 'sanity'

/** @internal */
export interface DeskToolFeatures {
Expand All @@ -30,9 +35,19 @@ export interface DeskToolContextValue {
structureContext: StructureContext
}

/** @public */
export interface StructureResolverContext extends ConfigContext {
/**
* This can be replaced by a different API in the future.
* It is provided as-is to support common structure patterns found in V2 in V3.
* @alpha
* */
documentStore: DocumentStore
}

/** @beta */
// TODO: this should be updated to enforce the correct return type
export type StructureResolver = (S: StructureBuilder, context: ConfigContext) => unknown
export type StructureResolver = (S: StructureBuilder, context: StructureResolverContext) => unknown

/** @internal */
export type DeskToolPaneActionHandler = (params: any, scope?: unknown) => void
Expand Down

0 comments on commit 3f2b888

Please sign in to comment.