Wael Nasreddine 7c7797d35a fix(share/prev_next): Prevent crashing if candide page is null
When a note is not visible, attempting to export it ends up crashing the
server with this error:

```
TypeError: ejs:193
    191|
    192|                 <% if (hasTree) { %>
 >> 193|                     <%- include("prev_next", { note: note, subRoot: subRoot }) %>
    194|                 <% } %>
    195|             </footer>
    196|         </div>
ejs:1
 >> 1| <%
    2|     // TODO: code cleanup + putting this behind a toggle/attribute
    3|     const previousNote = (() => {
    4|         // If we are at the subRoot, there is no previous
Cannot read properties of undefined (reading 'hasVisibleChildren')
    at eval (eval at compile (/usr/src/app/main.cjs:553:203), <anonymous>:27:26)
    at eval (eval at compile (/usr/src/app/main.cjs:553:203), <anonymous>:34:7)
    at d (/usr/src/app/main.cjs:557:265)
    at g (/usr/src/app/main.cjs:557:251)
    at eval (eval at compile (/usr/src/app/main.cjs:553:203), <anonymous>:293:17)
    at d (/usr/src/app/main.cjs:557:265)
    at as.render (/usr/src/app/main.cjs:532:458)
    at Omr (/usr/src/app/main.cjs:581:109552)
    at Rmr (/usr/src/app/main.cjs:581:107637)
    at $W.prepareContent (/usr/src/app/main.cjs:653:28) {
  path: ''
```

fixes #8002
fixes #8162
2025-12-25 16:11:01 -08:00

57 lines
2.3 KiB
Plaintext

<%
// TODO: code cleanup + putting this behind a toggle/attribute
const previousNote = (() => {
// If we are at the subRoot, there is no previous
if (note.noteId === subRoot.note.noteId) return null;
const parent = note.getParentNotes()[0];
const children = parent.getVisibleChildNotes();
const index = children.findIndex(n => n.noteId === note.noteId);
// If we are the first child, previous goes up a level
// this is already protected by the first if statement
if (index === 0) return parent;
// We are not the first child at this level so previous
// should go to the end of the previous tree
let candidate = children[index - 1];
while (candidate?.hasVisibleChildren()) {
const children = candidate.getVisibleChildNotes();
candidate = children[children.length - 1];
}
return candidate ?? null;
})();
const nextNote = (() => {
// If this currently active note has children, next
// should be the first child
if (note.hasVisibleChildren()) return note.getVisibleChildNotes()[0];
let parent = note.getParentNotes()[0];
let children = parent.getVisibleChildNotes();
let index = children.findIndex(n => n.noteId === note.noteId);
// If we are not the last of the current level, just go
// to the next sibling note
if (index !== children.length - 1) return children[index + 1];
// We are the last sibling, we need to find the next ancestral note
while (index === children.length - 1) {
// If we are already at subRoot level, no reason trying to go higher
if (parent.noteId === subRoot.note.noteId) return null;
const originalParent = parent;
parent = parent.getParentNotes()[0];
children = parent.getVisibleChildNotes();
index = children.findIndex(n => n.noteId === originalParent.noteId);
}
return children[index + 1];
})();
%>
<div class="navigation">
<% if (previousNote) { %><a class="previous" href="./<%- previousNote.shareId %>"><%- previousNote.title %></a><% } %>
<% if (nextNote) { %><a class="next" href="./<%- nextNote.shareId %>"><%- nextNote.title %></a><% } %>
</div>