Skip to content

Commit

Permalink
fix(core): version document's history was returning error (#7438)
Browse files Browse the repository at this point in the history
* fix: draftId correct set in timeline for version docs

* refactor: timeline uses documentId rather than publishedId for agnostic use with versions

* refactor(core): defining versionId rather than draftId

* refactor(core): refactor to better reflect use of version nomenclature in history

* fix(core): resolving tsdoc issue with destructure

* fix(core): resolving tsdoc issue with destructure
  • Loading branch information
jordanl17 authored and juice49 committed Oct 7, 2024
1 parent 7d070e3 commit 04f9fc2
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const getTimelineController = ({
}): TimelineController => {
const timeline = new Timeline({
enableTrace: isDev,
publishedId: documentId,
documentId,
})
return new TimelineController({
client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class Aligner {
constructor(timeline: Timeline) {
this.timeline = timeline
this._states = {
draft: emptyVersionState(timeline.draftId),
draft: emptyVersionState(timeline.versionId),
published: emptyVersionState(timeline.publishedId),
}
}
Expand Down
27 changes: 14 additions & 13 deletions packages/sanity/src/core/store/_legacy/history/history/Timeline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {type Diff} from '@sanity/diff'
import {type TransactionLogEventWithEffects} from '@sanity/types'
import {applyPatch, incremental} from 'mendoza'
import {isVersionId} from 'sanity'

import {type Annotation, type Chunk} from '../../../../field'
import {chunkFromTransaction, mergeChunk} from './chunker'
Expand All @@ -23,7 +24,7 @@ export type ParsedTimeRef = Chunk | 'loading' | 'invalid'
* @hidden
* @beta */
export interface TimelineOptions {
publishedId: string
documentId: string
enableTrace?: boolean
}

Expand All @@ -43,7 +44,7 @@ export class Timeline {
reachedEarliestEntry = false

publishedId: string
draftId: string
versionId: string
private _transactions = new TwoEndedArray<Transaction>()
private _chunks = new TwoEndedArray<Chunk>()

Expand All @@ -58,15 +59,15 @@ export class Timeline {
private _recreateTransactionsFrom?: number
private _trace?: TraceEvent[]

constructor(opts: TimelineOptions) {
this.publishedId = opts.publishedId
this.draftId = `drafts.${opts.publishedId}`
constructor({documentId, enableTrace}: TimelineOptions) {
this.publishedId = documentId
this.versionId = isVersionId(documentId) ? documentId : `drafts.${documentId}`

if (opts.enableTrace) {
if (enableTrace) {
this._trace = []
this._trace.push({
type: 'initial',
publishedId: opts.publishedId,
publishedId: documentId,
})
;(window as any).__sanityTimelineTrace = this._trace
}
Expand Down Expand Up @@ -122,7 +123,7 @@ export class Timeline {
}

if (entry.version === 'draft') {
transaction.draftEffect = entry.effects as any
transaction.versionEffect = entry.effects as any
} else {
transaction.publishedEffect = entry.effects as any
}
Expand All @@ -147,7 +148,7 @@ export class Timeline {
id: event.id,
author: event.author,
timestamp: event.timestamp,
draftEffect: event.effects[this.draftId],
versionEffect: event.effects[this.versionId],
publishedEffect: event.effects[this.publishedId],
})
}
Expand Down Expand Up @@ -330,8 +331,8 @@ export class Timeline {
for (let idx = lastIdx; idx >= firstIdx; idx--) {
const transaction = this._transactions.get(idx)

if (transaction.draftEffect) {
draft = applyPatch(draft, transaction.draftEffect.revert)
if (transaction.versionEffect) {
draft = applyPatch(draft, transaction.versionEffect.revert)
}

if (transaction.publishedEffect) {
Expand Down Expand Up @@ -375,8 +376,8 @@ export class Timeline {
const preDraftValue = draftValue
const prePublishedValue = publishedValue

if (transaction.draftEffect) {
draftValue = incremental.applyPatch(draftValue, transaction.draftEffect.apply, meta)
if (transaction.versionEffect) {
draftValue = incremental.applyPatch(draftValue, transaction.versionEffect.apply, meta)
}

if (transaction.publishedEffect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export class TimelineController {

private async fetchMoreTransactions() {
const publishedId = this.timeline.publishedId
const draftId = this.timeline.draftId
const versionId = this.timeline.versionId
const clientConfig = this.client.config()
const limit = TRANSLOG_ENTRY_LIMIT

Expand All @@ -285,7 +285,7 @@ export class TimelineController {
}

const transactionsUrl = this.client.getUrl(
`/data/history/${clientConfig.dataset}/transactions/${publishedId},${draftId}?${queryParams}`,
`/data/history/${clientConfig.dataset}/transactions/${publishedId},${versionId}?${queryParams}`,
)
const stream = await getJsonStream(transactionsUrl, clientConfig.token)
const reader = stream.getReader()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function getChunkState(effect?: MendozaEffectPair): ChunkState {
* | published upsert | liveEdit | publish | liveEdit |
*/
function getChunkType(transaction: Transaction): ChunkType {
const draftState = getChunkState(transaction.draftEffect)
const draftState = getChunkState(transaction.versionEffect)
const publishedState = getChunkState(transaction.publishedEffect)

if (publishedState === 'unedited') {
Expand Down Expand Up @@ -125,10 +125,10 @@ function getChunkType(transaction: Transaction): ChunkType {
}

export function chunkFromTransaction(transaction: Transaction): Chunk {
const modifiedDraft = Boolean(transaction.draftEffect)
const modifiedDraft = Boolean(transaction.versionEffect)
const modifiedPublished = Boolean(transaction.publishedEffect)

const draftDeleted = transaction.draftEffect && isDeletePatch(transaction.draftEffect.apply)
const draftDeleted = transaction.versionEffect && isDeletePatch(transaction.versionEffect.apply)
const publishedDeleted =
transaction.publishedEffect && isDeletePatch(transaction.publishedEffect.apply)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function replay(events: TraceEvent[]): Timeline {
if (fst?.type !== 'initial') throw new Error('no initial event')

const timeline = new Timeline({
publishedId: fst.publishedId,
documentId: fst.publishedId,
})

/* eslint-disable no-console */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export interface Transaction {
id: string
author: string
timestamp: string
draftEffect?: MendozaEffectPair
versionEffect?: MendozaEffectPair
publishedEffect?: MendozaEffectPair
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
type Chunk,
DRAFTS_FOLDER,
getVersionId,
isVersionId,
remoteSnapshots,
type RemoteSnapshotVersionEvent,
type SelectionState,
Expand Down Expand Up @@ -119,8 +118,7 @@ export function useTimelineStore({
() =>
historyStore.getTimelineController({
client,
documentId:
isVersionId(documentId) && version ? getVersionId(documentId, version) : documentId,
documentId: version ? getVersionId(documentId, version) : documentId,
documentType,
}),
[client, documentId, documentType, historyStore, version],
Expand Down

0 comments on commit 04f9fc2

Please sign in to comment.