Skip to content

Commit

Permalink
feat(sanity): move diffView into sanity
Browse files Browse the repository at this point in the history
In my opinion, this functionality was better organised when it was
isolated as a plugin. However, due to dependencies on
`sanity/structure`, it was necessary to move it into that part of the
codebase.

Some things are now more heavy-handed, such as the way the
"Compare Versions" document action is inserted.
  • Loading branch information
juice49 committed Jan 9, 2025
1 parent 413c6f4 commit 2cc93c7
Show file tree
Hide file tree
Showing 29 changed files with 123 additions and 205 deletions.
10 changes: 1 addition & 9 deletions packages/sanity/src/core/config/resolveDefaultPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {comments} from '../comments/plugin'
import {createIntegration} from '../create/createIntegrationPlugin'
import {diffView} from '../diffView/plugin'
import {releases, RELEASES_NAME} from '../releases/plugin'
import {DEFAULT_SCHEDULED_PUBLISH_PLUGIN_OPTIONS} from '../scheduledPublishing/constants'
import {SCHEDULED_PUBLISHING_NAME, scheduledPublishing} from '../scheduledPublishing/plugin'
Expand All @@ -12,14 +11,7 @@ import {
type WorkspaceOptions,
} from './types'

const defaultPlugins = [
comments(),
tasks(),
scheduledPublishing(),
createIntegration(),
releases(),
diffView(),
]
const defaultPlugins = [comments(), tasks(), scheduledPublishing(), createIntegration(), releases()]

export function getDefaultPlugins(
options: DefaultPluginsWorkspaceOptions,
Expand Down
27 changes: 0 additions & 27 deletions packages/sanity/src/core/diffView/i18n/index.ts

This file was deleted.

22 changes: 0 additions & 22 deletions packages/sanity/src/core/diffView/i18n/resources.ts

This file was deleted.

This file was deleted.

33 changes: 0 additions & 33 deletions packages/sanity/src/core/diffView/plugin/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/sanity/src/core/releases/hooks/usePerspective.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export function usePerspective({
| ReleaseId
| undefined

// TODO: When `perspectiveOverride` is set, exclusion should not have an effect.
const excludedPerspectives = useMemo(
() =>
excludedPerspectivesOverride ??
Expand Down
2 changes: 0 additions & 2 deletions packages/sanity/src/core/releases/i18n/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const releasesLocaleStrings = {
'action.archive.tooltip': 'Unschedule this release to archive it',
/** Action text for showing the archived releases */
'action.archived': 'Archived',
/** Action text for comparing document versions */
'action.compare-versions': 'Compare versions',
/** Action text for reverting a release by creating a new release */
'action.create-revert-release': 'Stage in new release',
/** Action text for deleting a release */
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {type DocumentActionComponent} from '../../../config/document/actions'
import {type DocumentActionsContext} from '../../../config/types'
import {CompareVersionsAction} from './CompareVersionsAction'
import {DiscardVersionAction} from './DiscardVersionAction'
import {UnpublishVersionAction} from './UnpublishVersionAction'

Expand All @@ -15,6 +14,5 @@ export default function resolveDocumentActions(
...(context.versionType === 'version'
? duplicateAction.concat(DiscardVersionAction).concat(UnpublishVersionAction)
: existingActions),
CompareVersionsAction,
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {type ComponentType, useState} from 'react'
import {type DocumentLayoutProps} from 'sanity'

import {Dialog} from '../../../ui-components/dialog/Dialog'
import {type DocumentLayoutProps} from '../../config/types'
import {useCreatePathSyncChannel} from '../hooks/useCreatePathSyncChannel'
import {useDiffViewRouter} from '../hooks/useDiffViewRouter'
import {useDiffViewState} from '../hooks/useDiffViewState'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import {BoundaryElementProvider, Box, Card, DialogProvider, PortalProvider} from '@sanity/ui'
import {noop} from 'lodash'
import {type CSSProperties, forwardRef, useRef, useState} from 'react'
import {
ChangeIndicatorsTracker,
getPublishedId,
getVersionFromId,
isDraftId,
useEditState,
VirtualizerScrollInstanceProvider,
} from 'sanity'
import {ConnectorContext} from 'sanity/_singletons'
import {DocumentPaneProvider, FormViewComponent} from 'sanity/structure'
import {styled} from 'styled-components'

import {ChangeIndicatorsTracker} from '../../changeIndicators/tracker'
import {VirtualizerScrollInstanceProvider} from '../../form/inputs/arrays/ArrayOfObjectsInput/List/VirtualizerScrollInstanceProvider'
import {useEditState} from '../../hooks/useEditState'
import {getPublishedId, getVersionFromId, isDraftId} from '../../util/draftUtils'
import {DocumentPaneProvider, FormViewComponent} from '../..'
import {type PathSyncChannel} from '../types/pathSyncChannel'
import {PathSyncChannelSubscriber} from './PathSyncChannelSubscriber'
import {Scroller} from './Scroller'
Expand All @@ -33,7 +37,7 @@ interface DiffViewPaneProps {
// TODO: Switch off comments. Document inspectors cannot currently be shown inside the diff view.
// TODO: Switch off references pane. It should be a hyperlink instead.
export const DiffViewPane = forwardRef<HTMLDivElement, DiffViewPaneProps>(function DiffViewPane(
{role, documentType, documentId, scrollElement, syncChannel, /*compareValue,*/ compareDocument},
{role, documentType, documentId, scrollElement, syncChannel, compareDocument},
ref,
) {
const paneId = ['diffView', role].join('.')
Expand Down Expand Up @@ -103,6 +107,7 @@ export const DiffViewPane = forwardRef<HTMLDivElement, DiffViewPaneProps>(functi
paneKey={paneId}
itemId={paneId}
perspectiveOverride={version}
excludedPerspectivesOverride={[]}
pane={{
id: paneId,
type: 'document',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type NavigateDiffView = (
>,
) => void

interface DiffViewRouter {
export interface DiffViewRouter {
navigateDiffView: NavigateDiffView
exitDiffView: () => void
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ import {type SanityClient} from '@sanity/client'
import {useEffect, useMemo} from 'react'
import {useObservable} from 'react-rx'
import {EMPTY, map, type Observable} from 'rxjs'
import {useRouter} from 'sanity/router'

import {useClient} from '../../hooks/useClient'
import {usePerspective} from '../../releases/hooks/usePerspective'
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../../studioClient'
import {
DEFAULT_STUDIO_CLIENT_OPTIONS,
getDraftId,
getPublishedId,
getVersionId,
isSystemBundle,
isVersionId,
type PublishedId,
} from '../../util/draftUtils'
useClient,
usePerspective,
} from 'sanity'
import {useRouter} from 'sanity/router'

import {
DIFF_SEARCH_PARAM_SEPERATOR,
DIFF_VIEW_NEXT_DOCUMENT_SEARCH_PARAMETER,
Expand Down Expand Up @@ -62,9 +61,10 @@ export function useDiffViewState(): DiffViewState {
const {perspectiveStack} = usePerspective()
const client = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS)

// ensure not `""`
// ensure not `null`
// ensure types are in schema
// TODO: Validate parameters.
// - Ensure not `""`.
// - Ensure not `null`.
// - Ensure types are present in schema.
const [previousDocumentType, previousDocumentId] = (
searchParams.get(DIFF_VIEW_PREVIOUS_DOCUMENT_SEARCH_PARAMETER) ?? ''
).split(DIFF_SEARCH_PARAM_SEPERATOR)
Expand All @@ -77,16 +77,15 @@ export function useDiffViewState(): DiffViewState {
// will be inferred by finding the next existing document in the perspective stack.
const shouldInferPreviousDocument =
diffViewMode === 'version' &&
searchParams.get(DIFF_VIEW_PREVIOUS_DOCUMENT_SEARCH_PARAMETER) === null &&
isVersionId(nextDocumentId)
searchParams.get(DIFF_VIEW_PREVIOUS_DOCUMENT_SEARCH_PARAMETER) === null

const inferPreviousDocument = useMemo(() => {
if (!shouldInferPreviousDocument) {
return EMPTY
}

return getFirstExistentVersion({
bundles: perspectiveStack.slice(1),
return findPrecedingVersion({
bundles: perspectiveStack,
publishedId: getPublishedId(nextDocumentId),
client,
})
Expand All @@ -99,6 +98,8 @@ export function useDiffViewState(): DiffViewState {
return
}

// TODO: Navigating like this once the previous document has been inferred causes a redundant
// browser history entry to be created, resulting in an unusual experience when navigating back.
navigateDiffView({
mode: 'version',
previousDocument: {
Expand Down Expand Up @@ -144,12 +145,12 @@ export function useDiffViewState(): DiffViewState {
}

/**
* Find the first existing document id in the provided array of bundles. This can be used to find
* the id of the document that immediately precedes another when release layering is applied.
* Find the first existing document id that precedes the first entry in the provided array of
* bundles.
*
* This function implicitly includes draft and published documents.
*/
function getFirstExistentVersion({
function findPrecedingVersion({
bundles,
publishedId,
client,
Expand All @@ -161,6 +162,7 @@ function getFirstExistentVersion({
const ids = bundles
.flatMap((bundle) => (isSystemBundle(bundle) ? [] : getVersionId(publishedId, bundle)))
.concat(getDraftId(publishedId), publishedId)
.slice(1)

return client.observable
.fetch<string | undefined>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {useCallback, useEffect, useMemo} from 'react'
import deepEquals from 'react-fast-compare'
import {useObservable} from 'react-rx'
import {distinctUntilChanged, filter, map} from 'rxjs'
import {useDocumentPane} from 'sanity/structure'

import {deepEquals} from '../../validation/util/deepEquals'
import {type PathSyncChannelProps, type PathSyncState} from '../types/pathSyncChannel'

/**
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/structure/diffView/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './hooks/useDiffViewRouter'
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {type ComponentType, type PropsWithChildren} from 'react'
import {type DocumentLayoutProps} from 'sanity'

import {DiffView} from '../components/DiffView'
import {useDiffViewState} from '../hooks/useDiffViewState'

export const DiffViewDocumentLayout: ComponentType<
PropsWithChildren<Pick<DocumentLayoutProps, 'documentId' | 'documentType'>>
> = ({children, documentId}) => {
const {state} = useDiffViewState()

return (
<>
{children}
{state === 'ready' && <DiffView documentId={documentId} />}
</>
)
}
Loading

0 comments on commit 2cc93c7

Please sign in to comment.