Skip to content

Commit

Permalink
[desk-tool] Improve detection of states in timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
judofyr authored and rexxars committed Oct 6, 2020
1 parent 6862c9b commit 1c02f87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-nested-ternary */
import {Transaction, MendozaPatch, ChunkType, Chunk} from './types'

function didDeleteDraft(type: ChunkType) {
Expand All @@ -14,15 +15,30 @@ function isWithinMergeWindow(a: string, b: string) {
return Date.parse(b) - Date.parse(a) < CHUNK_WINDOW
}

// eslint-disable-next-line complexity
export function mergeChunk(left: Chunk, right: Chunk): Chunk | [Chunk, Chunk] {
if (left.end !== right.start) throw new Error('chunks are not next to each other')

// TODO: How to detect first squash/create

if (didDeleteDraft(left.type) && right.type === 'editDraft') {
if (left.type === 'delete' && right.type === 'editDraft') {
return [left, {...right, type: 'create'}]
}

const draftState = combineState(left.draftState, right.draftState)
const publishedState = combineState(left.publishedState, right.publishedState)

// Convert deletes into either discardDraft or unpublish depending on what's been deleted.
if (right.type === 'delete') {
if (draftState === 'missing' && publishedState === 'present') {
return [left, {...right, type: 'discardDraft'}]
}

if (draftState === 'present' && publishedState === 'missing') {
return [left, {...right, type: 'unpublish'}]
}
}

if (
canMergeEdit(left.type) &&
right.type === 'editDraft' &&
Expand All @@ -40,7 +56,9 @@ export function mergeChunk(left: Chunk, right: Chunk): Chunk | [Chunk, Chunk] {
end: right.end,
startTimestamp: left.startTimestamp,
endTimestamp: right.endTimestamp,
authors
authors,
draftState,
publishedState
}
}

Expand All @@ -56,16 +74,12 @@ export function chunkFromTransaction(transaction: Transaction): Chunk {

let type: ChunkType = 'editDraft'

if (draftDeleted) {
if (publishedDeleted) {
type = 'delete'
} else if (modifedPublished) {
type = 'publish'
} else {
type = 'discardDraft'
}
} else if (publishedDeleted) {
type = 'unpublish'
if (draftDeleted && modifedPublished && !publishedDeleted) {
type = 'publish'
} else if (draftDeleted || publishedDeleted) {
// We don't really know anything more at this point since the actual
// behavior depends on the earlier state.
type = 'delete'
}

return {
Expand All @@ -76,10 +90,19 @@ export function chunkFromTransaction(transaction: Transaction): Chunk {
end: transaction.index + 1,
startTimestamp: transaction.timestamp,
endTimestamp: transaction.timestamp,
authors: new Set([transaction.author])
authors: new Set([transaction.author]),
draftState: modifedDraft ? (draftDeleted ? 'missing' : 'present') : 'unknown',
publishedState: modifedPublished ? (publishedDeleted ? 'missing' : 'present') : 'unknown'
}
}

function combineState(
left: 'present' | 'missing' | 'unknown',
right: 'present' | 'missing' | 'unknown'
) {
return right === 'unknown' ? left : right
}

export function isDeletePatch(patch: MendozaPatch) {
return patch[0] === 0 && patch[1] === null
}
2 changes: 2 additions & 0 deletions packages/@sanity/field/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export type Chunk = {
startTimestamp: string
endTimestamp: string
authors: Set<string>
draftState: 'present' | 'missing' | 'unknown'
publishedState: 'present' | 'missing' | 'unknown'
}

/**
Expand Down

0 comments on commit 1c02f87

Please sign in to comment.