Skip to content

Commit

Permalink
Fix chapter does not exist crashes
Browse files Browse the repository at this point in the history
If the chapter being referenced doesn't exist, this crashed or spun the wheel in several places
  • Loading branch information
davidmoore1 committed Jan 17, 2025
1 parent fb4f798 commit af2b1de
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/BookSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ The navbar component.
return first;
}
function getVerseCount(chapter, chapters) {
if (!chapter || chapter === 'i' || !chapters || Object.keys(chapters).length === 0) {
if (!chapter || chapter === 'i' || !chapters || Object.keys(chapters).length === 0 || !chapters[chapter]) {
return 0;
}
let count = Object.keys(chapters[chapter]).length;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/ChapterSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ The navbar component.
}
let books = $refs.catalog.documents;
let chapters = books.find((d) => d.bookCode === book)?.versesByChapters;
if (!chapters || Object.keys(chapters).length === 0) {
if (!chapters || Object.keys(chapters).length === 0 || !chapters[chapter]) {
return 0;
}
let count = Object.keys(chapters[chapter]).length;
Expand Down
16 changes: 15 additions & 1 deletion src/lib/components/ScriptureViewSofria.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2424,7 +2424,9 @@ LOGGING:
if (chapterCount(currentBook) === 0) {
cl.renderDocument({ docId, config: {}, output });
} else {
cl.renderDocument({ docId, config: { chapters: [chapter] }, output });
if (chapterExists(chapter, chapters)) {
cl.renderDocument({ docId, config: { chapters: [chapter] }, output });
}
}
performance.mark('cl-render-end');
performance.measure('cl-render-duration', 'cl-render-start', 'cl-render-end');
Expand Down Expand Up @@ -2457,6 +2459,15 @@ LOGGING:
);
return illustrations;
}
function chapterExists(chapter, chapters) {
var value = true;
if (chapter !== 'i') {
if (!chapter || !chapters[chapter]) {
value = false;
}
}
return value;
}
$: fontSize = bodyFontSize + 'px';
$: lineHeight = bodyLineHeight + '%';
Expand All @@ -2474,6 +2485,9 @@ LOGGING:
$: versePerLine = verseLayout === 'one-per-line';
/**list of books in current docSet*/
$: books = $refs.catalog.documents;
/**list of chapters in current book*/
$: chapters = books.find((d) => d.bookCode === currentBook)?.versesByChapters ?? [];
$: chapterValid = chapterExists(currentChapter, chapters);
$: direction = config.bookCollections.find((x) => x.id === references.collection).style
.textDirection;
$: verseRangeSeparator = config.bookCollections.find((x) => x.id === references.collection)
Expand Down
6 changes: 6 additions & 0 deletions src/lib/data/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type { AudioPlayer } from './audio';
import type { HistoryItem } from './history';

export function getBook(item: { collection?: string; book: string }) {
if (!item.collection) {
return null;
}
return config.bookCollections
.find((x) => x.id === item.collection)
.books.find((x) => x.id === item.book);
Expand All @@ -25,6 +28,9 @@ function getDamId(item: { book: any; chapter: string }) {

export function logScreenView(item: HistoryItem) {
const book = getBook({ ...item });
if (!book) {
return;
}
const chapter = item.chapter;
const bookAbbrev = book.abbreviation;
const damId = getDamId({ book, chapter });
Expand Down

0 comments on commit af2b1de

Please sign in to comment.