Commit c866b340 authored by Robert Knight's avatar Robert Knight

Fix a few errors relating to optional properties

TS 4.0 treats optional properties of type T as `T|undefined` and
produces an error if the property is used in a situation where it may
not be set.
parent ef7ce344
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
* @prop {number} totalChildren - Computed count of this thread's immediate * @prop {number} totalChildren - Computed count of this thread's immediate
* children. This count includes visually-hidden threads. * children. This count includes visually-hidden threads.
* @prop {number} replyCount - Computed count of all replies to a thread * @prop {number} replyCount - Computed count of all replies to a thread
* @prop {number} [depth] - The thread's depth in the hierarchy * @prop {number} depth - The thread's depth in the hierarchy
*/ */
/** /**
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
*/ */
const DEFAULT_THREAD_STATE = { const DEFAULT_THREAD_STATE = {
collapsed: false, collapsed: false,
depth: 0,
visible: true, visible: true,
replyCount: 0, replyCount: 0,
totalChildren: 0, totalChildren: 0,
...@@ -257,19 +258,19 @@ function hasVisibleChildren(thread) { ...@@ -257,19 +258,19 @@ function hasVisibleChildren(thread) {
/** /**
* @typedef Options * @typedef Options
* @prop {string[]} [selected] - List of currently-selected annotation ids, from * @prop {string[]} selected - List of currently-selected annotation ids, from
* the data store * the data store
* @prop {string[]} [forcedVisible] - List of $tags of annotations that have * @prop {string[]} forcedVisible - List of $tags of annotations that have
* been explicitly expanded by the user, even if they don't * been explicitly expanded by the user, even if they don't
* match current filters * match current filters
* @prop {(a: Annotation) => boolean} [filterFn] - Predicate function that * @prop {(a: Annotation) => boolean} [filterFn] - Predicate function that
* returns `true` if annotation should be visible * returns `true` if annotation should be visible
* @prop {(t: Thread) => boolean} [threadFilterFn] - Predicate function that * @prop {(t: Thread) => boolean} [threadFilterFn] - Predicate function that
* returns `true` if the annotation should be included in the thread tree * returns `true` if the annotation should be included in the thread tree
* @prop {Object.<string, boolean>} [expanded] - Map of thread id => expansion state * @prop {Object.<string, boolean>} expanded - Map of thread id => expansion state
* @prop {(a: Annotation, b: Annotation) => boolean} [sortCompareFn] - Less-than * @prop {(a: Annotation, b: Annotation) => boolean} sortCompareFn - Less-than
* comparison function for sorting top-level annotations * comparison function for sorting top-level annotations
* @prop {(a: Annotation, b: Annotation) => boolean} [replySortCompareFn] - Less-than * @prop {(a: Annotation, b: Annotation) => boolean} replySortCompareFn - Less-than
* comparison function for sorting replies * comparison function for sorting replies
*/ */
...@@ -281,8 +282,9 @@ function hasVisibleChildren(thread) { ...@@ -281,8 +282,9 @@ function hasVisibleChildren(thread) {
const defaultOpts = { const defaultOpts = {
selected: [], selected: [],
expanded: {}, expanded: {},
forcedVisible: [],
sortCompareFn: (a, b) => { sortCompareFn: (a, b) => {
return a.id < b.id; return a.$tag < b.$tag;
}, },
replySortCompareFn: (a, b) => { replySortCompareFn: (a, b) => {
return a.created < b.created; return a.created < b.created;
...@@ -315,11 +317,8 @@ const defaultOpts = { ...@@ -315,11 +317,8 @@ const defaultOpts = {
export default function buildThread(annotations, options) { export default function buildThread(annotations, options) {
const opts = { ...defaultOpts, ...options }; const opts = { ...defaultOpts, ...options };
const annotationsFiltered = !!opts.filterFn;
const threadsFiltered = !!opts.threadFilterFn;
const hasSelection = opts.selected.length > 0; const hasSelection = opts.selected.length > 0;
const hasForcedVisible = opts.forcedVisible && opts.forcedVisible.length; const hasForcedVisible = opts.forcedVisible.length > 0;
let thread = threadAnnotations(annotations); let thread = threadAnnotations(annotations);
...@@ -336,7 +335,7 @@ export default function buildThread(annotations, options) { ...@@ -336,7 +335,7 @@ export default function buildThread(annotations, options) {
}); });
} }
if (threadsFiltered) { if (opts.threadFilterFn) {
// Remove threads not matching thread-level filters // Remove threads not matching thread-level filters
thread.children = thread.children.filter(opts.threadFilterFn); thread.children = thread.children.filter(opts.threadFilterFn);
} }
...@@ -347,7 +346,7 @@ export default function buildThread(annotations, options) { ...@@ -347,7 +346,7 @@ export default function buildThread(annotations, options) {
if (!thread.annotation) { if (!thread.annotation) {
threadIsVisible = false; // Nothing to show threadIsVisible = false; // Nothing to show
} else if (annotationsFiltered) { } else if (opts.filterFn) {
if ( if (
hasForcedVisible && hasForcedVisible &&
opts.forcedVisible.includes(thread.annotation.$tag) opts.forcedVisible.includes(thread.annotation.$tag)
...@@ -380,8 +379,7 @@ export default function buildThread(annotations, options) { ...@@ -380,8 +379,7 @@ export default function buildThread(annotations, options) {
} else { } else {
// If annotations are filtered, and at least one child matches // If annotations are filtered, and at least one child matches
// those filters, make sure thread is not collapsed // those filters, make sure thread is not collapsed
const hasUnfilteredChildren = const hasUnfilteredChildren = opts.filterFn && hasVisibleChildren(thread);
annotationsFiltered && hasVisibleChildren(thread);
threadStates.collapsed = thread.collapsed && !hasUnfilteredChildren; threadStates.collapsed = thread.collapsed && !hasUnfilteredChildren;
} }
return { ...thread, ...threadStates }; return { ...thread, ...threadStates };
......
...@@ -32,7 +32,7 @@ const sortFns = { ...@@ -32,7 +32,7 @@ const sortFns = {
function buildRootThread(threadState) { function buildRootThread(threadState) {
const selection = threadState.selection; const selection = threadState.selection;
/** @type {BuildThreadOptions} */ /** @type {Partial<BuildThreadOptions>} */
const options = { const options = {
expanded: selection.expanded, expanded: selection.expanded,
forcedVisible: selection.forcedVisible, forcedVisible: selection.forcedVisible,
......
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