Skip to content

Commit

Permalink
fix(core): update exports definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Oct 23, 2024
1 parent 16ec450 commit 49d773c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 51 deletions.
48 changes: 13 additions & 35 deletions packages/sanity/src/core/store/events/getDocumentEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {

import {getDraftId, getPublishedId, getVersionFromId} from '../../util/draftUtils'
import {type Transaction} from '../_legacy/history/history/types'
import {type DocumentGroupEvent, documentVersionEventTypes} from './types'
import {type DocumentGroupEvent} from './types'

type EffectState = 'unedited' | 'deleted' | 'upsert' | 'created'

Expand Down Expand Up @@ -98,7 +98,7 @@ function isDeletePatch(patch: MendozaPatch): boolean {
* @beta
* This might change, don't use.
* This function receives a transaction and returns a document group event.
* Assumes the user is viewing the published document with only drafts. (Versions are not yet supported here)
* Assumes the user is viewing the published document with only drafts. Versions are not yet supported here
*/
export function getEventFromTransaction(
documentId: string,
Expand Down Expand Up @@ -309,20 +309,9 @@ const mergeEvents = (events: DocumentGroupEvent[]): DocumentGroupEvent[] => {
return result
}

const isDocumentGroupEvent = (event: unknown): event is DocumentGroupEvent => {
const eventType =
typeof event === 'object' && event !== null && 'type' in event && typeof event.type === 'string'
? (event.type as (typeof documentVersionEventTypes)[number])
: ''

return eventType ? documentVersionEventTypes.includes(eventType) : false
}

/**
*
* @internal
* @beta
* This function receives a transaction and returns a transaction with the draft and published effects.
*/
export const addTransactionEffect = (
documentId: string,
Expand All @@ -345,35 +334,24 @@ export const addTransactionEffect = (
* @internal
* @beta
*
* This function receives a list of transactions that can be fetched from CL transactions API with the following query:
* {@link https://www.sanity.io/docs/history-api#45ac5eece4ca}
* excludeContent: 'true',
* includeIdentifiedDocumentsOnly: 'true',
* tag: 'sanity.studio.structure.transactions',
* effectFormat: 'mendoza',
* excludeMutations: 'true',
* reverse: 'true',
* limit: '50'
*
* This function receives a list of transactions that can be fetched from CL transactions API {@link https://www.sanity.io/docs/history-api#45ac5eece4ca}
* It is intended at least now, to support fetching the transactions for the published and draft document and builds the
* document group events from the response.
*/
export function getDocumentEvents(
documentId: string,
transactions: TransactionLogEventWithEffects[],
): DocumentGroupEvent[] {
const events = transactions
.map((transaction, index) => {
// The transactions are ordered from newest to oldest, so we can slice the array from the current index
const previousTransactions = transactions.slice(index + 1)

return getEventFromTransaction(
documentId,
addTransactionEffect(documentId, transaction, index),
previousTransactions.map((tx, i) => addTransactionEffect(documentId, tx, i)),
)
})
.filter(isDocumentGroupEvent)
const events = transactions.map((transaction, index) => {
// The transactions are ordered from newest to oldest, so we can slice the array from the current index
const previousTransactions = transactions.slice(index + 1)

return getEventFromTransaction(
documentId,
addTransactionEffect(documentId, transaction, index),
previousTransactions.map((tx, i) => addTransactionEffect(documentId, tx, i)),
)
})

return mergeEvents(events)
}
19 changes: 3 additions & 16 deletions packages/sanity/src/core/store/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@

/**
* Events relevant for the whole document group.
* @internal
* @beta
**/
export type DocumentGroupEvent =
| CreateDocumentVersionEvent
Expand All @@ -149,24 +151,11 @@ export type DocumentGroupEvent =
| CreateLiveDocumentEvent
| UpdateLiveDocumentEvent

export const documentVersionEventTypes = [
'document.createVersion',
'document.editVersion',
'document.deleteVersion',
'document.publishVersion',
'document.unpublish',
'document.scheduleVersion',
'document.unscheduleVersion',
'document.deleteGroup',
'document.createLive',
'document.updateLive',
] as const

/**
* @internal
* @beta
**/
export type DocumentVersionEventType = (typeof documentVersionEventTypes)[number]
export type DocumentVersionEventType = DocumentGroupEvent['type']

/**
* A generic event with a type and a timestamp.
Expand All @@ -176,15 +165,13 @@ interface BaseEvent {
* The id of the transaction that generated this event, is the same as the `_rev` the documents that were affected by this event will have.
**/
id: string
type: (typeof documentVersionEventTypes)[number]
timestamp: string
// Moved author to baseEvent.
author: string
}

/**
* The critical events related to the state of a release.
* @todo
*/
export type ReleaseEvent = any

Expand Down

0 comments on commit 49d773c

Please sign in to comment.