Skip to content

Commit

Permalink
test(core): use context selector for DocumentPaneProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
binoy14 committed Oct 21, 2024
1 parent 816067e commit aba2ae5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 35 deletions.
1 change: 1 addition & 0 deletions packages/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@
"speakingurl": "^14.0.1",
"tar-fs": "^2.1.1",
"tar-stream": "^3.1.7",
"use-context-selector": "^2.0.0",
"use-device-pixel-ratio": "^1.1.0",
"use-hot-module-reload": "^2.0.0",
"use-sync-external-store": "^1.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {createContext} from 'sanity/_createContext'
// import {createContext} from 'sanity/_createContext'

import {createContext} from 'use-context-selector'

import type {DocumentPaneContextValue} from '../../structure/panes/document/DocumentPaneContext'

/** @internal */
export const DocumentPaneContext = createContext<DocumentPaneContextValue | null>(
'sanity/_singletons/context/document-pane',
// 'sanity/_singletons/context/document-pane',
null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,38 @@ const StyledChangeConnectorRoot = styled(ChangeConnectorRoot)`
`

export function DocumentLayout() {
const {
changesOpen,
documentId,
documentType,
fieldActions,
focusPath,
inspectOpen,
inspector,
inspectors,
onFocus,
onHistoryOpen,
onMenuAction,
onPathOpen,
paneKey,
schemaType,
value,
} = useDocumentPane()
// const {
// changesOpen,
// documentId,
// documentType,
// fieldActions,
// focusPath,
// inspectOpen,
// inspector,
// inspectors,
// onFocus,
// onHistoryOpen,
// onMenuAction,
// onPathOpen,
// paneKey,
// schemaType,
// value,
// } = useDocumentPane()
const changesOpen = useDocumentPane((state) => state.changesOpen)
const inspectOpen = useDocumentPane((state) => state.inspectOpen)
const documentId = useDocumentPane((state) => state.documentId)
const documentType = useDocumentPane((state) => state.documentType)
const fieldActions = useDocumentPane((state) => state.fieldActions)
const focusPath = useDocumentPane((state) => state.focusPath)
const inspector = useDocumentPane((state) => state.inspector)
const inspectors = useDocumentPane((state) => state.inspectors)
const onFocus = useDocumentPane((state) => state.onFocus)
const onHistoryOpen = useDocumentPane((state) => state.onHistoryOpen)
const onMenuAction = useDocumentPane((state) => state.onMenuAction)
const onPathOpen = useDocumentPane((state) => state.onPathOpen)
const paneKey = useDocumentPane((state) => state.paneKey)
const schemaType = useDocumentPane((state) => state.schemaType)
const value = useDocumentPane((state) => state.value)

const {features} = useStructureTool()
const {t} = useTranslation(structureLocaleNamespace)
Expand Down
16 changes: 9 additions & 7 deletions packages/sanity/src/structure/panes/document/useDocumentPane.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import {useContext} from 'react'
import {DocumentPaneContext} from 'sanity/_singletons'
import {type Context, useContextSelector} from 'use-context-selector'

import {type DocumentPaneContextValue} from './DocumentPaneContext'

/** @internal */
export function useDocumentPane(): DocumentPaneContextValue {
const documentPane = useContext(DocumentPaneContext)

if (!documentPane) {
throw new Error('DocumentPane: missing context value')
}
export function useDocumentPane<T = DocumentPaneContextValue>(
selector?: (value: DocumentPaneContextValue) => T,
): T {
const selectorFn = selector || ((value: DocumentPaneContextValue) => value as T)
const documentPane = useContextSelector<DocumentPaneContextValue, T>(
DocumentPaneContext as Context<DocumentPaneContextValue>,
selectorFn,
)

return documentPane
}
32 changes: 23 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aba2ae5

Please sign in to comment.