From 7440f6139e0cb9ffe1693818fa7ac1b10b0b3816 Mon Sep 17 00:00:00 2001 From: dusk Date: Mon, 16 Dec 2024 19:32:25 +0300 Subject: [PATCH] fix: find reply root properly --- src/lib/notes.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/lib/notes.ts b/src/lib/notes.ts index e124e74..03d30b0 100644 --- a/src/lib/notes.ts +++ b/src/lib/notes.ts @@ -32,18 +32,19 @@ export const readNote = (id: NoteId): Note => { return JSON.parse(readFileSync(getNotePath(id)).toString()) } export const findReplyRoot = (id: NoteId): {rootNote: Note, rootNoteId: NoteId} => { - let currentNoteId: string | null = id - let currentNote: Note | null = null - while (currentNoteId !== null) { - currentNote = readNote(currentNoteId) - currentNoteId = currentNote.replyTo ?? null + let noteId: string | null = id + let current: {rootNote?: Note, rootNoteId?: NoteId} = {} + while (noteId !== null) { + current.rootNote = readNote(noteId) + current.rootNoteId = noteId + noteId = current.rootNote.replyTo ?? null } - if (currentNote === null || currentNoteId === null) { + if (current.rootNote === undefined || current.rootNoteId === undefined) { throw "no note with id found" } return { - rootNote: currentNote, - rootNoteId: currentNoteId, + rootNote: current.rootNote, + rootNoteId: current.rootNoteId, } } export const writeNote = (id: NoteId, note: Note) => {