Skip to content

Commit

Permalink
fix(structure): add support to force a version for the pane provider (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin authored Jan 3, 2025
1 parent b512378 commit e9a5002
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {type ReleaseId} from '@sanity/client'
import {useMemo} from 'react'
import {
EventsProvider,
Expand All @@ -24,7 +25,8 @@ export const DocumentEventsPane = (props: DocumentPaneProviderProps) => {

const {selectedPerspectiveName} = usePerspective()
const {archivedReleases} = useReleases()
const {rev, since, historyVersion} = params
const {rev, since} = params
const historyVersion = params.historyVersion as ReleaseId | undefined

const documentId = useMemo(() => {
if (
Expand Down Expand Up @@ -69,9 +71,24 @@ export const DocumentEventsPane = (props: DocumentPaneProviderProps) => {

const value = useMemo(() => eventsStore, [eventsStore])

const forcedProviderVersion = useMemo(() => {
if (historyVersion) {
return {
selectedPerspectiveName: historyVersion,
selectedReleaseId: historyVersion,
isReleaseLocked: true,
}
}
return undefined
}, [historyVersion])

return (
<EventsProvider value={value}>
<DocumentPaneProvider {...props} historyStore={historyStoreProps} />
<DocumentPaneProvider
{...props}
historyStore={historyStoreProps}
forcedVersion={forcedProviderVersion}
/>
</EventsProvider>
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable camelcase */
import {type ReleaseId} from '@sanity/client'
import {isActionEnabled} from '@sanity/schema/_internal'
import {useTelemetry} from '@sanity/telemetry/react'
import {
Expand Down Expand Up @@ -81,7 +80,7 @@ interface DocumentPaneProviderProps extends DocumentPaneProviderWrapperProps {
*/
// eslint-disable-next-line complexity, max-statements
export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
const {children, index, pane, paneKey, onFocusPath, matchGlobalVersion = true} = props
const {children, index, pane, paneKey, onFocusPath, forcedVersion} = props
const schema = useSchema()
const templates = useTemplates()
const {setDocumentMeta} = useCopyPaste()
Expand Down Expand Up @@ -116,20 +115,8 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
const perspective = usePerspective()

const {isReleaseLocked, selectedReleaseId, selectedPerspectiveName} = useMemo(() => {
if (!matchGlobalVersion) {
return {
selectedPerspectiveName: undefined,
isReleaseLocked: false,
selectedReleaseId: undefined,
}
}
const historyVersion = params.historyVersion as ReleaseId | undefined
if (historyVersion) {
return {
selectedPerspectiveName: historyVersion,
selectedReleaseId: historyVersion,
isReleaseLocked: true,
}
if (forcedVersion) {
return forcedVersion
}
return {
selectedPerspectiveName: perspective.selectedPerspectiveName,
Expand All @@ -139,8 +126,7 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
: false,
}
}, [
matchGlobalVersion,
params.historyVersion,
forcedVersion,
perspective.selectedPerspectiveName,
perspective.selectedReleaseId,
perspective.selectedPerspective,
Expand Down
12 changes: 9 additions & 3 deletions packages/sanity/src/structure/panes/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {type ReleaseId} from '@sanity/client'

import {type PaneNode} from '../types'

export interface BaseStructureToolPaneProps<T extends PaneNode['type']> {
Expand All @@ -9,8 +11,12 @@ export interface BaseStructureToolPaneProps<T extends PaneNode['type']> {
isActive?: boolean
pane: Extract<PaneNode, {type: T}>
/**
* Decides if the pane should check for the global version when determining the document id.
* default: true
* Allows to override the global version with a specific version or release.
* @beta
*/
matchGlobalVersion?: boolean
forcedVersion?: {
selectedPerspectiveName: ReleaseId | 'published' | undefined
isReleaseLocked: boolean
selectedReleaseId: ReleaseId | undefined
}
}

0 comments on commit e9a5002

Please sign in to comment.