diff --git a/src/models/page.model.ts b/src/models/page.model.ts index c639207b..66a88566 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -161,7 +161,7 @@ const getLocalPages = (input: { noteId: 0, draftKey: key .replace(`draft-${input.characterId}-`, "") - .replace(`draft-${input.handle}-`, ""), // In order to be compatible with old drafts + .replace(`draft-${input.handle}-${input.characterId}-`, ""), // In order to be compatible with old drafts linkItemType: null, linkKey: "", toCharacterId: null, @@ -270,19 +270,21 @@ export async function getPagesBySite(input: { local.forEach((localPage) => { const index = expandedNotes.list.findIndex( - (page) => localPage.draftKey === page.noteId + "", + (page) => localPage.draftKey === (page.noteId || page.draftKey) + "", ) - if ( - index !== -1 && - new Date(localPage.updatedAt) > + if (index !== -1) { + if ( + new Date(localPage.updatedAt) > new Date(expandedNotes.list[index].updatedAt) - ) { - expandedNotes.list[index] = { - ...expandedNotes.list[index], - metadata: { - content: localPage.metadata?.content, - }, - local: true, + ) { + expandedNotes.list[index] = { + ...expandedNotes.list[index], + metadata: { + content: localPage.metadata?.content, + }, + local: true, + draftKey: localPage.draftKey, + } } } else { expandedNotes.list.push(localPage) @@ -378,10 +380,12 @@ export async function getPage(input: { characterId: input.characterId, handle: input.handle, }) - const localPage = local.find( - (page) => - page.draftKey === input.noteId + "" || page.draftKey === input.slug, - ) + const localPages = local.filter((page) => page.draftKey === input.noteId + "") + const localPage = + localPages.length && + localPages.reduce((prev, current) => { + return prev.updatedAt > current.updatedAt ? prev : current + }) let expandedNote: ExpandedNote | undefined if (page) {