Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] test(core): use context selector for DocumentPaneProvider #7644

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.

Loading