Commit 971e51f5 authored by Robert Knight's avatar Robert Knight

Add temporary fix for crash when creating page notes

The `getSegmentSelector` function assumed all annotations have at least one
target. This is true for all saved annotations and all new annotations with
selectors, but is not true for new page notes.

This is a stop-gap fix. The better solution will be to eliminate the
inconsistency and make it so that all annotations have a non-empty `target`
field.
parent 911ef334
...@@ -39,7 +39,15 @@ export type ThreadListProps = { ...@@ -39,7 +39,15 @@ export type ThreadListProps = {
* belongs to. * belongs to.
*/ */
function getSegmentSelector(ann: Annotation): EPUBContentSelector | undefined { function getSegmentSelector(ann: Annotation): EPUBContentSelector | undefined {
return ann.target[0].selector?.find(s => s.type === 'EPUBContentSelector') as const target = ann.target[0];
if (!target) {
// Page Notes that have just been created do not have a target. All saved
// annotations and new annotations/highlights do.
//
// FIXME - Fix this inconsistency for page notes.
return undefined;
}
return target.selector?.find(s => s.type === 'EPUBContentSelector') as
| EPUBContentSelector | EPUBContentSelector
| undefined; | undefined;
} }
......
...@@ -26,8 +26,8 @@ describe('ThreadList', () => { ...@@ -26,8 +26,8 @@ describe('ThreadList', () => {
return wrapper; return wrapper;
} }
function createThread(tag) { function createThread(tag, target) {
const target = [ const defaultTarget = [
{ {
source: 'https://example.com', source: 'https://example.com',
}, },
...@@ -35,7 +35,7 @@ describe('ThreadList', () => { ...@@ -35,7 +35,7 @@ describe('ThreadList', () => {
return { return {
id: tag, id: tag,
children: [], children: [],
annotation: { $tag: tag, target }, annotation: { $tag: tag, target: target ?? defaultTarget },
}; };
} }
...@@ -61,7 +61,10 @@ describe('ThreadList', () => { ...@@ -61,7 +61,10 @@ describe('ThreadList', () => {
createThread('t1'), createThread('t1'),
createThread('t2'), createThread('t2'),
createThread('t3'), createThread('t3'),
createThread('t4'),
// Add an annotation with an empty target. This matches how new
// (but not saved) page notes are currently created.
createThread('t4', []),
], ],
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment