Skip to content

Commit

Permalink
Fix restoring revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Nov 20, 2023
1 parent 42f7127 commit fe0f163
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/cli/Serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export async function serve(options: ServeOptions): Promise<void> {

function createBackend(): Handler {
if (process.env.ALINEA_CLOUD_DEBUG)
return createCloudDebugHandler(currentCMS, db)
return createCloudDebugHandler(currentCMS, db, rootDir)
if (process.env.ALINEA_CLOUD_URL)
return createCloudHandler(currentCMS, db, process.env.ALINEA_API_KEY)
return new Handler({
Expand Down
18 changes: 13 additions & 5 deletions src/cloud/server/CloudDebugHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Database, Handler, JWTPreviews, Media, Target} from 'alinea/backend'
import {Drafts} from 'alinea/backend/Drafts'
import {History, Revision} from 'alinea/backend/History'
import {Pending} from 'alinea/backend/Pending'
import {GitHistory} from 'alinea/cli/serve/GitHistory'
import {Config, Connection, Draft, createId} from 'alinea/core'
import {EntryRecord} from 'alinea/core/EntryRecord'
import {Mutation} from 'alinea/core/Mutation'
Expand All @@ -13,8 +14,11 @@ const lag = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
export class DebugCloud implements Media, Target, History, Drafts, Pending {
drafts = new Map<string, Draft>()
pending: Array<Connection.MutateParams & {toCommitHash: string}> = []
history: History

constructor(public config: Config, public db: Database) {}
constructor(public config: Config, public db: Database, rootDir: string) {
this.history = new GitHistory(config, rootDir)
}

async mutate(params: Connection.MutateParams) {
await lag(latency)
Expand Down Expand Up @@ -45,12 +49,12 @@ export class DebugCloud implements Media, Target, History, Drafts, Pending {

async revisions(file: string): Promise<Array<Revision>> {
await lag(latency)
return []
return this.history.revisions(file, undefined!)
}

async revisionData(file: string, revision: string): Promise<EntryRecord> {
await lag(latency)
throw new Error(`Not implemented`)
return this.history.revisionData(file, revision, undefined!)
}

async getDraft(entryId: string): Promise<Draft | undefined> {
Expand Down Expand Up @@ -83,8 +87,12 @@ export class DebugCloud implements Media, Target, History, Drafts, Pending {
}
}

export function createCloudDebugHandler(config: Config, db: Database) {
const api = new DebugCloud(config, db)
export function createCloudDebugHandler(
config: Config,
db: Database,
rootDir: string
) {
const api = new DebugCloud(config, db, rootDir)
return new Handler({
db,
config,
Expand Down
2 changes: 1 addition & 1 deletion src/core/shape/RichTextShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class RichTextShape<Blocks>
applyY(value: TextDoc<Blocks>, parent: Y.Map<any>, key: string): void {
// Sync blocks
const current: Y.Map<any> | undefined = parent.get(key)
if (!current) return void parent.set(key, this.toY(value))
if (!current || !value) return void parent.set(key, this.toY(value))
const blocks = value.filter(
row => this.values?.[row.type] && 'id' in row
) as Array<TextNode.Element<any>>
Expand Down
41 changes: 28 additions & 13 deletions src/dashboard/atoms/EntryEditorAtoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,11 @@ export function createEntryEditor(entryData: EntryData) {

const saveDraft = atom(null, async (get, set) => {
const update = base64.stringify(edits.getLocalUpdate())
const entry = await getDraftEntry({phase: EntryPhase.Published})
// Use the existing path, when the entry gets published the path will change
const entry = await getDraftEntry({
phase: EntryPhase.Published,
path: activeVersion.path
})
const mutation: Mutation = {
type: MutationType.Edit,
previousFile: entryFile(activeVersion),
Expand Down Expand Up @@ -378,7 +382,11 @@ export function createEntryEditor(entryData: EntryData) {
const {edits} = entryData
edits.applyEntryData(type, data)
const update = base64.stringify(edits.getLocalUpdate())
const entry = await getDraftEntry({phase: EntryPhase.Published})
// We're not restoring the previous path because that is unavailable
const entry = await getDraftEntry({
phase: EntryPhase.Published,
path: activeVersion.path
})
const editedFile = entryFile(entry)
const mutation: Mutation = {
type: MutationType.Edit,
Expand Down Expand Up @@ -486,16 +494,25 @@ export function createEntryEditor(entryData: EntryData) {
})
})

type DraftEntryOptions = {
phase?: EntryPhase
path?: string
parentPaths?: Array<string>
locale?: string | null
entryId?: string
parent?: string
}
async function getDraftEntry(
meta: Partial<EntryUrlMeta> & {entryId?: string; parent?: string} = {}
options: DraftEntryOptions = {}
): Promise<EntryRow> {
const data = parseYDoc(type, yDoc)
const locale = meta.locale ?? activeVersion.locale
const path = meta.path ?? data.path
const phase = meta.phase ?? activeVersion.phase
const entryId = meta.entryId ?? activeVersion.entryId
const parent = meta.parent ?? activeVersion.parent
const parentPaths = meta.parentPaths ?? entryData.parents.map(p => p.path)
const phase = options.phase ?? activeVersion.phase
const locale = options.locale ?? activeVersion.locale
const path = options.path ?? data.path ?? activeVersion.path
const entryId = options.entryId ?? activeVersion.entryId
const parent = options.parent ?? activeVersion.parent
const parentPaths =
options.parentPaths ?? entryData.parents.map(p => p.path)
const draftEntry = {
...activeVersion,
...data,
Expand Down Expand Up @@ -581,9 +598,7 @@ export function createEntryEditor(entryData: EntryData) {
})
})

// The debounce here prevents React warning us about a state change during
// render for rich text fields. Some day that should be properly fixed.
const yUpdate = debounceAtom(edits.yUpdate, 10)
const yUpdate = debounceAtom(edits.yUpdate, 250)

const discardEdits = edits.resetChanges
const isLoading = edits.isLoading
Expand All @@ -599,10 +614,10 @@ export function createEntryEditor(entryData: EntryData) {
editMode,
activeVersion,
type,
draftEntry,
yUpdate,
activeTitle,
hasChanges,
draftEntry,
saveDraft,
publishEdits,
restoreRevision,
Expand Down

0 comments on commit fe0f163

Please sign in to comment.